diff --git a/Lists.ipynb b/Lists.ipynb new file mode 100644 index 0000000..38e52c9 --- /dev/null +++ b/Lists.ipynb @@ -0,0 +1,393 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Lists.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/sayanmondal31/Assignment-2/blob/sayanmondal31/Lists.ipynb)" + ] + }, + { + "metadata": { + "id": "e0R1W0Vzm4UU", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Exercise - List" + ] + }, + { + "metadata": { + "id": "TrO7XNQnnQZ7", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "1) Create any random list and assign it to a variable dummy_list" + ] + }, + { + "metadata": { + "id": "bjl-2QkznWid", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "dummy_list=[12,13,15,14,16]" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "cDjddNGfngnp", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "2) print dummy_list" + ] + }, + { + "metadata": { + "id": "RVL5178inz9M", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 33 + }, + "outputId": "6e09e49b-ae3a-466f-e34c-934da5fd7bf6" + }, + "cell_type": "code", + "source": [ + "print(dummy_list)" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[12, 13, 15, 14, 16]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "15jKDXxkn16M", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "3) Reverse dummy_list and print" + ] + }, + { + "metadata": { + "id": "bYa9gFOOn-4o", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 33 + }, + "outputId": "dab6fa13-3329-474e-8932-91d5cddbb6b6" + }, + "cell_type": "code", + "source": [ + "dummy_list.reverse()\n", + "print(dummy_list)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[16, 14, 15, 13, 12]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "EShv0nfXpUys", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "4) Add the list dummy_list_2 to the previous dummy_list and now print dummy_list" + ] + }, + { + "metadata": { + "id": "Ngkc7hnYphg6", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 33 + }, + "outputId": "2e0eb0c5-7cd7-45f0-8bd7-9a75384d5981" + }, + "cell_type": "code", + "source": [ + "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "dummy_list = dummy_list + dummy_list_2\n", + "print(dummy_list)" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[16, 15, 14, 13, 12, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Le1aRTuYoDzS", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "5) Create a dictionary named dummy_dict which contains all the elements of dummy_list as keys and frequency as values. " + ] + }, + { + "metadata": { + "id": "VHfSR_Csthnk", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "dummy_dict={\"keys\":\"dummy_list\",\"values\":\"frequency\"}" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "rtPC6WkSos-y", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "RgCYpFXGou6q", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "6) print dummy_dict" + ] + }, + { + "metadata": { + "id": "qe5E5IgxpTWU", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 33 + }, + "outputId": "09b92109-c2ea-4fbf-8c59-c6e9e0bc14f9" + }, + "cell_type": "code", + "source": [ + "print(dummy_dict)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{'keys': 'dummy_list', 'values': 'frequency'}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "8n_nsBDup4--", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "7) Sort dummy_list in ascending order as well as descending order and print the changed lists " + ] + }, + { + "metadata": { + "id": "Z_m7vr26qKnK", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 50 + }, + "outputId": "1f7c57de-9f9b-400f-eced-529372acc45a" + }, + "cell_type": "code", + "source": [ + "dummy_list.sort()\n", + "print(\"ascending order \",dummy_list)\n", + "dummy_list.sort(reverse=True)\n", + "print(\"descending order \",dummy_list)" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "ascending order [0, 1, 2, 4, 9.45, 12, 12.01, 12.02, 13, 14, 15, 16, 16, 45.67, 90, 200]\n", + "descending order [200, 90, 45.67, 16, 16, 15, 14, 13, 12.02, 12.01, 12, 9.45, 4, 2, 1, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Znm5Qo4LqPKA", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "8) Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item." + ] + }, + { + "metadata": { + "id": "1-8mlngDqYvS", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 33 + }, + "outputId": "e4b6f5a3-1ad6-4334-9ea2-9f1836506c2b" + }, + "cell_type": "code", + "source": [ + "x = 200\n", + "dummy_list.remove(x)\n", + "print(dummy_list)\n", + "\n", + "# Let's play: try the same with something which is not in the list to get the ValueError" + ], + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[90, 45.67, 16, 16, 15, 14, 13, 12.02, 12.01, 12, 9.45, 4, 2, 1, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "QPB6iGbeqviN", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "9) Remove the item at position x. x is any random integer" + ] + }, + { + "metadata": { + "id": "aMyo1gmRrVHo", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 33 + }, + "outputId": "cb1345ba-00f4-44cc-c879-0043b7dc4b87" + }, + "cell_type": "code", + "source": [ + "# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get\n", + "x = x>len(dummy_list)+1\n", + "dummy_list.remove(x)\n", + "print(dummy_list)\n", + "\n" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[90, 45.67, 16, 16, 15, 14, 13, 12.02, 12.01, 12, 9.45, 4, 2, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "bqQnnsr8rm6G", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "10) Let's clean everything clear the list and then print" + ] + }, + { + "metadata": { + "id": "qBC8lKpLrtJW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 33 + }, + "outputId": "f4d3e45e-92e9-4c47-d913-087ea350c42f" + }, + "cell_type": "code", + "source": [ + "dummy_list.clear()\n", + "print(dummy_list)\n" + ], + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/Numpy_Examples_1.ipynb b/Numpy_Examples_1.ipynb new file mode 100644 index 0000000..457c282 --- /dev/null +++ b/Numpy_Examples_1.ipynb @@ -0,0 +1,553 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Numpy_Examples 1.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/sayanmondal31/Assignment-2/blob/sayanmondal31/Numpy_Examples_1.ipynb)" + ] + }, + { + "metadata": { + "id": "3pSVAeWfuPcq", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Numpy Examples\n", + "\n", + "## What is numpy?\n", + "\n", + "#### Python has built-in:\n", + "\n", + "- containers: lists (costless insertion and append), dictionnaries (fast lookup)\n", + "- high-level number objects: integers, floating point\n", + "\n", + "#### Numpy is:\n", + "\n", + " - extension package to Python for multidimensional arrays\n", + " - closer to hardware (efficiency)\n", + " - designed for scientific computation (convenience)\n", + "\n", + "\n", + "#### Import numpy\n", + "\n" + ] + }, + { + "metadata": { + "id": "ozUi4_X55UHE", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "import numpy as np" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "3-1ghFDF5N2z", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "### Uncomment Print statement and run each cell to see the output\n", + "\n", + "#### Create numpy arrays\n" + ] + }, + { + "metadata": { + "id": "atYpk2ert0b-", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 84 + }, + "outputId": "2afd4309-e1a2-4776-df66-990ba8e997e7" + }, + "cell_type": "code", + "source": [ + "a = np.array([1, 2, 3]) # Create a rank 1 array\n", + "#print(a)\n", + "print(a)\n", + "#print(type(a)) #print type of a\n", + "print(type(a))\n", + "\n", + "b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array\n", + "#print(b.shape) \n", + "print(b.shape)\n", + "# Prints \"(2, 3)\"\n", + "#print(b[0, 0], b[0, 1], b[1, 0])\n", + "print(b[0,0],b[0,1],b[1,1])" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3]\n", + "\n", + "(2, 3)\n", + "1 2 5\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Kro5ZOwXue5n", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Some basic functions for creating arrays. Print all the defined arrays and see the results." + ] + }, + { + "metadata": { + "id": "V3rdzgr9uhHS", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 217 + }, + "outputId": "cd5f19bd-59b9-4dd1-f1bc-791fc575b619" + }, + "cell_type": "code", + "source": [ + "a = np.zeros(shape=(2,2))\n", + "b = np.ones(shape = (3,3))\n", + "c = np.eye(2)\n", + "d = np.full(shape=(3,3), fill_value=5)\n", + "e = np.random.random((2,2))\n", + "\n", + "#print('a', a)\n", + "print('a',a)\n", + "#print('b',b)\n", + "print('b',b)\n", + "#print('c',c)\n", + "print('c',c)\n", + "#print('d',d)\n", + "print('d',d)\n", + "#print('e',e)\n", + "print('e',e)\n" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "a [[0. 0.]\n", + " [0. 0.]]\n", + "b [[1. 1. 1.]\n", + " [1. 1. 1.]\n", + " [1. 1. 1.]]\n", + "c [[1. 0.]\n", + " [0. 1.]]\n", + "d [[5 5 5]\n", + " [5 5 5]\n", + " [5 5 5]]\n", + "e [[0.61900735 0.08418817]\n", + " [0.52379918 0.50231734]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "8RPW_SutukjF", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Execute and understand :)" + ] + }, + { + "metadata": { + "id": "-8JuqYt4upeo", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 167 + }, + "outputId": "11c86bef-86d0-4132-8c22-7f73b151800a" + }, + "cell_type": "code", + "source": [ + "a == np.arange(10)\n", + "b == np.linspace(0,10, num=6)\n", + "#print(a)\n", + "print(a)\n", + "#print(b)\n", + "print(b)\n" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0. 0.]\n", + " [0. 0.]]\n", + "[[1. 1. 1.]\n", + " [1. 1. 1.]\n", + " [1. 1. 1.]]\n" + ], + "name": "stdout" + }, + { + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.\n", + " \"\"\"Entry point for launching an IPython kernel.\n", + "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:2: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.\n", + " \n" + ], + "name": "stderr" + } + ] + }, + { + "metadata": { + "id": "MRHhbjx4uvYN", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Array Indexing" + ] + }, + { + "metadata": { + "id": "grF5_yUSuxVK", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 50 + }, + "outputId": "79330543-ef66-4c52-e3d2-f05ec1d093fb" + }, + "cell_type": "code", + "source": [ + "a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n", + "\n", + "# Use slicing to pull out the subarray consisting of the first 2 rows\n", + "# and columns 1 and 2; b is the following array of shape (2, 2):\n", + "# [[2 3]\n", + "# [6 7]]\n", + "b = a[:2, 1:3]\n", + "\n", + "# A slice of an array is a view into the same data, so modifying it\n", + "# will modify the original array.\n", + "\n", + "#print(a[0, 1])\n", + "print(a[0,1])# Prints \"2\"\n", + "\n", + "b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]\n", + "#print(a[0, 1])\n", + "print(a[0,1])# Prints \"77\"" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n", + "77\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "s400Gijxu0kO", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Slicing" + ] + }, + { + "metadata": { + "id": "kubpegh2u4zF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 117 + }, + "outputId": "8d226701-23ab-4377-8d79-72bbe1a9bc9d" + }, + "cell_type": "code", + "source": [ + "a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n", + "\n", + "row_r1 = a[1, :] # Rank 1 view of the second row of a\n", + "row_r2 = a[1:2, :] # Rank 2 view of the second row of a\n", + "\n", + "#print(row_r1, row_r1.shape)\n", + "print(row_r1, row_r1.shape)# Prints \"[5 6 7 8] (4,)\"\n", + "#print(row_r2, row_r2.shape) \n", + "print(row_r2,row_r2.shape)# Prints \"[[5 6 7 8]] (1, 4)\"\n", + "\n", + "col_r1 = a[:, 1]\n", + "col_r2 = a[:, 1:2]\n", + "\n", + "#print(col_r1, col_r1.shape)\n", + "print(col_r1,col_r1.shape)# Prints \"[ 2 6 10] (3,)\"\n", + "#print(col_r2, col_r2.shape)\n", + "print(col_r2,col_r2.shape)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[5 6 7 8] (4,)\n", + "[[5 6 7 8]] (1, 4)\n", + "[ 2 6 10] (3,)\n", + "[[ 2]\n", + " [ 6]\n", + " [10]] (3, 1)\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "TmGnCO3AvE8t", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Aritmetic operations" + ] + }, + { + "metadata": { + "id": "YvBw3ImjvGqD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 67 + }, + "outputId": "77e731e4-10b9-4abe-cd4e-b1c29830ea81" + }, + "cell_type": "code", + "source": [ + "x = np.array([[1,2],[3,4]])\n", + "\n", + "#print(np.sum(x)) \n", + "print(np.sum(x))# Compute sum of all elements; prints \"10\"\n", + "#print(np.sum(x, axis=0))\n", + "print(np.sum(x,axis=0))# Compute sum of each column; prints \"[4 6]\"\n", + "#print(np.sum(x, axis=1))\n", + "print(np.sum(x,axis=1))# Compute sum of each row; prints \"[3 7]\"" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10\n", + "[4 6]\n", + "[3 7]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "uaVY3ZzD4pC2", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Using Boolean Mask" + ] + }, + { + "metadata": { + "id": "-PNfOMvh4_Gp", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 67 + }, + "outputId": "ed1c77b2-a6fb-430b-8d96-aa98f1da0197" + }, + "cell_type": "code", + "source": [ + "b = np.arange(10)\n", + "\n", + "#print(b)\n", + "print(b)\n", + "\n", + "mask = b%2!=0 #perform computations on the list \n", + "\n", + "#print(mask)\n", + "print(mask)\n", + "\n", + "#print(b[mask]) \n", + "print(b[mask])#applying the mask on the numpy array\n" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0 1 2 3 4 5 6 7 8 9]\n", + "[False True False True False True False True False True]\n", + "[1 3 5 7 9]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "HbEPBbz-5J9K", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 33 + }, + "outputId": "3ac6cba8-8059-4ca1-fddf-6c3dfecd2263" + }, + "cell_type": "code", + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "#print(modified_b)\n", + "print(modified_b)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 0 -1 2 -1 4 -1 6 -1 8 -1]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "zgSd71EEAHC7", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Swapping two columns in a 2d numpy array" + ] + }, + { + "metadata": { + "id": "-cvqeXd_AGo1", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 117 + }, + "outputId": "c60d276f-cf97-4238-9f87-d4fb5ca18ed2" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "#print(a)\n", + "print(a)\n", + "\n", + "#print(a[:, [1,0,2]])\n", + "print(a[:,[1,0,2]])" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0 1 2]\n", + " [3 4 5]\n", + " [6 7 8]]\n", + "[[1 0 2]\n", + " [4 3 5]\n", + " [7 6 8]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "U7ifiLY3Ayky", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Swapping two rows in a 2d numpy array" + ] + }, + { + "metadata": { + "id": "0FrOURRDAZNP", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 117 + }, + "outputId": "d3e48996-47fb-430a-9f57-5efdb0dabb7c" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "#print(a)\n", + "print(a)\n", + "\n", + "#print(arr[[1,0,2], :])\n", + "print(a[[1,0,2], :])\n" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0 1 2]\n", + " [3 4 5]\n", + " [6 7 8]]\n", + "[[3 4 5]\n", + " [0 1 2]\n", + " [6 7 8]]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/Numpy_Exercises.ipynb b/Numpy_Exercises.ipynb new file mode 100644 index 0000000..5c64d31 --- /dev/null +++ b/Numpy_Exercises.ipynb @@ -0,0 +1,379 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Numpy_Exercises.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/sayanmondal31/Assignment-2/blob/sayanmondal31/Numpy_Exercises.ipynb)" + ] + }, + { + "metadata": { + "id": "a_4UupTr9fbX", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Numpy Exercises\n", + "\n", + "1) Create a uniform subdivision of the interval -1.3 to 2.5 with 64 subdivisions" + ] + }, + { + "metadata": { + "id": "LIP5u4zi0Nmg", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 201 + }, + "outputId": "21588ff5-472a-4771-ccd2-04020f4a1dec" + }, + "cell_type": "code", + "source": [ + "import numpy as np #import numpy\n", + "s=np.random.uniform(-1.3,2.5,64)\n", + "print(s)\n" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[-0.92847835 2.16635285 1.58173917 1.61273914 1.60990939 -1.02543714\n", + " 0.46532421 1.10440527 1.89593727 -1.1124212 -0.54577597 0.42978527\n", + " 1.61450955 0.92872055 2.23775387 0.19406846 0.94577686 2.20516991\n", + " 0.73545259 1.15513843 2.32342577 2.10919311 -0.26367905 1.52965673\n", + " 0.72426996 -0.90306365 1.93898394 2.32504855 -0.1252837 2.4056438\n", + " 1.43768491 -0.45177326 0.53679716 -0.06122403 0.88628156 -0.2928114\n", + " -0.55744346 1.94641126 -1.19942371 -1.05753952 0.7124453 1.77629621\n", + " 1.39047877 1.21402328 2.0579934 0.68490074 -0.8244926 2.2477931\n", + " 1.1547666 -0.86531884 2.41155762 0.32280929 -0.58664679 1.00839942\n", + " 2.42928925 -0.76051337 1.81138519 2.0128754 -0.90171955 0.88895307\n", + " 2.00585918 -0.46717285 -1.15523489 -0.86037499]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "dBoH_A7M9jjL", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "2) Generate an array of length 3n filled with the cyclic pattern 1, 2, 3" + ] + }, + { + "metadata": { + "id": "4TxT66309n1o", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 67 + }, + "outputId": "cf818f11-d165-4d5d-b3e1-9924908d624d" + }, + "cell_type": "code", + "source": [ + "m=np.full(shape=(3,3),fill_value=[1,2,3])\n", + "print(m)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[1 2 3]\n", + " [1 2 3]\n", + " [1 2 3]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Vh-UKizx9oTp", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "3) Create an array of the first 10 odd integers." + ] + }, + { + "metadata": { + "id": "ebhEUZq29r32", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 33 + }, + "outputId": "dcf68f6b-e72d-4870-cef2-98ee43e56e02" + }, + "cell_type": "code", + "source": [ + "a=np.array([1,3,5,7,9])\n", + "print(a)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 3 5 7 9]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "QfJRdMat90f4", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "4) Find intersection of a and b" + ] + }, + { + "metadata": { + "id": "gOlfuJCo-JwF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 33 + }, + "outputId": "a3527c35-4874-4686-a02d-ec4356967eb0" + }, + "cell_type": "code", + "source": [ + "#expected output array([2, 4])\n", + "a = np.array([1,2,3,2,3,4,3,4,5,6])\n", + "b = np.array([7,2,10,2,7,4,9,4,9,8])\n", + "s = [val for val in a if val in b]\n", + "print(s)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[2, 2, 4, 4]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "RtVCf0UoCeB8", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "5) Reshape 1d array a to 2d array of 2X5" + ] + }, + { + "metadata": { + "id": "2E8b55_2Cjx5", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 67 + }, + "outputId": "e4737837-4711-45d4-da8c-a77beb10928c" + }, + "cell_type": "code", + "source": [ + "a = np.arange(10)\n", + "print(\"1d\",a)\n", + "a = np.arange(10).reshape(2,5)\n", + "print(\"2d\",a)" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1d [0 1 2 3 4 5 6 7 8 9]\n", + "2d [[0 1 2 3 4]\n", + " [5 6 7 8 9]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "dVrSBW1zEjp2", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "6) Create a numpy array to list and vice versa" + ] + }, + { + "metadata": { + "id": "tcBCyhXPEp9C", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 50 + }, + "outputId": "5ef498a7-6074-415e-acf9-ae056730db41" + }, + "cell_type": "code", + "source": [ + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "print(\"making list\",list(a))\n", + "a.reverse()\n", + "print(\"vice versa of list\",list(a))\n" + ], + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "text": [ + "making list [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "vice versa of list [9, 8, 7, 6, 5, 4, 3, 2, 1]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "JNqX8wnz9sQJ", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "7) Create a 10 x 10 arrays of zeros and then \"frame\" it with a border of ones." + ] + }, + { + "metadata": { + "id": "4bjP3JAc9vRD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 385 + }, + "outputId": "321ae8af-f65a-40f7-feb8-6d55c761acb5" + }, + "cell_type": "code", + "source": [ + "n=np.zeros(shape=(10,10))\n", + "print(\"original array\")\n", + "print(n)\n", + "n=np.ones(shape=(10,10))\n", + "print(\"frame with ones\")\n", + "n[1:-1,1:-1] = 0\n", + "print(n)\n" + ], + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "text": [ + "original array\n", + "[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]\n", + "frame with ones\n", + "[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "xaQgf8tT9v-n", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "8) Create an 8 x 8 array with a checkerboard pattern of zeros and ones using a slicing+striding approach." + ] + }, + { + "metadata": { + "id": "No7fx0Xy9zEh", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 167 + }, + "outputId": "bc9fa7b8-3ef8-4164-e0d5-27e66aa3b367" + }, + "cell_type": "code", + "source": [ + "x = np.ones((3,3))\n", + "print(\"Checkerboard pattern:\")\n", + "x = np.zeros((8,8),dtype=int)\n", + "x[1::2,::2] = 1\n", + "x[::2,1::2] = 1\n", + "print(x)" + ], + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Checkerboard pattern:\n", + "[[0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file