From 4a63d38420d8d2d67729c652546bf7e76b5220e7 Mon Sep 17 00:00:00 2001 From: Ayan Dutta Date: Tue, 25 Sep 2018 03:08:21 +0530 Subject: [PATCH 1/5] First 7 problems of List.ipynb solved. --- ayan59dutta.ipynb | 304 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 274 insertions(+), 30 deletions(-) diff --git a/ayan59dutta.ipynb b/ayan59dutta.ipynb index 9e2543a..b7b6e88 100644 --- a/ayan59dutta.ipynb +++ b/ayan59dutta.ipynb @@ -1,32 +1,276 @@ { - "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": "ayan59dutta.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/ayan59dutta/Assignment-2/blob/ayan59dutta/ayan59dutta.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": "uDnPKUobqeQv", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "from numpy.random import *\n", + "dummy_list = [round(x, randint(2, 4)) for x in list(random(randint(10)) * 100)]\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "cDjddNGfngnp", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "2) print dummy_list" + ] + }, + { + "metadata": { + "id": "nlssoShyvhm2", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "6b059a08-b4ff-4e35-cb68-c9e71a05f5d1" + }, + "cell_type": "code", + "source": [ + "print(dummy_list)" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[56.097, 55.994, 93.992, 39.443, 95.399, 77.68]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "15jKDXxkn16M", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "3) Reverse dummy_list and print" + ] + }, + { + "metadata": { + "id": "nbUemDWCw_R3", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "40fa4dec-a62a-454a-ca76-6defbe4c2564" + }, + "cell_type": "code", + "source": [ + "dummy_list.reverse()\n", + "print(dummy_list)" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[77.68, 95.399, 39.443, 93.992, 55.994, 56.097]\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": "f-EGppxlyIDO", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "94778734-fe42-4af2-bb51-9c585ddc0750" + }, + "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": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[77.68, 95.399, 39.443, 93.992, 55.994, 56.097, 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": "qK_vaWrS0ql9", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "dummy_dict = dict()\n", + "for a in dummy_list:\n", + " try:\n", + " dummy_dict[a] += 1\n", + " except:\n", + " dummy_dict[a] = 1" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "RgCYpFXGou6q", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "6) print dummy_dict" + ] + }, + { + "metadata": { + "id": "d2e-Rgole_et", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "31eaec4d-d6db-496c-f92f-a7d2469abe60" + }, + "cell_type": "code", + "source": [ + "print(dummy_dict)" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{77.68: 1, 95.399: 1, 39.443: 1, 93.992: 1, 55.994: 1, 56.097: 1, 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}\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": "FY-_3W39fedQ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "outputId": "cb8015f6-3202-454a-b1c1-aeeb15fe05c8" + }, + "cell_type": "code", + "source": [ + "#ascending order sort\n", + "dummy_list.sort()\n", + "print(dummy_list)\n", + "\n", + "#descending order sort\n", + "dummy_list.sort(reverse=True)\n", + "print(dummy_list)\n" + ], + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 1, 2, 4, 9.45, 12.01, 12.02, 16, 39.443, 45.67, 55.994, 56.097, 77.68, 90, 93.992, 95.399, 200]\n", + "[200, 95.399, 93.992, 90, 77.68, 56.097, 55.994, 45.67, 39.443, 16, 12.02, 12.01, 9.45, 4, 2, 1, 0]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From 93dc3f972c375c831a119172a5e6c6ab3265d4be Mon Sep 17 00:00:00 2001 From: Ayan Dutta Date: Wed, 26 Sep 2018 00:31:47 +0530 Subject: [PATCH 2/5] Updated problems solved as of now. --- ayan59dutta.ipynb | 435 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 414 insertions(+), 21 deletions(-) diff --git a/ayan59dutta.ipynb b/ayan59dutta.ipynb index b7b6e88..cc8ca4d 100644 --- a/ayan59dutta.ipynb +++ b/ayan59dutta.ipynb @@ -54,7 +54,10 @@ "cell_type": "code", "source": [ "from numpy.random import *\n", - "dummy_list = [round(x, randint(2, 4)) for x in list(random(randint(10)) * 100)]\n" + "while True:\n", + " dummy_list = [round(x, randint(2, 4)) for x in list(random(randint(10)) * 100)]\n", + " if len(dummy_list) > 0:\n", + " break" ], "execution_count": 0, "outputs": [] @@ -77,18 +80,18 @@ "base_uri": "https://localhost:8080/", "height": 35 }, - "outputId": "6b059a08-b4ff-4e35-cb68-c9e71a05f5d1" + "outputId": "844f6066-048e-42f4-e231-2c8a88bed718" }, "cell_type": "code", "source": [ "print(dummy_list)" ], - "execution_count": 13, + "execution_count": 51, "outputs": [ { "output_type": "stream", "text": [ - "[56.097, 55.994, 93.992, 39.443, 95.399, 77.68]\n" + "[24.993, 94.155, 58.713, 38.038, 53.08]\n" ], "name": "stdout" } @@ -112,19 +115,19 @@ "base_uri": "https://localhost:8080/", "height": 35 }, - "outputId": "40fa4dec-a62a-454a-ca76-6defbe4c2564" + "outputId": "37088e82-342e-4e6d-81f1-a5f84a7c9a62" }, "cell_type": "code", "source": [ "dummy_list.reverse()\n", "print(dummy_list)" ], - "execution_count": 14, + "execution_count": 52, "outputs": [ { "output_type": "stream", "text": [ - "[77.68, 95.399, 39.443, 93.992, 55.994, 56.097]\n" + "[53.08, 38.038, 58.713, 94.155, 24.993]\n" ], "name": "stdout" } @@ -148,7 +151,7 @@ "base_uri": "https://localhost:8080/", "height": 35 }, - "outputId": "94778734-fe42-4af2-bb51-9c585ddc0750" + "outputId": "ca8886cc-48fe-46d9-ac4e-50e3cc331594" }, "cell_type": "code", "source": [ @@ -156,12 +159,12 @@ "dummy_list = dummy_list + dummy_list_2\n", "print(dummy_list)" ], - "execution_count": 15, + "execution_count": 53, "outputs": [ { "output_type": "stream", "text": [ - "[77.68, 95.399, 39.443, 93.992, 55.994, 56.097, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + "[53.08, 38.038, 58.713, 94.155, 24.993, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" ], "name": "stdout" } @@ -213,18 +216,18 @@ "base_uri": "https://localhost:8080/", "height": 35 }, - "outputId": "31eaec4d-d6db-496c-f92f-a7d2469abe60" + "outputId": "46a713d7-1339-4641-b0c7-fa1bff5e01c3" }, "cell_type": "code", "source": [ "print(dummy_dict)" ], - "execution_count": 17, + "execution_count": 55, "outputs": [ { "output_type": "stream", "text": [ - "{77.68: 1, 95.399: 1, 39.443: 1, 93.992: 1, 55.994: 1, 56.097: 1, 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}\n" + "{53.08: 1, 38.038: 1, 58.713: 1, 94.155: 1, 24.993: 1, 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}\n" ], "name": "stdout" } @@ -246,31 +249,421 @@ "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 54 + "height": 35 }, - "outputId": "cb8015f6-3202-454a-b1c1-aeeb15fe05c8" + "outputId": "a0728b8b-909d-4f55-ae97-43369e9faab1" }, "cell_type": "code", "source": [ "#ascending order sort\n", "dummy_list.sort()\n", - "print(dummy_list)\n", - "\n", + "print(dummy_list)" + ], + "execution_count": 56, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 1, 2, 4, 9.45, 12.01, 12.02, 16, 24.993, 38.038, 45.67, 53.08, 58.713, 90, 94.155, 200]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "RE_6fFkE2R_a", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "97182b2c-d81b-41be-d429-882fd29aa89c" + }, + "cell_type": "code", + "source": [ "#descending order sort\n", "dummy_list.sort(reverse=True)\n", - "print(dummy_list)\n" + "print(dummy_list)" + ], + "execution_count": 57, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[200, 94.155, 90, 58.713, 53.08, 45.67, 38.038, 24.993, 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": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "dd9910d5-5ae3-47f8-a2ab-e2738e16cb30" + }, + "cell_type": "code", + "source": [ + "x = 200\n", + "dummy_list.remove(x)\n", + "print(dummy_list)" + ], + "execution_count": 58, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[94.155, 90, 58.713, 53.08, 45.67, 38.038, 24.993, 16, 12.02, 12.01, 9.45, 4, 2, 1, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "BhNU-PEL_PKS", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 191 + }, + "outputId": "9984f26c-66db-40d7-acd9-67c0b64737f5" + }, + "cell_type": "code", + "source": [ + "# Let's play: try the same with something which is not in the list to get the ValueError\n", + "x = -1\n", + "dummy_list.remove(x)" + ], + "execution_count": 59, + "outputs": [ + { + "output_type": "error", + "ename": "ValueError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" + ] + } + ] + }, + { + "metadata": { + "id": "QPB6iGbeqviN", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "9) Remove the item at position x. x is any random integer" + ] + }, + { + "metadata": { + "id": "yF__PKgn3Pjd", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "4c92fe66-ad91-42ee-98c1-6f9441e164b4" + }, + "cell_type": "code", + "source": [ + "x = randint(len(dummy_list))\n", + "del dummy_list[x]\n", + "print(x, dummy_list)" ], - "execution_count": 18, + "execution_count": 60, "outputs": [ { "output_type": "stream", "text": [ - "[0, 1, 2, 4, 9.45, 12.01, 12.02, 16, 39.443, 45.67, 55.994, 56.097, 77.68, 90, 93.992, 95.399, 200]\n", - "[200, 95.399, 93.992, 90, 77.68, 56.097, 55.994, 45.67, 39.443, 16, 12.02, 12.01, 9.45, 4, 2, 1, 0]\n" + "12 [94.155, 90, 58.713, 53.08, 45.67, 38.038, 24.993, 16, 12.02, 12.01, 9.45, 4, 1, 0]\n" ], "name": "stdout" } ] + }, + { + "metadata": { + "id": "clcbmQ-C_XoW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 191 + }, + "outputId": "0558ec7f-2d82-43af-e8e3-dbee5e0cf3fe" + }, + "cell_type": "code", + "source": [ + "# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get\n", + "x = len(dummy_list) + 1\n", + "del dummy_list[x]" + ], + "execution_count": 61, + "outputs": [ + { + "output_type": "error", + "ename": "IndexError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdummy_list\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0mdummy_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range" + ] + } + ] + }, + { + "metadata": { + "id": "bqQnnsr8rm6G", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "10) Let's clean everything clear the list and then print" + ] + }, + { + "metadata": { + "id": "vq2M7dzK--50", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "a_4UupTr9fbX", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Numpy Exercises" + ] + }, + { + "metadata": { + "id": "TSvU5zo0GU8x", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "import numpy as np #import numpy" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "dBoH_A7M9jjL", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "1) Create a uniform subdivision of the interval -1.3 to 2.5 with 64 subdivisions" + ] + }, + { + "metadata": { + "id": "GTUSJWuZCv0t", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "AHZIeHqyCaud", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "2) Generate an array of length 3n filled with the cyclic pattern 1, 2, 3" + ] + }, + { + "metadata": { + "id": "lUHIFXe1CxGF", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "Vh-UKizx9oTp", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "3) Create an array of the first 10 odd integers." + ] + }, + { + "metadata": { + "id": "2ALw66xXC1Aj", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "QfJRdMat90f4", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "4) Find intersection of a and b" + ] + }, + { + "metadata": { + "id": "kT5ZlkPnC34h", + "colab_type": "code", + "colab": {} + }, + "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])" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "RtVCf0UoCeB8", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "5) Reshape 1d array a to 2d array of 2X5" + ] + }, + { + "metadata": { + "id": "Ui9T269FDF4P", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "a = np.arange(10)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "dVrSBW1zEjp2", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "6) Create a numpy array to list and vice versa" + ] + }, + { + "metadata": { + "id": "EwO-PX4NDRMO", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]" + ], + "execution_count": 0, + "outputs": [] + }, + { + "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": "pm0652oxDRvk", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "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": "VPacmlprDShw", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] } ] } \ No newline at end of file From 910bfc355404797292fc3c4900023a535c4171fe Mon Sep 17 00:00:00 2001 From: Ayan Dutta Date: Wed, 26 Sep 2018 00:48:05 +0530 Subject: [PATCH 3/5] Completed Assignment 2 - Lists --- ayan59dutta.ipynb | 104 +++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 42 deletions(-) diff --git a/ayan59dutta.ipynb b/ayan59dutta.ipynb index cc8ca4d..34b2b8c 100644 --- a/ayan59dutta.ipynb +++ b/ayan59dutta.ipynb @@ -45,6 +45,19 @@ "1) Create any random list and assign it to a variable dummy_list" ] }, + { + "metadata": { + "id": "RoZl5UmPIu1q", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "from numpy.random import *" + ], + "execution_count": 0, + "outputs": [] + }, { "metadata": { "id": "uDnPKUobqeQv", @@ -53,7 +66,6 @@ }, "cell_type": "code", "source": [ - "from numpy.random import *\n", "while True:\n", " dummy_list = [round(x, randint(2, 4)) for x in list(random(randint(10)) * 100)]\n", " if len(dummy_list) > 0:\n", @@ -80,18 +92,18 @@ "base_uri": "https://localhost:8080/", "height": 35 }, - "outputId": "844f6066-048e-42f4-e231-2c8a88bed718" + "outputId": "788643bd-039c-481c-8e16-eaa5746f9ee7" }, "cell_type": "code", "source": [ "print(dummy_list)" ], - "execution_count": 51, + "execution_count": 3, "outputs": [ { "output_type": "stream", "text": [ - "[24.993, 94.155, 58.713, 38.038, 53.08]\n" + "[83.561, 88.577, 87.39, 9.12, 25.29, 90.656, 8.28]\n" ], "name": "stdout" } @@ -115,19 +127,19 @@ "base_uri": "https://localhost:8080/", "height": 35 }, - "outputId": "37088e82-342e-4e6d-81f1-a5f84a7c9a62" + "outputId": "1119e630-f103-420f-fea4-66f1c4025cdd" }, "cell_type": "code", "source": [ "dummy_list.reverse()\n", "print(dummy_list)" ], - "execution_count": 52, + "execution_count": 4, "outputs": [ { "output_type": "stream", "text": [ - "[53.08, 38.038, 58.713, 94.155, 24.993]\n" + "[8.28, 90.656, 25.29, 9.12, 87.39, 88.577, 83.561]\n" ], "name": "stdout" } @@ -151,7 +163,7 @@ "base_uri": "https://localhost:8080/", "height": 35 }, - "outputId": "ca8886cc-48fe-46d9-ac4e-50e3cc331594" + "outputId": "7205aaf9-3788-45d6-b86f-13c46c1a6d7e" }, "cell_type": "code", "source": [ @@ -159,12 +171,12 @@ "dummy_list = dummy_list + dummy_list_2\n", "print(dummy_list)" ], - "execution_count": 53, + "execution_count": 5, "outputs": [ { "output_type": "stream", "text": [ - "[53.08, 38.038, 58.713, 94.155, 24.993, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + "[8.28, 90.656, 25.29, 9.12, 87.39, 88.577, 83.561, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" ], "name": "stdout" } @@ -188,12 +200,7 @@ }, "cell_type": "code", "source": [ - "dummy_dict = dict()\n", - "for a in dummy_list:\n", - " try:\n", - " dummy_dict[a] += 1\n", - " except:\n", - " dummy_dict[a] = 1" + "dummy_dict = {element:dummy_list.count(element) for element in dummy_list}" ], "execution_count": 0, "outputs": [] @@ -214,20 +221,20 @@ "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 35 + "height": 55 }, - "outputId": "46a713d7-1339-4641-b0c7-fa1bff5e01c3" + "outputId": "11fb999e-d149-4bc5-9d85-0133101e22a8" }, "cell_type": "code", "source": [ "print(dummy_dict)" ], - "execution_count": 55, + "execution_count": 7, "outputs": [ { "output_type": "stream", "text": [ - "{53.08: 1, 38.038: 1, 58.713: 1, 94.155: 1, 24.993: 1, 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}\n" + "{8.28: 1, 90.656: 1, 25.29: 1, 9.12: 1, 87.39: 1, 88.577: 1, 83.561: 1, 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}\n" ], "name": "stdout" } @@ -251,7 +258,7 @@ "base_uri": "https://localhost:8080/", "height": 35 }, - "outputId": "a0728b8b-909d-4f55-ae97-43369e9faab1" + "outputId": "c774a924-0e71-47a7-9811-031c4976f23f" }, "cell_type": "code", "source": [ @@ -259,12 +266,12 @@ "dummy_list.sort()\n", "print(dummy_list)" ], - "execution_count": 56, + "execution_count": 8, "outputs": [ { "output_type": "stream", "text": [ - "[0, 1, 2, 4, 9.45, 12.01, 12.02, 16, 24.993, 38.038, 45.67, 53.08, 58.713, 90, 94.155, 200]\n" + "[0, 1, 2, 4, 8.28, 9.12, 9.45, 12.01, 12.02, 16, 25.29, 45.67, 83.561, 87.39, 88.577, 90, 90.656, 200]\n" ], "name": "stdout" } @@ -278,7 +285,7 @@ "base_uri": "https://localhost:8080/", "height": 35 }, - "outputId": "97182b2c-d81b-41be-d429-882fd29aa89c" + "outputId": "3d9e6329-9fac-4d5d-9fef-186be5b6e3bc" }, "cell_type": "code", "source": [ @@ -286,12 +293,12 @@ "dummy_list.sort(reverse=True)\n", "print(dummy_list)" ], - "execution_count": 57, + "execution_count": 9, "outputs": [ { "output_type": "stream", "text": [ - "[200, 94.155, 90, 58.713, 53.08, 45.67, 38.038, 24.993, 16, 12.02, 12.01, 9.45, 4, 2, 1, 0]\n" + "[200, 90.656, 90, 88.577, 87.39, 83.561, 45.67, 25.29, 16, 12.02, 12.01, 9.45, 9.12, 8.28, 4, 2, 1, 0]\n" ], "name": "stdout" } @@ -315,7 +322,7 @@ "base_uri": "https://localhost:8080/", "height": 35 }, - "outputId": "dd9910d5-5ae3-47f8-a2ab-e2738e16cb30" + "outputId": "924204c9-1e56-4b8a-a530-8664016b0ac8" }, "cell_type": "code", "source": [ @@ -323,12 +330,12 @@ "dummy_list.remove(x)\n", "print(dummy_list)" ], - "execution_count": 58, + "execution_count": 10, "outputs": [ { "output_type": "stream", "text": [ - "[94.155, 90, 58.713, 53.08, 45.67, 38.038, 24.993, 16, 12.02, 12.01, 9.45, 4, 2, 1, 0]\n" + "[90.656, 90, 88.577, 87.39, 83.561, 45.67, 25.29, 16, 12.02, 12.01, 9.45, 9.12, 8.28, 4, 2, 1, 0]\n" ], "name": "stdout" } @@ -342,7 +349,7 @@ "base_uri": "https://localhost:8080/", "height": 191 }, - "outputId": "9984f26c-66db-40d7-acd9-67c0b64737f5" + "outputId": "8d9f51c1-1f30-431c-d063-fb473aaa9f0b" }, "cell_type": "code", "source": [ @@ -350,7 +357,7 @@ "x = -1\n", "dummy_list.remove(x)" ], - "execution_count": 59, + "execution_count": 11, "outputs": [ { "output_type": "error", @@ -359,7 +366,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" ] } @@ -383,7 +390,7 @@ "base_uri": "https://localhost:8080/", "height": 35 }, - "outputId": "4c92fe66-ad91-42ee-98c1-6f9441e164b4" + "outputId": "94132ab0-ec28-410a-dbd8-e3b0a15d9768" }, "cell_type": "code", "source": [ @@ -391,12 +398,12 @@ "del dummy_list[x]\n", "print(x, dummy_list)" ], - "execution_count": 60, + "execution_count": 12, "outputs": [ { "output_type": "stream", "text": [ - "12 [94.155, 90, 58.713, 53.08, 45.67, 38.038, 24.993, 16, 12.02, 12.01, 9.45, 4, 1, 0]\n" + "1 [90.656, 88.577, 87.39, 83.561, 45.67, 25.29, 16, 12.02, 12.01, 9.45, 9.12, 8.28, 4, 2, 1, 0]\n" ], "name": "stdout" } @@ -410,7 +417,7 @@ "base_uri": "https://localhost:8080/", "height": 191 }, - "outputId": "0558ec7f-2d82-43af-e8e3-dbee5e0cf3fe" + "outputId": "c75cab6f-66ad-4d19-8f66-6e485fa5240e" }, "cell_type": "code", "source": [ @@ -418,7 +425,7 @@ "x = len(dummy_list) + 1\n", "del dummy_list[x]" ], - "execution_count": 61, + "execution_count": 13, "outputs": [ { "output_type": "error", @@ -427,7 +434,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdummy_list\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0mdummy_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdummy_list\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0mdummy_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range" ] } @@ -447,14 +454,27 @@ "metadata": { "id": "vq2M7dzK--50", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "a875d435-18b9-4e28-bb67-34089d03022c" }, "cell_type": "code", "source": [ - "" + "dummy_list.clear()\n", + "print(dummy_list)" ], - "execution_count": 0, - "outputs": [] + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[]\n" + ], + "name": "stdout" + } + ] }, { "metadata": { From f1c423d43223cf4ffc205339b6f3daf117b95318 Mon Sep 17 00:00:00 2001 From: Ayan Dutta Date: Thu, 27 Sep 2018 23:57:57 +0530 Subject: [PATCH 4/5] Completed Assignment-2. --- ayan59dutta.ipynb | 282 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 239 insertions(+), 43 deletions(-) diff --git a/ayan59dutta.ipynb b/ayan59dutta.ipynb index 34b2b8c..383d928 100644 --- a/ayan59dutta.ipynb +++ b/ayan59dutta.ipynb @@ -98,7 +98,7 @@ "source": [ "print(dummy_list)" ], - "execution_count": 3, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -134,7 +134,7 @@ "dummy_list.reverse()\n", "print(dummy_list)" ], - "execution_count": 4, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -171,7 +171,7 @@ "dummy_list = dummy_list + dummy_list_2\n", "print(dummy_list)" ], - "execution_count": 5, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -229,7 +229,7 @@ "source": [ "print(dummy_dict)" ], - "execution_count": 7, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -266,7 +266,7 @@ "dummy_list.sort()\n", "print(dummy_list)" ], - "execution_count": 8, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -293,7 +293,7 @@ "dummy_list.sort(reverse=True)\n", "print(dummy_list)" ], - "execution_count": 9, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -330,7 +330,7 @@ "dummy_list.remove(x)\n", "print(dummy_list)" ], - "execution_count": 10, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -357,7 +357,7 @@ "x = -1\n", "dummy_list.remove(x)" ], - "execution_count": 11, + "execution_count": 0, "outputs": [ { "output_type": "error", @@ -398,7 +398,7 @@ "del dummy_list[x]\n", "print(x, dummy_list)" ], - "execution_count": 12, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -425,7 +425,7 @@ "x = len(dummy_list) + 1\n", "del dummy_list[x]" ], - "execution_count": 13, + "execution_count": 0, "outputs": [ { "output_type": "error", @@ -465,7 +465,7 @@ "dummy_list.clear()\n", "print(dummy_list)" ], - "execution_count": 14, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -513,14 +513,43 @@ "metadata": { "id": "GTUSJWuZCv0t", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 256 + }, + "outputId": "fbb894c0-f2d4-47cd-8b9a-ada9614a8707" }, "cell_type": "code", "source": [ - "" + "np.linspace(-1.3, 2.5, num=64)" ], - "execution_count": 0, - "outputs": [] + "execution_count": 2, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([-1.3 , -1.23968254, -1.17936508, -1.11904762, -1.05873016,\n", + " -0.9984127 , -0.93809524, -0.87777778, -0.81746032, -0.75714286,\n", + " -0.6968254 , -0.63650794, -0.57619048, -0.51587302, -0.45555556,\n", + " -0.3952381 , -0.33492063, -0.27460317, -0.21428571, -0.15396825,\n", + " -0.09365079, -0.03333333, 0.02698413, 0.08730159, 0.14761905,\n", + " 0.20793651, 0.26825397, 0.32857143, 0.38888889, 0.44920635,\n", + " 0.50952381, 0.56984127, 0.63015873, 0.69047619, 0.75079365,\n", + " 0.81111111, 0.87142857, 0.93174603, 0.99206349, 1.05238095,\n", + " 1.11269841, 1.17301587, 1.23333333, 1.29365079, 1.35396825,\n", + " 1.41428571, 1.47460317, 1.53492063, 1.5952381 , 1.65555556,\n", + " 1.71587302, 1.77619048, 1.83650794, 1.8968254 , 1.95714286,\n", + " 2.01746032, 2.07777778, 2.13809524, 2.1984127 , 2.25873016,\n", + " 2.31904762, 2.37936508, 2.43968254, 2.5 ])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 2 + } + ] }, { "metadata": { @@ -536,14 +565,35 @@ "metadata": { "id": "lUHIFXe1CxGF", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "outputId": "9bd0c64d-8c5f-4505-9ac5-75a1ea730fed" }, "cell_type": "code", "source": [ - "" + "pattern = [1, 2, 3]\n", + "n = 1 + np.random.randint(10)\n", + "\n", + "np.tile(pattern, n)" ], - "execution_count": 0, - "outputs": [] + "execution_count": 5, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1,\n", + " 2, 3, 1, 2, 3, 1, 2, 3])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 5 + } + ] }, { "metadata": { @@ -559,14 +609,31 @@ "metadata": { "id": "2ALw66xXC1Aj", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "59b3f95d-9e14-4d9d-d186-e643a1b892e1" }, "cell_type": "code", "source": [ - "" + "np.arange(1, 20, 2)" ], - "execution_count": 0, - "outputs": [] + "execution_count": 6, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 6 + } + ] }, { "metadata": { @@ -582,16 +649,35 @@ "metadata": { "id": "kT5ZlkPnC34h", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "fac65bf4-ad59-4815-a4a2-cc75df1dcc6f" }, "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])" + "b = np.array([7,2,10,2,7,4,9,4,9,8])\n", + "\n", + "np.intersect1d(a, b)" ], - "execution_count": 0, - "outputs": [] + "execution_count": 7, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([2, 4])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 7 + } + ] }, { "metadata": { @@ -607,14 +693,35 @@ "metadata": { "id": "Ui9T269FDF4P", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "outputId": "2f0bda2a-1e97-434b-fd2d-d1d9689631cb" }, "cell_type": "code", "source": [ - "a = np.arange(10)" + "a = np.arange(10)\n", + "\n", + "a = a.reshape(2, 5)\n", + "a" ], - "execution_count": 0, - "outputs": [] + "execution_count": 8, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[0, 1, 2, 3, 4],\n", + " [5, 6, 7, 8, 9]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 8 + } + ] }, { "metadata": { @@ -630,14 +737,45 @@ "metadata": { "id": "EwO-PX4NDRMO", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 164 + }, + "outputId": "b35c4231-8888-4327-fea4-a05c6ee44b1c" }, "cell_type": "code", "source": [ - "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]" + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "\n", + "print(type(a), a, sep='\\n')\n", + "\n", + "print()\n", + "\n", + "np_arr = np.array(a)\n", + "print(type(np_arr), np_arr, sep='\\n')\n", + "\n", + "print()\n", + "\n", + "a = np_arr.tolist()\n", + "print(type(a), a, sep='\\n')" ], - "execution_count": 0, - "outputs": [] + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "\n", + "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "\n", + "\n", + "[1 2 3 4 5 6 7 8 9]\n", + "\n", + "\n", + "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n" + ], + "name": "stdout" + } + ] }, { "metadata": { @@ -653,14 +791,45 @@ "metadata": { "id": "pm0652oxDRvk", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 201 + }, + "outputId": "c9bf39b8-3066-478d-aa52-e563534ef71a" }, "cell_type": "code", "source": [ - "" + "g = np.zeros((10, 10))\n", + "g[0, :] = 1\n", + "g[:, 0] = 1\n", + "g[:, -1] = 1\n", + "g[-1, :] = 1\n", + "g" ], - "execution_count": 0, - "outputs": [] + "execution_count": 10, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[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.]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 10 + } + ] }, { "metadata": { @@ -676,14 +845,41 @@ "metadata": { "id": "VPacmlprDShw", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 164 + }, + "outputId": "fe4e9028-0d8e-4211-ce46-1e6d8b52e107" }, "cell_type": "code", "source": [ - "" + "h = np.zeros(shape=(8, 8))\n", + "h[::2, ::2] = 1\n", + "h[1::2, 1::2] = 1\n", + "h" ], - "execution_count": 0, - "outputs": [] + "execution_count": 11, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[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", + " [0., 1., 0., 1., 0., 1., 0., 1.]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 11 + } + ] } ] } \ No newline at end of file From be06d72309e0bad6531e11cb5cda9caf2afa7ae3 Mon Sep 17 00:00:00 2001 From: Ayan Dutta Date: Tue, 2 Oct 2018 15:57:29 +0530 Subject: [PATCH 5/5] Included Numpy Examples into Assignment - 2. --- ayan59dutta.ipynb | 512 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 504 insertions(+), 8 deletions(-) diff --git a/ayan59dutta.ipynb b/ayan59dutta.ipynb index 383d928..ce036e8 100644 --- a/ayan59dutta.ipynb +++ b/ayan59dutta.ipynb @@ -476,6 +476,502 @@ } ] }, + { + "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": "416bEqAn9sR-", + "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": "tkdxXsfqLxTX", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 90 + }, + "outputId": "19a9d17a-d522-4024-96b0-c266bf10eec9" + }, + "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": 4, + "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": "WuOE3x4UL3pL", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 237 + }, + "outputId": "5951dd7b-6003-4bec-de03-e91336c50762" + }, + "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": 6, + "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.43219251 0.94828845]\n", + " [0.6674261 0.35916053]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "8RPW_SutukjF", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Execute and understand :)" + ] + }, + { + "metadata": { + "id": "_nY5zTb2L4lv", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 202 + }, + "outputId": "d23fdb0d-877a-4cf3-b608-35a7dc3c2ba4" + }, + "cell_type": "code", + "source": [ + "a == np.arange(10)\n", + "b == np.linspace(0,10, num=6)\n", + "print(a)\n", + "print(b)" + ], + "execution_count": 7, + "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": "rxeJj7hvMBp7", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "outputId": "c3f998b3-abd9-4f46-9ffb-1d78ac0c1400" + }, + "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": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n", + "77\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "s400Gijxu0kO", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Slicing" + ] + }, + { + "metadata": { + "id": "ikbLrit9MGU3", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 127 + }, + "outputId": "074f5f9e-72d1-43d0-a80a-aea4a7eaa2aa" + }, + "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": 9, + "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": "0DxFeHk1MIz_", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + }, + "outputId": "146aa9a8-8742-4363-ca76-7ac0f595c2a4" + }, + "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": 10, + "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": "z_XhsMQmMQOf", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 72 + }, + "outputId": "da918701-cec8-4992-e9de-bca160750c93" + }, + "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": 11, + "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": "BLvkXVTVMRCT", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "edacc250-1296-4ff2-91b2-e66c3f499e2e" + }, + "cell_type": "code", + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "print(modified_b)" + ], + "execution_count": 12, + "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": "jFOCJBEAMXMP", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 127 + }, + "outputId": "2a512fca-be05-46a2-8a6d-0c96165dfb14" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[:, [1,0,2]])" + ], + "execution_count": 13, + "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": "OQ3ofr4mMa3c", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 127 + }, + "outputId": "ec3664ae-8bac-41cd-f85c-872b6acd39fc" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[[1,0,2], :])" + ], + "execution_count": 15, + "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", @@ -523,7 +1019,7 @@ "source": [ "np.linspace(-1.3, 2.5, num=64)" ], - "execution_count": 2, + "execution_count": 0, "outputs": [ { "output_type": "execute_result", @@ -578,7 +1074,7 @@ "\n", "np.tile(pattern, n)" ], - "execution_count": 5, + "execution_count": 0, "outputs": [ { "output_type": "execute_result", @@ -619,7 +1115,7 @@ "source": [ "np.arange(1, 20, 2)" ], - "execution_count": 6, + "execution_count": 0, "outputs": [ { "output_type": "execute_result", @@ -663,7 +1159,7 @@ "\n", "np.intersect1d(a, b)" ], - "execution_count": 7, + "execution_count": 0, "outputs": [ { "output_type": "execute_result", @@ -706,7 +1202,7 @@ "a = a.reshape(2, 5)\n", "a" ], - "execution_count": 8, + "execution_count": 0, "outputs": [ { "output_type": "execute_result", @@ -759,7 +1255,7 @@ "a = np_arr.tolist()\n", "print(type(a), a, sep='\\n')" ], - "execution_count": 9, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -806,7 +1302,7 @@ "g[-1, :] = 1\n", "g" ], - "execution_count": 10, + "execution_count": 0, "outputs": [ { "output_type": "execute_result", @@ -858,7 +1354,7 @@ "h[1::2, 1::2] = 1\n", "h" ], - "execution_count": 11, + "execution_count": 0, "outputs": [ { "output_type": "execute_result",