From 2b7c46cb2742ad2ec151b48dd561cac455bcc90c Mon Sep 17 00:00:00 2001 From: ASIF IQBAL <43201136+ASIF8240233397@users.noreply.github.com> Date: Tue, 25 Sep 2018 00:50:51 +0530 Subject: [PATCH 1/5] Created using Colaboratory --- ASIF8240233397.ipynb | 59 ++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/ASIF8240233397.ipynb b/ASIF8240233397.ipynb index 9e2543a..10aab98 100644 --- a/ASIF8240233397.ipynb +++ b/ASIF8240233397.ipynb @@ -1,32 +1,31 @@ { - "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": "ASIF8240233397.ipynb", + "version": "0.3.2", + "provenance": [] + }, + "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": [ + { + "metadata": { + "id": "H3RpbLQaBbhj", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From 790e2eb9b93a413d7f1e2f67a38f7aeda5399242 Mon Sep 17 00:00:00 2001 From: ASIF IQBAL <43201136+ASIF8240233397@users.noreply.github.com> Date: Thu, 27 Sep 2018 23:16:18 +0530 Subject: [PATCH 2/5] nympy cplt --- ASIF8240233397.ipynb | 252 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 251 insertions(+), 1 deletion(-) diff --git a/ASIF8240233397.ipynb b/ASIF8240233397.ipynb index 10aab98..1e661c0 100644 --- a/ASIF8240233397.ipynb +++ b/ASIF8240233397.ipynb @@ -5,7 +5,8 @@ "colab": { "name": "ASIF8240233397.ipynb", "version": "0.3.2", - "provenance": [] + "provenance": [], + "include_colab_link": true }, "kernelspec": { "display_name": "Python 3", @@ -14,6 +15,16 @@ } }, "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/ASIF8240233397/Assignment-2/blob/ASIF8240233397/ASIF8240233397.ipynb)" + ] + }, { "metadata": { "id": "H3RpbLQaBbhj", @@ -21,11 +32,250 @@ "colab": {} }, "cell_type": "code", + "source": [ + " import numpy as np" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "PFQbi5rDcQU6", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", "source": [ "" ], "execution_count": 0, "outputs": [] + }, + { + "metadata": { + "id": "hOBhal1JcQuM", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 238 + }, + "outputId": "86ee54ff-6ae1-44b4-da50-e6c02404c3bf" + }, + "cell_type": "code", + "source": [ + " \n", + "lower=int(input(\"Enter the lower limit for the range:\"))\n", + "upper=int(input(\"Enter the upper limit for the range:\"))\n", + "for i in range(lower,upper+1):\n", + " if(i%2!=0):\n", + " print(i)" + ], + "execution_count": 25, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter the lower limit for the range:1\n", + "Enter the upper limit for the range:21\n", + "1\n", + "3\n", + "5\n", + "7\n", + "9\n", + "11\n", + "13\n", + "15\n", + "17\n", + "19\n", + "21\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "RMcB9817hmf1", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "2158f67c-f6cd-424c-9c44-af0ed1624da3" + }, + "cell_type": "code", + "source": [ + "\n", + " \n", + "set1 = {2, 4, 5, 6} \n", + "set2 = {4, 6, 7, 8} \n", + "print(\"set1 intersection set2 : \", set1.intersection(set2)) \n", + " " + ], + "execution_count": 26, + "outputs": [ + { + "output_type": "stream", + "text": [ + "set1 intersection set2 : {4, 6}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "hEUbmuKeijC-", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "998caa12-769a-41a2-d58e-ffce14167ac8" + }, + "cell_type": "code", + "source": [ + "\n", + "# split input and output\n", + "from numpy import array\n", + "# define array\n", + "data = array([[11, 22, 33],\n", + "\t\t[44, 55, 66],\n", + "\t\t[77, 88, 99]])\n", + "# separate data\n", + "X, y = data[:, :-1], data[:, -1]\n", + "print(X)\n", + "print(y)" + ], + "execution_count": 32, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[11 22]\n", + " [44 55]\n", + " [77 88]]\n", + "[33 66 99]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "6Ntc9RDrcQwV", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "859a54b2-8d5f-4ffe-a02c-01eb045ab58c" + }, + "cell_type": "code", + "source": [ + "import numpy as np\n", + "import numpy as np\n", + "x = np.arange(12, 38)\n", + "print(\"Original array:\")\n", + "print(x)\n", + "print(\"Reverse array:\")\n", + "x = x[::-1]\n", + "print(x)" + ], + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Original array:\n", + "[12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35\n", + " 36 37]\n", + "Reverse array:\n", + "[37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14\n", + " 13 12]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "IN720_6vcQ0B", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 187 + }, + "outputId": "802c30de-2219-4973-a009-30a1f7509205" + }, + "cell_type": "code", + "source": [ + "import numpy as np\n", + "x = np.ones((3,3))\n", + "print(\"Original array:\")\n", + "print(x)\n", + "print(\"0 on the border and 1 inside in the array\")\n", + "x = np.pad(x, pad_width=1, mode='constant', constant_values=0)\n", + "print(x)" + ], + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Original array:\n", + "[[1. 1. 1.]\n", + " [1. 1. 1.]\n", + " [1. 1. 1.]]\n", + "0 on the border and 1 inside in the array\n", + "[[0. 0. 0. 0. 0.]\n", + " [0. 1. 1. 1. 0.]\n", + " [0. 1. 1. 1. 0.]\n", + " [0. 1. 1. 1. 0.]\n", + " [0. 0. 0. 0. 0.]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "MMKMWAvtZIyA", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 170 + }, + "outputId": "40baa00a-d614-4263-fe90-7273c8a31434" + }, + "cell_type": "code", + "source": [ + "import numpy as np\n", + "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)\n" + ], + "execution_count": 9, + "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 From c66ce8492d4de718b6b310da243dd1f4390fffd9 Mon Sep 17 00:00:00 2001 From: ASIF IQBAL <43201136+ASIF8240233397@users.noreply.github.com> Date: Fri, 28 Sep 2018 22:46:41 +0530 Subject: [PATCH 3/5] i completed week 2 list numpy and numpy exercise --- ASIF8240233397.ipynb | 586 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 552 insertions(+), 34 deletions(-) diff --git a/ASIF8240233397.ipynb b/ASIF8240233397.ipynb index 1e661c0..5fa8ef1 100644 --- a/ASIF8240233397.ipynb +++ b/ASIF8240233397.ipynb @@ -33,23 +33,534 @@ }, "cell_type": "code", "source": [ - " import numpy as np" + "import numpy as np" ], "execution_count": 0, "outputs": [] }, + { + "metadata": { + "id": "w24qqVoFIXlj", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "90336579-ed81-4eaf-9a7c-ac502a023abb" + }, + "cell_type": "code", + "source": [ + "dummy_list_1=[12, 4, 5, 6, 7, 8, 9, 10, 45, 98, 14]\n", + "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "print (dummy_list_1)\n", + "print (dummy_list_2)" + ], + "execution_count": 28, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[12, 4, 5, 6, 7, 8, 9, 10, 45, 98, 14]\n", + "[2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "PH6T1ERyJzfT", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "ecf831d6-2453-4d53-892f-e56ce1a35fea" + }, + "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_1=[12, 4, 5, 6, 7, 8, 9, 10, 45, 98, 14]\n", + "dummy_list=dummy_list_1+dummy_list_2\n", + "print (dummy_list)\n" + ], + "execution_count": 29, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[12, 4, 5, 6, 7, 8, 9, 10, 45, 98, 14, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "hQzcX1f9KWAp", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "8f77a867-2842-4622-e0c7-0edec2635b84" + }, + "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_1=[12, 4, 5, 6, 7, 8, 9, 10, 45, 98, 14]\n", + "dummy_list=dummy_list_1+dummy_list_2\n", + "dummy_list.sort()\n", + "print (dummy_list)" + ], + "execution_count": 32, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 1, 2, 4, 4, 5, 6, 7, 8, 9, 9.45, 10, 12, 12.01, 12.02, 14, 16, 45, 45.67, 90, 98, 200]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "xjwmHEphKxyn", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "4855a342-8b2a-47cc-d066-0f661072e5e4" + }, + "cell_type": "code", + "source": [ + "import numpy as np\n", + "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "dummy_list_1=[12, 4, 5, 6, 7, 8, 9, 10, 45, 98, 14]\n", + "dummy_list=dummy_list_1+dummy_list_2\n", + "dummy_list.sort()\n", + "dummy_list.pop()\n", + "print (dummy_list)" + ], + "execution_count": 35, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 1, 2, 4, 4, 5, 6, 7, 8, 9, 9.45, 10, 12, 12.01, 12.02, 14, 16, 45, 45.67, 90, 98]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "CK4IwoIAEAuh", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "60b827e5-ffab-4c5b-9488-6f5413b226d7" + }, + "cell_type": "code", + "source": [ + "a = np.array([1, 2, 3])\n", + "print(a)\n", + "b = np.array([[1,2,3],[4,5,6]])\n", + "print(b.shape) \n", + "print(b[0, 0], b[0, 1], b[1, 0])" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3]\n", + "(2, 3)\n", + "1 2 4\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "iCsX5yPoEtsD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + }, + "outputId": "b6eefa82-e26b-4dea-9966-d0fafb95f55d" + }, + "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)\n", + "print(b)\n", + "print(c)\n", + "print(d)\n", + "print(e)" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0. 0.]\n", + " [0. 0.]]\n", + "[[1. 1. 1.]\n", + " [1. 1. 1.]\n", + " [1. 1. 1.]]\n", + "[[1. 0.]\n", + " [0. 1.]]\n", + "[[5 5 5]\n", + " [5 5 5]\n", + " [5 5 5]]\n", + "[[0.80700247 0.49397538]\n", + " [0.08392541 0.02261966]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "l7pqcM4zFDJb", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "21e25aec-e006-4bd7-a486-8ef7e1729398" + }, + "cell_type": "code", + "source": [ + "a = np.arange(10)\n", + "b = np.linspace(0,10, num=6)\n", + "print(a)\n", + "print(b)" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0 1 2 3 4 5 6 7 8 9]\n", + "[ 0. 2. 4. 6. 8. 10.]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "sG7TXomwFRHF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "86709c87-09f5-4170-89c3-4e0fa3b620a5" + }, + "cell_type": "code", + "source": [ + "a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n", + "b = a[:2, 1:3]\n", + "print(a[0, 1]) \n", + "\n", + "b[0, 0] = 77 \n", + "print(a[0, 1])" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n", + "77\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "xNgQQeznFnwC", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "8255cffb-c4f1-40b9-eff3-092975669a11" + }, + "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, :] \n", + "row_r2 = a[1:2, :] \n", + "print(row_r1, row_r1.shape) \n", + "print(row_r2, row_r2.shape) \n", + "\n", + "col_r1 = a[:, 1]\n", + "col_r2 = a[:, 1:2]\n", + "\n", + "print(col_r1, col_r1.shape) \n", + "print(col_r2, col_r2.shape)" + ], + "execution_count": 16, + "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": "oc5l805AF6jT", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "b7e811bd-5445-4172-a600-b122dc2e537f" + }, + "cell_type": "code", + "source": [ + "x = np.array([[1,2],[3,4]])\n", + "\n", + "print(np.sum(x)) \n", + "print(np.sum(x, axis=0)) \n", + "print(np.sum(x, axis=1)) " + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10\n", + "[4 6]\n", + "[3 7]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "trevoygOGPN-", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "abfee82b-6ac2-476c-e97a-4b7ee34471b7" + }, + "cell_type": "code", + "source": [ + "b = np.arange(10)\n", + "\n", + "print(b)\n", + "\n", + "mask = b%2!=0 \n", + "print(mask)\n", + "print(b[mask])\n" + ], + "execution_count": 18, + "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": "ZRLuYwiyGbdj", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "ebd8bfc0-6ed5-402d-e2a1-6123edaa487b" + }, + "cell_type": "code", + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "print(modified_b)" + ], + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 0 -1 2 -1 4 -1 6 -1 8 -1]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "JFDUz8szGgCa", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "83b676fe-7fe5-4b7c-b67c-fca5e30a0176" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "print(a[:, [1,0,2]])" + ], + "execution_count": 20, + "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": "YDSnIObaGlkW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "1a87d729-a84a-4d8f-dad0-d6de187e0f13" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "print([[1,0,2]])" + ], + "execution_count": 25, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0 1 2]\n", + " [3 4 5]\n", + " [6 7 8]]\n", + "[[1, 0, 2]]\n" + ], + "name": "stdout" + } + ] + }, { "metadata": { "id": "PFQbi5rDcQU6", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 71 + }, + "outputId": "5cacf573-b004-4703-ba88-d251c49759ef" }, "cell_type": "code", "source": [ - "" + "array1=[]\n", + "diff=(2.5-1.3)/64\n", + "for i in range(64):\n", + " array1.append(1.3+(i*diff))\n", + "print(array1)\n", + "print(len(array1))\n" ], - "execution_count": 0, - "outputs": [] + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1.3, 1.31875, 1.3375000000000001, 1.35625, 1.375, 1.39375, 1.4125, 1.4312500000000001, 1.45, 1.46875, 1.4875, 1.50625, 1.525, 1.54375, 1.5625, 1.58125, 1.6, 1.61875, 1.6375, 1.65625, 1.675, 1.69375, 1.7125, 1.73125, 1.75, 1.76875, 1.7875, 1.80625, 1.8250000000000002, 1.84375, 1.8625, 1.88125, 1.9, 1.9187500000000002, 1.9375, 1.95625, 1.975, 1.99375, 2.0125, 2.03125, 2.05, 2.06875, 2.0875, 2.10625, 2.125, 2.14375, 2.1625, 2.18125, 2.2, 2.21875, 2.2375, 2.25625, 2.275, 2.29375, 2.3125, 2.33125, 2.35, 2.36875, 2.3875, 2.40625, 2.425, 2.44375, 2.4625, 2.48125]\n", + "64\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "eKuxXhmC-Iju", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 153 + }, + "outputId": "c0421875-049d-4dbd-fc8c-645576a076ee" + }, + "cell_type": "code", + "source": [ + "import numpy as np\n", + "n=input(\"Enter a no.: \")\n", + "array1=np.arange(1,3*int(n)+1)\n", + "print (array1)\n", + "for j in range(len(array1)):\n", + " \n", + " c=array1[0]\n", + "\n", + " for i in range(len(array1)-1):\n", + " \n", + " array1[i]=array1[i+1]\n", + "\n", + " array1[len(array1)-1]=c\n", + " print (array1)" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a no.: 2\n", + "[1 2 3 4 5 6]\n", + "[2 3 4 5 6 1]\n", + "[3 4 5 6 1 2]\n", + "[4 5 6 1 2 3]\n", + "[5 6 1 2 3 4]\n", + "[6 1 2 3 4 5]\n", + "[1 2 3 4 5 6]\n" + ], + "name": "stdout" + } + ] }, { "metadata": { @@ -59,18 +570,17 @@ "base_uri": "https://localhost:8080/", "height": 238 }, - "outputId": "86ee54ff-6ae1-44b4-da50-e6c02404c3bf" + "outputId": "d7d003b2-6fb4-4406-b618-67d0cc63dcd5" }, "cell_type": "code", "source": [ - " \n", "lower=int(input(\"Enter the lower limit for the range:\"))\n", "upper=int(input(\"Enter the upper limit for the range:\"))\n", "for i in range(lower,upper+1):\n", " if(i%2!=0):\n", " print(i)" ], - "execution_count": 25, + "execution_count": 4, "outputs": [ { "output_type": "stream", @@ -101,18 +611,16 @@ "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "2158f67c-f6cd-424c-9c44-af0ed1624da3" + "outputId": "de465d47-07bd-4170-c4f5-62a988935f1a" }, "cell_type": "code", "source": [ - "\n", - " \n", "set1 = {2, 4, 5, 6} \n", "set2 = {4, 6, 7, 8} \n", "print(\"set1 intersection set2 : \", set1.intersection(set2)) \n", " " ], - "execution_count": 26, + "execution_count": 5, "outputs": [ { "output_type": "stream", @@ -131,23 +639,19 @@ "base_uri": "https://localhost:8080/", "height": 85 }, - "outputId": "998caa12-769a-41a2-d58e-ffce14167ac8" + "outputId": "8e684fe3-c8e2-40d1-d805-b9612903bf67" }, "cell_type": "code", "source": [ - "\n", - "# split input and output\n", "from numpy import array\n", - "# define array\n", "data = array([[11, 22, 33],\n", "\t\t[44, 55, 66],\n", "\t\t[77, 88, 99]])\n", - "# separate data\n", "X, y = data[:, :-1], data[:, -1]\n", "print(X)\n", "print(y)" ], - "execution_count": 32, + "execution_count": 6, "outputs": [ { "output_type": "stream", @@ -182,7 +686,7 @@ "x = x[::-1]\n", "print(x)" ], - "execution_count": 19, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -204,35 +708,49 @@ "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 187 + "height": 425 }, - "outputId": "802c30de-2219-4973-a009-30a1f7509205" + "outputId": "f3423aaa-4783-4cda-8602-395aa5bf92c5" }, "cell_type": "code", "source": [ "import numpy as np\n", - "x = np.ones((3,3))\n", + "x = np.zeros((10,10))\n", "print(\"Original array:\")\n", "print(x)\n", - "print(\"0 on the border and 1 inside in the array\")\n", - "x = np.pad(x, pad_width=1, mode='constant', constant_values=0)\n", + "print(\"1 on the border and 0 inside in the array\")\n", + "x = np.pad(x, pad_width=1, mode='constant', constant_values=1)\n", "print(x)" ], - "execution_count": 18, + "execution_count": 8, "outputs": [ { "output_type": "stream", "text": [ "Original array:\n", - "[[1. 1. 1.]\n", - " [1. 1. 1.]\n", - " [1. 1. 1.]]\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", "0 on the border and 1 inside in the array\n", - "[[0. 0. 0. 0. 0.]\n", - " [0. 1. 1. 1. 0.]\n", - " [0. 1. 1. 1. 0.]\n", - " [0. 1. 1. 1. 0.]\n", - " [0. 0. 0. 0. 0.]]\n" + "[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]\n" ], "name": "stdout" } @@ -258,7 +776,7 @@ "x[::2,1::2] = 1\n", "print(x)\n" ], - "execution_count": 9, + "execution_count": 0, "outputs": [ { "output_type": "stream", From 4b44e73f843e08dcce69b13a4e5c3d508c2da4aa Mon Sep 17 00:00:00 2001 From: ASIF IQBAL <43201136+ASIF8240233397@users.noreply.github.com> Date: Sun, 30 Sep 2018 10:38:41 +0530 Subject: [PATCH 4/5] I SUBMITTED WEEK 2 ALL 3 ASSIGNMENTS --- ASIF8240233397.ipynb | 83 ++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 29 deletions(-) diff --git a/ASIF8240233397.ipynb b/ASIF8240233397.ipynb index 5fa8ef1..3d28de0 100644 --- a/ASIF8240233397.ipynb +++ b/ASIF8240233397.ipynb @@ -33,7 +33,10 @@ }, "cell_type": "code", "source": [ - "import numpy as np" + "import random\n", + "dummy_list=[]\n", + "for i in range(10):\n", + " dummy_list.append(random.randrange(1,100))" ], "execution_count": 0, "outputs": [] @@ -44,24 +47,46 @@ "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 51 + "height": 34 }, - "outputId": "90336579-ed81-4eaf-9a7c-ac502a023abb" + "outputId": "7692861e-c9a3-4964-fbe0-d0a03ff318f1" }, "cell_type": "code", "source": [ - "dummy_list_1=[12, 4, 5, 6, 7, 8, 9, 10, 45, 98, 14]\n", - "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", - "print (dummy_list_1)\n", - "print (dummy_list_2)" + "print(dummy_list)" ], - "execution_count": 28, + "execution_count": 2, "outputs": [ { "output_type": "stream", "text": [ - "[12, 4, 5, 6, 7, 8, 9, 10, 45, 98, 14]\n", - "[2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + "[73, 57, 31, 43, 75, 6, 84, 15, 34, 67]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "aq554l4s39gD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "2f0a62b8-d8f6-4b6c-dee2-55d60757974f" + }, + "cell_type": "code", + "source": [ + "dummy_list.reverse()\n", + "print(dummy_list)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[67, 34, 15, 84, 6, 75, 43, 31, 57, 73]\n" ], "name": "stdout" } @@ -84,7 +109,7 @@ "dummy_list=dummy_list_1+dummy_list_2\n", "print (dummy_list)\n" ], - "execution_count": 29, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -113,7 +138,7 @@ "dummy_list.sort()\n", "print (dummy_list)" ], - "execution_count": 32, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -144,7 +169,7 @@ "dummy_list.pop()\n", "print (dummy_list)" ], - "execution_count": 35, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -173,7 +198,7 @@ "print(b.shape) \n", "print(b[0, 0], b[0, 1], b[1, 0])" ], - "execution_count": 10, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -210,7 +235,7 @@ "print(d)\n", "print(e)" ], - "execution_count": 11, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -249,7 +274,7 @@ "print(a)\n", "print(b)" ], - "execution_count": 13, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -280,7 +305,7 @@ "b[0, 0] = 77 \n", "print(a[0, 1])" ], - "execution_count": 15, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -317,7 +342,7 @@ "print(col_r1, col_r1.shape) \n", "print(col_r2, col_r2.shape)" ], - "execution_count": 16, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -351,7 +376,7 @@ "print(np.sum(x, axis=0)) \n", "print(np.sum(x, axis=1)) " ], - "execution_count": 17, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -384,7 +409,7 @@ "print(mask)\n", "print(b[mask])\n" ], - "execution_count": 18, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -413,7 +438,7 @@ "modified_b[mask] = -1\n", "print(modified_b)" ], - "execution_count": 19, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -440,7 +465,7 @@ "print(a)\n", "print(a[:, [1,0,2]])" ], - "execution_count": 20, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -472,7 +497,7 @@ "print(a)\n", "print([[1,0,2]])" ], - "execution_count": 25, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -505,7 +530,7 @@ "print(array1)\n", "print(len(array1))\n" ], - "execution_count": 3, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -544,7 +569,7 @@ " array1[len(array1)-1]=c\n", " print (array1)" ], - "execution_count": 2, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -580,7 +605,7 @@ " if(i%2!=0):\n", " print(i)" ], - "execution_count": 4, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -620,7 +645,7 @@ "print(\"set1 intersection set2 : \", set1.intersection(set2)) \n", " " ], - "execution_count": 5, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -651,7 +676,7 @@ "print(X)\n", "print(y)" ], - "execution_count": 6, + "execution_count": 0, "outputs": [ { "output_type": "stream", @@ -722,7 +747,7 @@ "x = np.pad(x, pad_width=1, mode='constant', constant_values=1)\n", "print(x)" ], - "execution_count": 8, + "execution_count": 0, "outputs": [ { "output_type": "stream", From 728270588de2489b8e8f48decfedb0be0114f033 Mon Sep 17 00:00:00 2001 From: ASIF IQBAL <43201136+ASIF8240233397@users.noreply.github.com> Date: Sat, 6 Oct 2018 20:31:17 +0530 Subject: [PATCH 5/5] i completed week 2 hurrah!!!!!!!!!!!!!!!!!!11 --- ASIF8240233397.ipynb | 905 +++++++++++++++++++++++++++---------------- 1 file changed, 568 insertions(+), 337 deletions(-) diff --git a/ASIF8240233397.ipynb b/ASIF8240233397.ipynb index 3d28de0..d97eb82 100644 --- a/ASIF8240233397.ipynb +++ b/ASIF8240233397.ipynb @@ -33,10 +33,20 @@ }, "cell_type": "code", "source": [ - "import random\n", - "dummy_list=[]\n", - "for i in range(10):\n", - " dummy_list.append(random.randrange(1,100))" + "import random" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "04BUY2MZ2xro", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "dummy_list = [random.randrange(10) for i in range(11)]" ], "execution_count": 0, "outputs": [] @@ -49,18 +59,18 @@ "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "7692861e-c9a3-4964-fbe0-d0a03ff318f1" + "outputId": "eea0a5cb-0319-411f-9cb5-6ddd5c934882" }, "cell_type": "code", "source": [ "print(dummy_list)" ], - "execution_count": 2, + "execution_count": 3, "outputs": [ { "output_type": "stream", "text": [ - "[73, 57, 31, 43, 75, 6, 84, 15, 34, 67]\n" + "[8, 4, 0, 2, 4, 2, 1, 9, 7, 9, 3]\n" ], "name": "stdout" } @@ -74,19 +84,18 @@ "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "2f0a62b8-d8f6-4b6c-dee2-55d60757974f" + "outputId": "a6bf55ee-c501-4a7d-d9ce-24095211d5a4" }, "cell_type": "code", "source": [ - "dummy_list.reverse()\n", - "print(dummy_list)" + "print(list(reversed(dummy_list)))" ], - "execution_count": 3, + "execution_count": 4, "outputs": [ { "output_type": "stream", "text": [ - "[67, 34, 15, 84, 6, 75, 43, 31, 57, 73]\n" + "[3, 9, 7, 9, 1, 2, 4, 2, 0, 4, 8]\n" ], "name": "stdout" } @@ -96,25 +105,37 @@ "metadata": { "id": "PH6T1ERyJzfT", "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "hQzcX1f9KWAp", + "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "ecf831d6-2453-4d53-892f-e56ce1a35fea" + "outputId": "1db8ef7e-f125-4e1f-e0be-15efd639fa7e" }, "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_1=[12, 4, 5, 6, 7, 8, 9, 10, 45, 98, 14]\n", - "dummy_list=dummy_list_1+dummy_list_2\n", - "print (dummy_list)\n" + "#Adding (element wise addition) with original dummy_list (not reversed)\n", + "dummy_list = [a+b for a,b in zip(dummy_list,dummy_list_2)]\n", + "print(dummy_list)" ], - "execution_count": 0, + "execution_count": 6, "outputs": [ { "output_type": "stream", "text": [ - "[12, 4, 5, 6, 7, 8, 9, 10, 45, 98, 14, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + "[10, 204, 16, 6, 5, 2, 10.45, 54.67, 97, 21.009999999999998, 15.02]\n" ], "name": "stdout" } @@ -122,59 +143,55 @@ }, { "metadata": { - "id": "hQzcX1f9KWAp", + "id": "xjwmHEphKxyn", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "8f77a867-2842-4622-e0c7-0edec2635b84" + "outputId": "838809c5-dda6-43e3-91e6-3e049c93f9aa" }, "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_1=[12, 4, 5, 6, 7, 8, 9, 10, 45, 98, 14]\n", - "dummy_list=dummy_list_1+dummy_list_2\n", - "dummy_list.sort()\n", - "print (dummy_list)" + "dummy_dict = {}\n", + "[dummy_dict.update({i:dummy_list.count(i)}) for i in dummy_list]" ], - "execution_count": 0, + "execution_count": 7, "outputs": [ { - "output_type": "stream", - "text": [ - "[0, 1, 2, 4, 4, 5, 6, 7, 8, 9, 9.45, 10, 12, 12.01, 12.02, 14, 16, 45, 45.67, 90, 98, 200]\n" - ], - "name": "stdout" + "output_type": "execute_result", + "data": { + "text/plain": [ + "[None, None, None, None, None, None, None, None, None, None, None]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 7 } ] }, { "metadata": { - "id": "xjwmHEphKxyn", + "id": "CK4IwoIAEAuh", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "4855a342-8b2a-47cc-d066-0f661072e5e4" + "outputId": "84ffb0dc-8da7-454c-9657-1b888c5d4ce8" }, "cell_type": "code", "source": [ - "import numpy as np\n", - "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", - "dummy_list_1=[12, 4, 5, 6, 7, 8, 9, 10, 45, 98, 14]\n", - "dummy_list=dummy_list_1+dummy_list_2\n", - "dummy_list.sort()\n", - "dummy_list.pop()\n", - "print (dummy_list)" + "print(dummy_dict)" ], - "execution_count": 0, + "execution_count": 8, "outputs": [ { "output_type": "stream", "text": [ - "[0, 1, 2, 4, 4, 5, 6, 7, 8, 9, 9.45, 10, 12, 12.01, 12.02, 14, 16, 45, 45.67, 90, 98]\n" + "{10: 1, 204: 1, 16: 1, 6: 1, 5: 1, 2: 1, 10.45: 1, 54.67: 1, 97: 1, 21.009999999999998: 1, 15.02: 1}\n" ], "name": "stdout" } @@ -182,30 +199,28 @@ }, { "metadata": { - "id": "CK4IwoIAEAuh", + "id": "iCsX5yPoEtsD", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 68 + "height": 51 }, - "outputId": "60b827e5-ffab-4c5b-9488-6f5413b226d7" + "outputId": "c276a0a2-eae2-4a19-c962-42b8be8d7158" }, "cell_type": "code", "source": [ - "a = np.array([1, 2, 3])\n", - "print(a)\n", - "b = np.array([[1,2,3],[4,5,6]])\n", - "print(b.shape) \n", - "print(b[0, 0], b[0, 1], b[1, 0])" + "dummy_list_asc = sorted(dummy_list)\n", + "print(dummy_list_asc)\n", + "dummy_list_desc = sorted(dummy_list, reverse=True)\n", + "print(dummy_list_desc)" ], - "execution_count": 0, + "execution_count": 9, "outputs": [ { "output_type": "stream", "text": [ - "[1 2 3]\n", - "(2, 3)\n", - "1 2 4\n" + "[2, 5, 6, 10, 10.45, 15.02, 16, 21.009999999999998, 54.67, 97, 204]\n", + "[204, 97, 54.67, 21.009999999999998, 16, 15.02, 10.45, 10, 6, 5, 2]\n" ], "name": "stdout" } @@ -213,45 +228,24 @@ }, { "metadata": { - "id": "iCsX5yPoEtsD", + "id": "l7pqcM4zFDJb", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 221 + "height": 34 }, - "outputId": "b6eefa82-e26b-4dea-9966-d0fafb95f55d" + "outputId": "82e55244-4890-485e-a3e4-db25b43ab801" }, "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)\n", - "print(b)\n", - "print(c)\n", - "print(d)\n", - "print(e)" + "print('Before',dummy_list)" ], - "execution_count": 0, + "execution_count": 10, "outputs": [ { "output_type": "stream", "text": [ - "[[0. 0.]\n", - " [0. 0.]]\n", - "[[1. 1. 1.]\n", - " [1. 1. 1.]\n", - " [1. 1. 1.]]\n", - "[[1. 0.]\n", - " [0. 1.]]\n", - "[[5 5 5]\n", - " [5 5 5]\n", - " [5 5 5]]\n", - "[[0.80700247 0.49397538]\n", - " [0.08392541 0.02261966]]\n" + "Before [10, 204, 16, 6, 5, 2, 10.45, 54.67, 97, 21.009999999999998, 15.02]\n" ], "name": "stdout" } @@ -259,28 +253,60 @@ }, { "metadata": { - "id": "l7pqcM4zFDJb", + "id": "sG7TXomwFRHF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 197 + }, + "outputId": "2b6c8565-012e-4dc6-9b63-b15514603505" + }, + "cell_type": "code", + "source": [ + "x = 200\n", + "# Let's play: try the same with something which is not in the list to get the ValueError\n", + "dummy_list.remove(x)" + ], + "execution_count": 13, + "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;36m200\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# Let's play: try the same with something which is not in the list to get the ValueError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\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": "xNgQQeznFnwC", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, - "outputId": "21e25aec-e006-4bd7-a486-8ef7e1729398" + "outputId": "03160024-91ff-4981-a1b3-d7cc804296f4" }, "cell_type": "code", "source": [ - "a = np.arange(10)\n", - "b = np.linspace(0,10, num=6)\n", - "print(a)\n", - "print(b)" + "\n", + "print('Before',dummy_list)\n", + "x=2\n", + "dummy_list.pop(x)\n", + "print('After',dummy_list)" ], - "execution_count": 0, + "execution_count": 14, "outputs": [ { "output_type": "stream", "text": [ - "[0 1 2 3 4 5 6 7 8 9]\n", - "[ 0. 2. 4. 6. 8. 10.]\n" + "Before [10, 204, 16, 6, 5, 2, 10.45, 54.67, 97, 21.009999999999998, 15.02]\n", + "After [10, 204, 6, 5, 2, 10.45, 54.67, 97, 21.009999999999998, 15.02]\n" ], "name": "stdout" } @@ -288,30 +314,59 @@ }, { "metadata": { - "id": "sG7TXomwFRHF", + "id": "oc5l805AF6jT", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 197 + }, + "outputId": "539ed97e-569a-41c9-8d92-2455907bde0a" + }, + "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", + "dummy_list.pop(x)\n", + "print(dummy_list)" + ], + "execution_count": 15, + "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[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\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[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdummy_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mIndexError\u001b[0m: pop index out of range" + ] + } + ] + }, + { + "metadata": { + "id": "trevoygOGPN-", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, - "outputId": "86709c87-09f5-4170-89c3-4e0fa3b620a5" + "outputId": "080f55b3-3451-4470-8bd6-5be205c17d38" }, "cell_type": "code", "source": [ - "a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n", - "b = a[:2, 1:3]\n", - "print(a[0, 1]) \n", - "\n", - "b[0, 0] = 77 \n", - "print(a[0, 1])" + "print('Before',dummy_list)\n", + "dummy_list.clear()\n", + "print('After',dummy_list)" ], - "execution_count": 0, + "execution_count": 16, "outputs": [ { "output_type": "stream", "text": [ - "2\n", - "77\n" + "Before [10, 204, 6, 5, 2, 10.45, 54.67, 97, 21.009999999999998, 15.02]\n", + "After []\n" ], "name": "stdout" } @@ -319,40 +374,41 @@ }, { "metadata": { - "id": "xNgQQeznFnwC", + "id": "ZRLuYwiyGbdj", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "\n", + "import numpy as np #import numpy" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "JFDUz8szGgCa", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 119 + "height": 51 }, - "outputId": "8255cffb-c4f1-40b9-eff3-092975669a11" + "outputId": "c2b8af30-80a7-483d-e647-89306c266faf" }, "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, :] \n", - "row_r2 = a[1:2, :] \n", - "print(row_r1, row_r1.shape) \n", - "print(row_r2, row_r2.shape) \n", - "\n", - "col_r1 = a[:, 1]\n", - "col_r2 = a[:, 1:2]\n", - "\n", - "print(col_r1, col_r1.shape) \n", - "print(col_r2, col_r2.shape)" + "n = int(input(\"n: \"))\n", + "arr = np.tile([1,2,3],3)\n", + "print(arr)" ], - "execution_count": 0, + "execution_count": 18, "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" + "n: 3\n", + "[1 2 3 1 2 3 1 2 3]\n" ], "name": "stdout" } @@ -360,30 +416,25 @@ }, { "metadata": { - "id": "oc5l805AF6jT", + "id": "YDSnIObaGlkW", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 68 + "height": 34 }, - "outputId": "b7e811bd-5445-4172-a600-b122dc2e537f" + "outputId": "db37885d-8037-4fb1-8135-a904026aed33" }, "cell_type": "code", "source": [ - "x = np.array([[1,2],[3,4]])\n", - "\n", - "print(np.sum(x)) \n", - "print(np.sum(x, axis=0)) \n", - "print(np.sum(x, axis=1)) " + "arr = list(range(1,2*10,2))\n", + "print(arr, len(arr))" ], - "execution_count": 0, + "execution_count": 19, "outputs": [ { "output_type": "stream", "text": [ - "10\n", - "[4 6]\n", - "[3 7]\n" + "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19] 10\n" ], "name": "stdout" } @@ -391,32 +442,84 @@ }, { "metadata": { - "id": "trevoygOGPN-", + "id": "PFQbi5rDcQU6", + "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": "eKuxXhmC-Iju", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 68 + "height": 34 }, - "outputId": "abfee82b-6ac2-476c-e97a-4b7ee34471b7" + "outputId": "27fafc2e-ddd9-48da-ff48-bd56d8b8387d" + }, + "cell_type": "code", + "source": [ + "np.intersect1d(a,b)" + ], + "execution_count": 21, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([2, 4])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 21 + } + ] + }, + { + "metadata": { + "id": "hOBhal1JcQuM", + "colab_type": "code", + "colab": {} }, "cell_type": "code", "source": [ - "b = np.arange(10)\n", - "\n", - "print(b)\n", "\n", - "mask = b%2!=0 \n", - "print(mask)\n", - "print(b[mask])\n" + "a = np.arange(10)" ], "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "RMcB9817hmf1", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "fb3cd282-71c5-4122-862c-86b5c9b6bb0c" + }, + "cell_type": "code", + "source": [ + "print(a.reshape((2,5)))" + ], + "execution_count": 23, "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" + "[[0 1 2 3 4]\n", + " [5 6 7 8 9]]\n" ], "name": "stdout" } @@ -424,58 +527,193 @@ }, { "metadata": { - "id": "ZRLuYwiyGbdj", + "id": "hEUbmuKeijC-", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "ebd8bfc0-6ed5-402d-e2a1-6123edaa487b" + "outputId": "9a29e0e2-7a13-41fd-9d96-756604cfcdcc" }, "cell_type": "code", "source": [ - "modified_b = b\n", - "modified_b[mask] = -1\n", - "print(modified_b)" + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "#to array\n", + "arr = np.array(a)\n", + "arr" ], - "execution_count": 0, + "execution_count": 24, "outputs": [ { - "output_type": "stream", - "text": [ - "[ 0 -1 2 -1 4 -1 6 -1 8 -1]\n" - ], - "name": "stdout" + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 2, 3, 4, 5, 6, 7, 8, 9])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 24 } ] }, { "metadata": { - "id": "JFDUz8szGgCa", + "id": "6Ntc9RDrcQwV", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 119 + "height": 34 }, - "outputId": "83b676fe-7fe5-4b7c-b67c-fca5e30a0176" + "outputId": "6e6b8a61-5d52-448a-efe9-e3da376c9d89" }, "cell_type": "code", "source": [ - "a = np.arange(9).reshape(3,3)\n", - "print(a)\n", - "print(a[:, [1,0,2]])" + "#to list\n", + "lst = arr.tolist()\n", + "lst" + ], + "execution_count": 25, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 25 + } + ] + }, + { + "metadata": { + "id": "IN720_6vcQ0B", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + }, + "outputId": "438af848-1f9e-4ec5-fb28-aac47e98266b" + }, + "cell_type": "code", + "source": [ + "np.pad(np.zeros((10,10)), 1 ,'constant',constant_values=1)" + ], + "execution_count": 26, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", + " [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n", + " [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n", + " [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n", + " [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n", + " [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n", + " [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n", + " [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n", + " [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n", + " [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n", + " [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n", + " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 26 + } + ] + }, + { + "metadata": { + "id": "MMKMWAvtZIyA", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 153 + }, + "outputId": "59c83327-863b-482a-d015-0b9874e0f051" + }, + "cell_type": "code", + "source": [ + "h=int(8/2)\n", + "w=int(8/2)\n", + "np.tile(np.eye(2),(h,w))\n", + "#whats slicing+striding approach?" + ], + "execution_count": 27, + "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": 27 + } + ] + }, + { + "metadata": { + "id": "XexOcORt4sM_", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "\n", + "import numpy as np" ], "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "8Sr9Qq0n4sPM", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "160dbc20-a433-4007-86f5-abb4befcbca8" + }, + "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": 29, "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" + "[1 2 3]\n", + "\n", + "(2, 3)\n", + "1 2 4\n" ], "name": "stdout" } @@ -483,29 +721,45 @@ }, { "metadata": { - "id": "YDSnIObaGlkW", + "id": "HGeHakK44sRe", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 85 + "height": 221 }, - "outputId": "1a87d729-a84a-4d8f-dad0-d6de187e0f13" + "outputId": "7772d30a-9792-419f-c866-ec72d235b4f8" }, "cell_type": "code", "source": [ - "a = np.arange(9).reshape(3,3)\n", - "print(a)\n", - "print([[1,0,2]])" + "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, + "execution_count": 30, "outputs": [ { "output_type": "stream", "text": [ - "[[0 1 2]\n", - " [3 4 5]\n", - " [6 7 8]]\n", - "[[1, 0, 2]]\n" + "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.27869968 0.2071114 ]\n", + " [0.65202304 0.0191683 ]]\n" ], "name": "stdout" } @@ -513,30 +767,29 @@ }, { "metadata": { - "id": "PFQbi5rDcQU6", + "id": "MNiQ75RH4sTv", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 71 + "height": 51 }, - "outputId": "5cacf573-b004-4703-ba88-d251c49759ef" + "outputId": "17df09ea-00b4-4032-a45f-cc844d42cfc9" }, "cell_type": "code", "source": [ - "array1=[]\n", - "diff=(2.5-1.3)/64\n", - "for i in range(64):\n", - " array1.append(1.3+(i*diff))\n", - "print(array1)\n", - "print(len(array1))\n" + "\n", + "a = np.arange(10)\n", + "b = np.linspace(0,10, num=6)\n", + "print(a)\n", + "print(b)" ], - "execution_count": 0, + "execution_count": 31, "outputs": [ { "output_type": "stream", "text": [ - "[1.3, 1.31875, 1.3375000000000001, 1.35625, 1.375, 1.39375, 1.4125, 1.4312500000000001, 1.45, 1.46875, 1.4875, 1.50625, 1.525, 1.54375, 1.5625, 1.58125, 1.6, 1.61875, 1.6375, 1.65625, 1.675, 1.69375, 1.7125, 1.73125, 1.75, 1.76875, 1.7875, 1.80625, 1.8250000000000002, 1.84375, 1.8625, 1.88125, 1.9, 1.9187500000000002, 1.9375, 1.95625, 1.975, 1.99375, 2.0125, 2.03125, 2.05, 2.06875, 2.0875, 2.10625, 2.125, 2.14375, 2.1625, 2.18125, 2.2, 2.21875, 2.2375, 2.25625, 2.275, 2.29375, 2.3125, 2.33125, 2.35, 2.36875, 2.3875, 2.40625, 2.425, 2.44375, 2.4625, 2.48125]\n", - "64\n" + "[0 1 2 3 4 5 6 7 8 9]\n", + "[ 0. 2. 4. 6. 8. 10.]\n" ], "name": "stdout" } @@ -544,44 +797,39 @@ }, { "metadata": { - "id": "eKuxXhmC-Iju", + "id": "QOCrZFP54sXZ", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 153 + "height": 51 }, - "outputId": "c0421875-049d-4dbd-fc8c-645576a076ee" + "outputId": "a82cbd82-b2a6-471a-c526-87ce759ab2c2" }, "cell_type": "code", "source": [ - "import numpy as np\n", - "n=input(\"Enter a no.: \")\n", - "array1=np.arange(1,3*int(n)+1)\n", - "print (array1)\n", - "for j in range(len(array1)):\n", - " \n", - " c=array1[0]\n", + "a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n", "\n", - " for i in range(len(array1)-1):\n", - " \n", - " array1[i]=array1[i+1]\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", - " array1[len(array1)-1]=c\n", - " print (array1)" + "# 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, + "execution_count": 32, "outputs": [ { "output_type": "stream", "text": [ - "Enter a no.: 2\n", - "[1 2 3 4 5 6]\n", - "[2 3 4 5 6 1]\n", - "[3 4 5 6 1 2]\n", - "[4 5 6 1 2 3]\n", - "[5 6 1 2 3 4]\n", - "[6 1 2 3 4 5]\n", - "[1 2 3 4 5 6]\n" + "2\n", + "77\n" ], "name": "stdout" } @@ -589,40 +837,41 @@ }, { "metadata": { - "id": "hOBhal1JcQuM", + "id": "Z_pJbyi34sbe", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 238 + "height": 119 }, - "outputId": "d7d003b2-6fb4-4406-b618-67d0cc63dcd5" + "outputId": "dc2002ff-a00b-48e5-f502-494a4eb9dec8" }, "cell_type": "code", "source": [ - "lower=int(input(\"Enter the lower limit for the range:\"))\n", - "upper=int(input(\"Enter the upper limit for the range:\"))\n", - "for i in range(lower,upper+1):\n", - " if(i%2!=0):\n", - " print(i)" + "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, + "execution_count": 33, "outputs": [ { "output_type": "stream", "text": [ - "Enter the lower limit for the range:1\n", - "Enter the upper limit for the range:21\n", - "1\n", - "3\n", - "5\n", - "7\n", - "9\n", - "11\n", - "13\n", - "15\n", - "17\n", - "19\n", - "21\n" + "[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" } @@ -630,27 +879,30 @@ }, { "metadata": { - "id": "RMcB9817hmf1", + "id": "bmCJ1vA44seQ", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 34 + "height": 68 }, - "outputId": "de465d47-07bd-4170-c4f5-62a988935f1a" + "outputId": "40bb79a1-f59a-495e-cf83-de5361cbe4f0" }, "cell_type": "code", "source": [ - "set1 = {2, 4, 5, 6} \n", - "set2 = {4, 6, 7, 8} \n", - "print(\"set1 intersection set2 : \", set1.intersection(set2)) \n", - " " + "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, + "execution_count": 34, "outputs": [ { "output_type": "stream", "text": [ - "set1 intersection set2 : {4, 6}\n" + "10\n", + "[4 6]\n", + "[3 7]\n" ], "name": "stdout" } @@ -658,33 +910,35 @@ }, { "metadata": { - "id": "hEUbmuKeijC-", + "id": "ikgqGWj34sgr", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 85 + "height": 68 }, - "outputId": "8e684fe3-c8e2-40d1-d805-b9612903bf67" + "outputId": "240812f2-7609-4d07-de93-a102bcb32647" }, "cell_type": "code", "source": [ - "from numpy import array\n", - "data = array([[11, 22, 33],\n", - "\t\t[44, 55, 66],\n", - "\t\t[77, 88, 99]])\n", - "X, y = data[:, :-1], data[:, -1]\n", - "print(X)\n", - "print(y)" + "\n", + "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" ], - "execution_count": 0, + "execution_count": 35, "outputs": [ { "output_type": "stream", "text": [ - "[[11 22]\n", - " [44 55]\n", - " [77 88]]\n", - "[33 66 99]\n" + "[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" } @@ -692,36 +946,27 @@ }, { "metadata": { - "id": "6Ntc9RDrcQwV", + "id": "3hwPBYxF4sjr", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 119 + "height": 34 }, - "outputId": "859a54b2-8d5f-4ffe-a02c-01eb045ab58c" + "outputId": "dc432a24-4f86-48b2-861e-b97a0e093c47" }, "cell_type": "code", "source": [ - "import numpy as np\n", - "import numpy as np\n", - "x = np.arange(12, 38)\n", - "print(\"Original array:\")\n", - "print(x)\n", - "print(\"Reverse array:\")\n", - "x = x[::-1]\n", - "print(x)" + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "print(modified_b)" ], - "execution_count": 0, + "execution_count": 36, "outputs": [ { "output_type": "stream", "text": [ - "Original array:\n", - "[12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35\n", - " 36 37]\n", - "Reverse array:\n", - "[37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14\n", - " 13 12]\n" + "[ 0 -1 2 -1 4 -1 6 -1 8 -1]\n" ], "name": "stdout" } @@ -729,53 +974,32 @@ }, { "metadata": { - "id": "IN720_6vcQ0B", + "id": "Rf0LbjgF4sma", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 425 + "height": 119 }, - "outputId": "f3423aaa-4783-4cda-8602-395aa5bf92c5" + "outputId": "1412354d-502f-4e08-fe16-7f1cbaaa16c6" }, "cell_type": "code", "source": [ - "import numpy as np\n", - "x = np.zeros((10,10))\n", - "print(\"Original array:\")\n", - "print(x)\n", - "print(\"1 on the border and 0 inside in the array\")\n", - "x = np.pad(x, pad_width=1, mode='constant', constant_values=1)\n", - "print(x)" + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[:, [1,0,2]])" ], - "execution_count": 0, + "execution_count": 37, "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", - "0 on the border and 1 inside in the array\n", - "[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", - " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", - " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", - " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", - " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", - " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", - " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", - " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", - " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", - " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", - " [1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", - " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]\n" + "[[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" } @@ -783,42 +1007,49 @@ }, { "metadata": { - "id": "MMKMWAvtZIyA", + "id": "Jwst5ENa4saH", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 170 + "height": 119 }, - "outputId": "40baa00a-d614-4263-fe90-7273c8a31434" + "outputId": "466fdcc6-40a9-4eca-f46f-d4adda25a339" }, "cell_type": "code", "source": [ - "import numpy as np\n", - "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)\n" + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[[1,0,2], :])" ], - "execution_count": 0, + "execution_count": 38, "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" + "[[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": "zHdvGd324sWb", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] } ] } \ No newline at end of file