diff --git a/ArnabG99.ipynb b/ArnabG99.ipynb index 9e2543a..18aeecf 100644 --- a/ArnabG99.ipynb +++ b/ArnabG99.ipynb @@ -1,32 +1,1191 @@ { - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "ArnabG99.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + } }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.5.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/ArnabG99/Assignment-2/blob/ArnabG99/ArnabG99.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": [ + "import random\n", + "dummy_list = [random.randrange(1, 100) for i in range(0, 5)]" + ], + "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": 34 + }, + "outputId": "a15132fe-f21b-4fe2-8e81-89ba25ab824d" + }, + "cell_type": "code", + "source": [ + "print(dummy_list)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[18, 94, 99, 51, 58]\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": 34 + }, + "outputId": "456a1355-325b-40cd-ad51-1e352e47996e" + }, + "cell_type": "code", + "source": [ + "dummy_list = dummy_list[ : :-1]\n", + "print(dummy_list)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[58, 51, 99, 94, 18]\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": 34 + }, + "outputId": "58dc0c78-310c-4b13-e108-c2854fb98287" + }, + "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_2 + dummy_list\n", + "print(dummy_list)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02, 58, 51, 99, 94, 18]\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 = {}\n", + "dummy_dict = {x:dummy_list.count(x) for x in dummy_list}" + ], + "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": 34 + }, + "outputId": "5611f4ab-d46b-4ace-d662-be09cbb7506c" + }, + "cell_type": "code", + "source": [ + "print(dummy_dict)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{2: 1, 200: 1, 16: 1, 4: 1, 1: 1, 0: 1, 9.45: 1, 45.67: 1, 90: 1, 12.01: 1, 12.02: 1, 58: 1, 51: 1, 99: 1, 94: 1, 18: 1}\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": 34 + }, + "outputId": "57328bd2-abf0-4618-8b44-fa839f0e2804" + }, + "cell_type": "code", + "source": [ + "dummy_list.sort(reverse=True)\n", + "print(dummy_list)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[200, 99, 94, 90, 58, 51, 45.67, 18, 16, 12.02, 12.01, 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": {} + }, + "cell_type": "code", + "source": [ + "x = 200\n", + "dummy_list.remove(x)\n", + "# Let's play: try the same with something which is not in the list to get the ValueError" + ], + "execution_count": 0, + "outputs": [] + }, + { + "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": 34 + }, + "outputId": "2513b306-ea9a-4859-f243-15b5b36c12ad" + }, + "cell_type": "code", + "source": [ + "dummy_list.pop(random.randrange(0,len(dummy_list)))\n", + "# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "12.01" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 10 + } + ] + }, + { + "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": 34 + }, + "outputId": "69503e5e-3243-42da-b149-3d069f5dd956" + }, + "cell_type": "code", + "source": [ + "dummy_list = []\n", + "print(dummy_list)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[]\n" + ], + "name": "stdout" + } + ] + }, + { + "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": 85 + }, + "outputId": "50cdd551-74a5-4f1c-905f-8461b08c75c8" + }, + "cell_type": "code", + "source": [ + "a = np.array([1, 2, 3]) # Create a rank 1 array\n", + "print(a)\n", + "print(type(a)) #print type of a\n", + "\n", + "b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array\n", + "print(b.shape) # Prints \"(2, 3)\"\n", + "print(b[0, 0], b[0, 1], b[1, 0])" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3]\n", + "\n", + "(2, 3)\n", + "1 2 4\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": 221 + }, + "outputId": "1d886dea-1217-4b2c-bef9-d8cbe8036dca" + }, + "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('b',b)\n", + "print('c',c)\n", + "print('d',d)\n", + "print('e',e)" + ], + "execution_count": 0, + "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.37285472 0.61570729]\n", + " [0.91580759 0.42228578]]\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": 85 + }, + "outputId": "3d0de866-b167-4939-8a47-2678a50ed471" + }, + "cell_type": "code", + "source": [ + "a == np.arange(10)\n", + "b == np.linspace(0,10, num=6)\n", + "print(a)\n", + "print(b)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0. 0.]\n", + " [0. 0.]]\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" + ], + "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": 51 + }, + "outputId": "2f010fca-1f2a-421a-da82-e73c3492b61b" + }, + "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]) # 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]) # Prints \"77\"" + ], + "execution_count": 0, + "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": 119 + }, + "outputId": "1be31dd7-5581-48b0-f6b5-48462f62138d" + }, + "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) # Prints \"[5 6 7 8] (4,)\"\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) # Prints \"[ 2 6 10] (3,)\"\n", + "print(col_r2, col_r2.shape)" + ], + "execution_count": 0, + "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": 68 + }, + "outputId": "ce7eb63a-fa06-4181-b383-3a55c37c287c" + }, + "cell_type": "code", + "source": [ + "x = np.array([[1,2],[3,4]])\n", + "\n", + "print(np.sum(x)) # Compute sum of all elements; prints \"10\"\n", + "print(np.sum(x, axis=0)) # Compute sum of each column; prints \"[4 6]\"\n", + "print(np.sum(x, axis=1)) # Compute sum of each row; prints \"[3 7]\"" + ], + "execution_count": 0, + "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": 68 + }, + "outputId": "60ac9e87-c685-4023-a3eb-855584c06286" + }, + "cell_type": "code", + "source": [ + "b = np.arange(10)\n", + "\n", + "print(b)\n", + "\n", + "mask = b%2!=0 #perform computations on the list \n", + "\n", + "print(mask)\n", + "\n", + "print(b[mask]) #applying the mask on the numpy array\n" + ], + "execution_count": 0, + "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": 34 + }, + "outputId": "b7ddb3c6-f262-4667-e165-3bc9d6f57714" + }, + "cell_type": "code", + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "print(modified_b)" + ], + "execution_count": 0, + "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": 119 + }, + "outputId": "6c467cd2-b86a-428b-86e1-fb19fcd797f4" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[:, [1,0,2]])" + ], + "execution_count": 0, + "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": 119 + }, + "outputId": "595b61f9-dc76-427a-b97d-ea3bdb4cbcd7" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[[1,0,2], :])" + ], + "execution_count": 0, + "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" + } + ] + }, + { + "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": 204 + }, + "outputId": "b7758370-a597-45fa-8668-ee2738a67c99" + }, + "cell_type": "code", + "source": [ + "import numpy as np #import numpy\n", + "n = np.linspace(-1.3,2.5,64)\n", + "print(n)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[-1.3 -1.23968254 -1.17936508 -1.11904762 -1.05873016 -0.9984127\n", + " -0.93809524 -0.87777778 -0.81746032 -0.75714286 -0.6968254 -0.63650794\n", + " -0.57619048 -0.51587302 -0.45555556 -0.3952381 -0.33492063 -0.27460317\n", + " -0.21428571 -0.15396825 -0.09365079 -0.03333333 0.02698413 0.08730159\n", + " 0.14761905 0.20793651 0.26825397 0.32857143 0.38888889 0.44920635\n", + " 0.50952381 0.56984127 0.63015873 0.69047619 0.75079365 0.81111111\n", + " 0.87142857 0.93174603 0.99206349 1.05238095 1.11269841 1.17301587\n", + " 1.23333333 1.29365079 1.35396825 1.41428571 1.47460317 1.53492063\n", + " 1.5952381 1.65555556 1.71587302 1.77619048 1.83650794 1.8968254\n", + " 1.95714286 2.01746032 2.07777778 2.13809524 2.1984127 2.25873016\n", + " 2.31904762 2.37936508 2.43968254 2.5 ]\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": 34 + }, + "outputId": "061271cf-508d-420b-d56b-900f1e91ff92" + }, + "cell_type": "code", + "source": [ + "a = np.array([(1,2,3)])\n", + "b = np.resize(a,3*3)\n", + "print(b)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3 1 2 3 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": 34 + }, + "outputId": "0510a779-ab25-4c67-e57a-3cfd6bac7a8b" + }, + "cell_type": "code", + "source": [ + "b = np.arange(1,20,2)\n", + "print(b)" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 1 3 5 7 9 11 13 15 17 19]\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": 34 + }, + "outputId": "eafa543d-3d10-4daa-f6a7-827a3a0534d3" + }, + "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", + "i = np.intersect1d(a,b)\n", + "print(i)" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[2 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": 51 + }, + "outputId": "db82b55f-db42-420f-92df-5ccbd5e5c2d3" + }, + "cell_type": "code", + "source": [ + "a = np.arange(10)\n", + "a = a.reshape(2,5)\n", + "print(a)" + ], + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[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": 51 + }, + "outputId": "14816a12-7bdb-4b4b-a91b-d21c47fa0c22" + }, + "cell_type": "code", + "source": [ + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "arr = np.array(a)\n", + "print(arr)\n", + "li = list(a)\n", + "print(li)\n" + ], + "execution_count": 26, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3 4 5 6 7 8 9]\n", + "[1, 2, 3, 4, 5, 6, 7, 8, 9]\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": 187 + }, + "outputId": "9ac4275d-9fdd-4909-a8f6-0ee895f0707e" + }, + "cell_type": "code", + "source": [ + "zero = np.zeros((10,10))\n", + "zero[0:,0] = 1\n", + "zero[0:,9] = 1\n", + "zero[0,0:] = 1\n", + "zero[9,0:] = 1\n", + "\n", + "print(zero)" + ], + "execution_count": 37, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[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": 153 + }, + "outputId": "67a7f749-8028-4cca-be50-d53e351f094d" + }, + "cell_type": "code", + "source": [ + "t = np.eye(8,8)\n", + "print(t)" + ], + "execution_count": 38, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[1. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 1. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 1. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 1. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 1. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 1. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 1. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 1.]]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/Lists.ipynb b/Lists.ipynb new file mode 100644 index 0000000..ca3e74d --- /dev/null +++ b/Lists.ipynb @@ -0,0 +1,368 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Copy of 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/ArnabG99/Assignment-2/blob/ArnabG99/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": [ + "import random\n", + "dummy_list = [random.randrange(1, 100) for i in range(0, 5)]" + ], + "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": 34 + }, + "outputId": "216d6b6f-854e-40fe-f8dd-961b624c6a61" + }, + "cell_type": "code", + "source": [ + "print(dummy_list)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[26, 39, 87, 62, 82]\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": 34 + }, + "outputId": "aab5b4f7-3dc3-4921-e720-d09c4755bcf6" + }, + "cell_type": "code", + "source": [ + "dummy_list = dummy_list[ : :-1]\n", + "print(dummy_list)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[82, 62, 87, 39, 26]\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": 34 + }, + "outputId": "65464133-f734-47ec-ed7d-5b80b9112ac7" + }, + "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_2 + dummy_list\n", + "print(dummy_list)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02, 82, 62, 87, 39, 26]\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 = {}\n", + "for num in dummy_list:\n", + " dummy_dict[num] = \"frequency\"" + ], + "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": 54 + }, + "outputId": "aa29d7c3-dd1c-4bd7-c9f1-5a6710d74bb4" + }, + "cell_type": "code", + "source": [ + "print(dummy_dict)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{2: 'frequency', 200: 'frequency', 16: 'frequency', 4: 'frequency', 1: 'frequency', 0: 'frequency', 9.45: 'frequency', 45.67: 'frequency', 90: 'frequency', 12.01: 'frequency', 12.02: 'frequency', 82: 'frequency', 62: 'frequency', 87: 'frequency', 39: 'frequency', 26: '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": 34 + }, + "outputId": "2f1e6c8f-cff5-447e-e633-148e3749fd93" + }, + "cell_type": "code", + "source": [ + "dummy_list.sort(reverse=True)\n", + "print(dummy_list)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[200, 90, 87, 82, 62, 45.67, 39, 26, 16, 12.02, 12.01, 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": {} + }, + "cell_type": "code", + "source": [ + "x = 200\n", + "dummy_list.remove(x)\n", + "# Let's play: try the same with something which is not in the list to get the ValueError" + ], + "execution_count": 0, + "outputs": [] + }, + { + "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": 34 + }, + "outputId": "edb5dd1c-996d-44a7-9191-d441295df74b" + }, + "cell_type": "code", + "source": [ + "dummy_list.pop(random.randrange(0,len(dummy_list)))\n", + "# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "39" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 31 + } + ] + }, + { + "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": 34 + }, + "outputId": "c5a0c82e-8e86-4e51-f8d1-afa1300c5877" + }, + "cell_type": "code", + "source": [ + "dummy_list = []\n", + "print(dummy_list)" + ], + "execution_count": 0, + "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..167b241 --- /dev/null +++ b/Numpy_Examples_1.ipynb @@ -0,0 +1,356 @@ +{ + "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/ArnabG99/Assignment-2/blob/ArnabG99/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": {} + }, + "cell_type": "code", + "source": [ + "a = np.array([1, 2, 3]) # Create a rank 1 array\n", + "#print(a)\n", + "#print(type(a)) #print type of a\n", + "\n", + "b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array\n", + "#print(b.shape) # Prints \"(2, 3)\"\n", + "#print(b[0, 0], b[0, 1], b[1, 0])" + ], + "execution_count": 0, + "outputs": [] + }, + { + "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": {} + }, + "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('b',b)\n", + "#print('c',c)\n", + "#print('d',d)\n", + "#print('e',e)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "8RPW_SutukjF", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Execute and understand :)" + ] + }, + { + "metadata": { + "id": "-8JuqYt4upeo", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "a == np.arange(10)\n", + "b == np.linspace(0,10, num=6)\n", + "#print(a)\n", + "#print(b)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "MRHhbjx4uvYN", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Array Indexing" + ] + }, + { + "metadata": { + "id": "grF5_yUSuxVK", + "colab_type": "code", + "colab": {} + }, + "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]) # 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]) # Prints \"77\"" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "s400Gijxu0kO", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Slicing" + ] + }, + { + "metadata": { + "id": "kubpegh2u4zF", + "colab_type": "code", + "colab": {} + }, + "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) # Prints \"[5 6 7 8] (4,)\"\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) # Prints \"[ 2 6 10] (3,)\"\n", + "#print(col_r2, col_r2.shape)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "TmGnCO3AvE8t", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Aritmetic operations" + ] + }, + { + "metadata": { + "id": "YvBw3ImjvGqD", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "x = np.array([[1,2],[3,4]])\n", + "\n", + "#print(np.sum(x)) # Compute sum of all elements; prints \"10\"\n", + "#print(np.sum(x, axis=0)) # Compute sum of each column; prints \"[4 6]\"\n", + "#print(np.sum(x, axis=1)) # Compute sum of each row; prints \"[3 7]\"" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "uaVY3ZzD4pC2", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Using Boolean Mask" + ] + }, + { + "metadata": { + "id": "-PNfOMvh4_Gp", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "b = np.arange(10)\n", + "\n", + "#print(b)\n", + "\n", + "mask = b%2!=0 #perform computations on the list \n", + "\n", + "#print(mask)\n", + "\n", + "#print(b[mask]) #applying the mask on the numpy array\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "HbEPBbz-5J9K", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "#print(modified_b)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "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": {} + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "#print(a)\n", + "\n", + "#print(a[:, [1,0,2]])" + ], + "execution_count": 0, + "outputs": [] + }, + { + "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": {} + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "#print(a)\n", + "\n", + "#print(arr[[1,0,2], :])" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file