From 9bc7f0e7dc1f31be36375f1144458cc6052eb876 Mon Sep 17 00:00:00 2001 From: MONA7584908095 <43202653+MONA7584908095@users.noreply.github.com> Date: Thu, 27 Sep 2018 23:58:37 +0530 Subject: [PATCH 1/3] WEEK 1SUBMITTED WEEK2 SUBMITTED2 --- MONA7584908095.ipynb | 243 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 213 insertions(+), 30 deletions(-) diff --git a/MONA7584908095.ipynb b/MONA7584908095.ipynb index 9e2543a..6fc687f 100644 --- a/MONA7584908095.ipynb +++ b/MONA7584908095.ipynb @@ -1,32 +1,215 @@ { - "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": "MONA7584908095.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/MONA7584908095/Assignment-2/blob/MONA7584908095/MONA7584908095.ipynb)" + ] + }, + { + "metadata": { + "id": "l__k5Lh_Qp-_", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + " import numpy as np" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "DwE1dSUIRD1h", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 264 + }, + "outputId": "6abe7c69-50e5-4168-b7b4-27235f0e3aea" + }, + "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)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter the lower limit for the range:\n" + ], + "name": "stdout" + }, + { + "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[0;32m----> 1\u001b[0;31m \u001b[0mlower\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter the lower limit for the range:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mupper\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter the upper limit for the range:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mupper\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m%\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m!=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: ''" + ] + } + ] + }, + { + "metadata": { + "id": "wEq7cGt_RD6D", + "colab_type": "code", + "colab": {} + }, + "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": 0, + "outputs": [] + }, + { + "metadata": { + "id": "0frf_DyVREB1", + "colab_type": "code", + "colab": {} + }, + "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": 0, + "outputs": [] + }, + { + "metadata": { + "id": "u7DJ906MRD9f", + "colab_type": "code", + "colab": {} + }, + "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": 0, + "outputs": [] + }, + { + "metadata": { + "id": "rj8qPe4URD_r", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 201 + }, + "outputId": "0e99185f-4f38-4b47-b337-3d32e4219827" + }, + "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": 4, + "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": "dJ2fBHtCRD8D", + "colab_type": "code", + "colab": {} + }, + "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": 0, + "outputs": [] + }, + { + "metadata": { + "id": "0wUeAJ5WRD4x", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From c91ae0c170bbb26362ff8e7773fe977157b8f468 Mon Sep 17 00:00:00 2001 From: MONA7584908095 <43202653+MONA7584908095@users.noreply.github.com> Date: Fri, 28 Sep 2018 23:30:17 +0530 Subject: [PATCH 2/3] i submitted week 2 list numpy and numpy exercise --- MONA7584908095.ipynb | 720 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 652 insertions(+), 68 deletions(-) diff --git a/MONA7584908095.ipynb b/MONA7584908095.ipynb index 6fc687f..1b38105 100644 --- a/MONA7584908095.ipynb +++ b/MONA7584908095.ipynb @@ -40,90 +40,640 @@ }, { "metadata": { - "id": "DwE1dSUIRD1h", + "id": "CumcatFiShHm", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 264 + "height": 51 }, - "outputId": "6abe7c69-50e5-4168-b7b4-27235f0e3aea" + "outputId": "90ecb97c-627d-4fdf-95bf-aea2b3189510" }, "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)" + "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": 1, + "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": "9qkHJzuVSwPW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "2c3990df-6d93-4176-af66-a7650ea0829c" + }, + "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": 2, + "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": "ZvAw_CLdTBdg", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "856029b9-c1de-4088-80f3-94095a054ccb" + }, + "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": 3, "outputs": [ { "output_type": "stream", "text": [ - "Enter the lower limit for the range:\n" + "[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": "1QASD5O-THeb", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "9b137851-9ab3-437e-c453-c9c6b01c91f5" + }, + "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": 4, + "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": "2KO1f78VTO_b", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 }, + "outputId": "113979df-39ed-4810-9677-c6c01d9da290" + }, + "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": 5, + "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[0;32m----> 1\u001b[0;31m \u001b[0mlower\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter the lower limit for the range:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mupper\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter the upper limit for the range:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mupper\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m%\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m!=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: ''" - ] + "output_type": "stream", + "text": [ + "[1 2 3]\n", + "(2, 3)\n", + "1 2 4\n" + ], + "name": "stdout" } ] }, { "metadata": { - "id": "wEq7cGt_RD6D", + "id": "cz3d2zPJTTxe", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + }, + "outputId": "f0e381c6-c704-4f9f-9295-93cee7ae4235" + }, + "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": 6, + "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.26047818 0.07837141]\n", + " [0.93970419 0.25886382]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "3r8gB16YTazj", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "7483ed6d-aab5-42fe-fe65-b51f290b4b91" + }, + "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 1 2 3 4 5 6 7 8 9]\n", + "[ 0. 2. 4. 6. 8. 10.]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "G7XRNlIQT3-h", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "3fa1a04e-d20f-4e7a-c176-89161c961ef8" + }, + "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": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n", + "77\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "uHtGAAptT7Lh", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "c7ba9cdc-3e75-4b5e-e706-7542d4322be0" + }, + "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": 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": "yRs33DKQUDzf", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "4f4460d8-1594-458f-c360-ed3a44c048cc" + }, + "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": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10\n", + "[4 6]\n", + "[3 7]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "T00-P1XVUD1y", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "008f427f-9144-4cba-8444-b4e24c305f79" }, "cell_type": "code", "source": [ + "b = np.arange(10)\n", "\n", - " \n", + "print(b)\n", + "\n", + "mask = b%2!=0 \n", + "print(mask)\n", + "print(b[mask])\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": "NCI0FTF3UD5K", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "cf399ae1-10cd-4490-a668-ae78b5549210" + }, + "cell_type": "code", + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\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": "83IbAzVNUdVL", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "b4a9cf1f-c161-4a5f-9da7-0c76c63b0d1a" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\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": "LuICdiXgUdXl", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "c2326eda-6d41-43ad-9e83-b394dbd7364e" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "print([[1,0,2]])" + ], + "execution_count": 14, + "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": "0kuweqHbUdcL", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 71 + }, + "outputId": "c8c10a5e-1a45-477a-8e06-993607f97750" + }, + "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": 15, + "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": "Xi05YqvdUdfG", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 153 + }, + "outputId": "05284b17-bfd1-46bf-e308-85f831c0edec" + }, + "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": 17, + "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": { + "id": "lR3LytRdUwNO", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 238 + }, + "outputId": "0fc128f3-6974-47a4-d48e-b5ebd02ed873" + }, + "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)" + ], + "execution_count": 18, + "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": "UFB0j74IU2-K", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "4adab402-d414-4738-b9c3-2af78c0cb521" + }, + "cell_type": "code", + "source": [ "set1 = {2, 4, 5, 6} \n", "set2 = {4, 6, 7, 8} \n", "print(\"set1 intersection set2 : \", set1.intersection(set2)) \n", " " ], - "execution_count": 0, - "outputs": [] + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "text": [ + "set1 intersection set2 : {4, 6}\n" + ], + "name": "stdout" + } + ] }, { "metadata": { - "id": "0frf_DyVREB1", + "id": "mNBAkgdDU7_V", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "8a2b5cb6-4325-4b42-babf-04d6cf082af9" }, "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": 0, - "outputs": [] + "execution_count": 20, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[11 22]\n", + " [44 55]\n", + " [77 88]]\n", + "[33 66 99]\n" + ], + "name": "stdout" + } + ] }, { "metadata": { - "id": "u7DJ906MRD9f", + "id": "8VlS5hf9U8vH", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "b63fc2e6-381f-4c74-fefd-c855751caf3f" }, "cell_type": "code", "source": [ @@ -136,44 +686,71 @@ "x = x[::-1]\n", "print(x)" ], - "execution_count": 0, - "outputs": [] + "execution_count": 21, + "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": "rj8qPe4URD_r", + "id": "y8JYP7NjVEqk", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 201 + "height": 425 }, - "outputId": "0e99185f-4f38-4b47-b337-3d32e4219827" + "outputId": "613367f3-e680-44f3-c885-92d8b6ef3a49" }, "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": 4, + "execution_count": 22, "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" + "[[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", + "1 on the border and 0 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" ], "name": "stdout" } @@ -181,9 +758,13 @@ }, { "metadata": { - "id": "dJ2fBHtCRD8D", + "id": "n0AlH-OfVMUx", "colab_type": "code", - "colab": {} + "colab": { + "base_uri": "https://localhost:8080/", + "height": 170 + }, + "outputId": "aa95025a-e1bd-4524-9858-aeddca4d67e3" }, "cell_type": "code", "source": [ @@ -195,21 +776,24 @@ "x[::2,1::2] = 1\n", "print(x)\n" ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "0wUeAJ5WRD4x", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "" - ], - "execution_count": 0, - "outputs": [] + "execution_count": 23, + "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 5cac28b4f58decbe57aaa4a1f35b29f2e9d67b1f Mon Sep 17 00:00:00 2001 From: MONA7584908095 <43202653+MONA7584908095@users.noreply.github.com> Date: Fri, 12 Oct 2018 20:20:58 +0530 Subject: [PATCH 3/3] i completed assignment 2 --- MONA7584908095.ipynb | 925 +++++++++++++++++++++++++++---------------- 1 file changed, 591 insertions(+), 334 deletions(-) diff --git a/MONA7584908095.ipynb b/MONA7584908095.ipynb index 1b38105..e12cc67 100644 --- a/MONA7584908095.ipynb +++ b/MONA7584908095.ipynb @@ -33,7 +33,7 @@ }, "cell_type": "code", "source": [ - " import numpy as np" + "import random" ], "execution_count": 0, "outputs": [] @@ -42,26 +42,35 @@ "metadata": { "id": "CumcatFiShHm", "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "dummy_list = [random.randrange(10) for i in range(11)]" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "9qkHJzuVSwPW", + "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 51 + "height": 34 }, - "outputId": "90ecb97c-627d-4fdf-95bf-aea2b3189510" + "outputId": "4bf4572a-def6-4d73-abdc-72448595f778" }, "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)\n" ], - "execution_count": 1, + "execution_count": 3, "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" + "[9, 9, 6, 0, 8, 9, 5, 1, 1, 7, 7]\n" ], "name": "stdout" } @@ -69,27 +78,24 @@ }, { "metadata": { - "id": "9qkHJzuVSwPW", + "id": "ZvAw_CLdTBdg", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "2c3990df-6d93-4176-af66-a7650ea0829c" + "outputId": "b6fa5f59-04df-4255-8ce1-377540f74123" }, "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" + "print(list(reversed(dummy_list)))" ], - "execution_count": 2, + "execution_count": 4, "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" + "[7, 7, 1, 1, 5, 9, 8, 0, 6, 9, 9]\n" ], "name": "stdout" } @@ -97,28 +103,39 @@ }, { "metadata": { - "id": "ZvAw_CLdTBdg", + "id": "1QASD5O-THeb", + "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": "2KO1f78VTO_b", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "856029b9-c1de-4088-80f3-94095a054ccb" + "outputId": "36b491ad-321b-449a-fdf9-f100b3f0500b" }, "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)" + "#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": 3, + "execution_count": 6, "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" + "[11, 209, 22, 4, 9, 9, 14.45, 46.67, 91, 19.009999999999998, 19.02]\n" ], "name": "stdout" } @@ -126,30 +143,55 @@ }, { "metadata": { - "id": "1QASD5O-THeb", + "id": "cz3d2zPJTTxe", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "9b137851-9ab3-437e-c453-c9c6b01c91f5" + "outputId": "8cd75476-605c-47f4-bbd0-131e81f36b2d" }, "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)" + "dummy_dict = {}\n", + "[dummy_dict.update({i:dummy_list.count(i)}) for i in dummy_list]" ], - "execution_count": 4, + "execution_count": 7, + "outputs": [ + { + "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": "3r8gB16YTazj", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "33854b06-b7a3-4114-b276-758a70c1f97c" + }, + "cell_type": "code", + "source": [ + "print(dummy_dict)" + ], + "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" + "{11: 1, 209: 1, 22: 1, 4: 1, 9: 2, 14.45: 1, 46.67: 1, 91: 1, 19.009999999999998: 1, 19.02: 1}\n" ], "name": "stdout" } @@ -157,30 +199,28 @@ }, { "metadata": { - "id": "2KO1f78VTO_b", + "id": "G7XRNlIQT3-h", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 68 + "height": 51 }, - "outputId": "113979df-39ed-4810-9677-c6c01d9da290" + "outputId": "6e6879f6-664b-407f-b008-88252f5c4f3b" }, "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": 5, + "execution_count": 9, "outputs": [ { "output_type": "stream", "text": [ - "[1 2 3]\n", - "(2, 3)\n", - "1 2 4\n" + "[4, 9, 9, 11, 14.45, 19.009999999999998, 19.02, 22, 46.67, 91, 209]\n", + "[209, 91, 46.67, 22, 19.02, 19.009999999999998, 14.45, 11, 9, 9, 4]\n" ], "name": "stdout" } @@ -188,45 +228,24 @@ }, { "metadata": { - "id": "cz3d2zPJTTxe", + "id": "uHtGAAptT7Lh", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 221 + "height": 34 }, - "outputId": "f0e381c6-c704-4f9f-9295-93cee7ae4235" + "outputId": "b2a98121-67e7-455c-a492-c7fd32ea4881" }, "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": 6, + "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.26047818 0.07837141]\n", - " [0.93970419 0.25886382]]\n" + "Before [11, 209, 22, 4, 9, 9, 14.45, 46.67, 91, 19.009999999999998, 19.02]\n" ], "name": "stdout" } @@ -234,28 +253,60 @@ }, { "metadata": { - "id": "3r8gB16YTazj", + "id": "yRs33DKQUDzf", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 197 + }, + "outputId": "6832e269-be22-4195-d674-50dee48e6545" + }, + "cell_type": "code", + "source": [ + "\n", + "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)\n" + ], + "execution_count": 12, + "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 2\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 3\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----> 4\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": "T00-P1XVUD1y", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, - "outputId": "7483ed6d-aab5-42fe-fe65-b51f290b4b91" + "outputId": "979bc46d-0ea8-4534-82a1-0294e031128e" }, "cell_type": "code", "source": [ - "a = np.arange(10)\n", - "b = np.linspace(0,10, num=6)\n", - "print(a)\n", - "print(b)" + "print('Before',dummy_list)\n", + "x=2\n", + "dummy_list.pop(x)\n", + "print('After',dummy_list)" ], - "execution_count": 7, + "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" + "Before [11, 209, 22, 4, 9, 9, 14.45, 46.67, 91, 19.009999999999998, 19.02]\n", + "After [11, 209, 4, 9, 9, 14.45, 46.67, 91, 19.009999999999998, 19.02]\n" ], "name": "stdout" } @@ -263,30 +314,59 @@ }, { "metadata": { - "id": "G7XRNlIQT3-h", + "id": "NCI0FTF3UD5K", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 197 + }, + "outputId": "37956975-52c3-403b-956b-ed562f14e5ac" + }, + "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": 14, + "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": "83IbAzVNUdVL", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, - "outputId": "3fa1a04e-d20f-4e7a-c176-89161c961ef8" + "outputId": "e2cb9a5a-1c64-40cb-c63e-e7161ae0bf1e" }, "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": 8, + "execution_count": 15, "outputs": [ { "output_type": "stream", "text": [ - "2\n", - "77\n" + "Before [11, 209, 4, 9, 9, 14.45, 46.67, 91, 19.009999999999998, 19.02]\n", + "After []\n" ], "name": "stdout" } @@ -294,40 +374,41 @@ }, { "metadata": { - "id": "uHtGAAptT7Lh", + "id": "LuICdiXgUdXl", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "\n", + "import numpy as np #import numpy" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "0kuweqHbUdcL", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 119 + "height": 51 }, - "outputId": "c7ba9cdc-3e75-4b5e-e706-7542d4322be0" + "outputId": "d6149ac4-afd7-49e3-ed45-a7732da09af7" }, "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": 9, + "execution_count": 17, "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: 121\n", + "[1 2 3 1 2 3 1 2 3]\n" ], "name": "stdout" } @@ -335,30 +416,25 @@ }, { "metadata": { - "id": "yRs33DKQUDzf", + "id": "Xi05YqvdUdfG", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 68 + "height": 34 }, - "outputId": "4f4460d8-1594-458f-c360-ed3a44c048cc" + "outputId": "965ac65d-b301-4077-ddd2-f5ac989aac11" }, "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": 10, + "execution_count": 18, "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" } @@ -366,32 +442,83 @@ }, { "metadata": { - "id": "T00-P1XVUD1y", + "id": "lR3LytRdUwNO", + "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": "UFB0j74IU2-K", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 68 + "height": 34 }, - "outputId": "008f427f-9144-4cba-8444-b4e24c305f79" + "outputId": "a4ac84c9-590d-434b-af8f-9e353fc9d70a" }, "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" + "np.intersect1d(a,b)" + ], + "execution_count": 20, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([2, 4])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 20 + } + ] + }, + { + "metadata": { + "id": "mNBAkgdDU7_V", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "a = np.arange(10)\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "8VlS5hf9U8vH", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "64121120-dc16-43b3-c615-36b4536f3014" + }, + "cell_type": "code", + "source": [ + "print(a.reshape((2,5)))" ], - "execution_count": 11, + "execution_count": 22, "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" } @@ -399,58 +526,194 @@ }, { "metadata": { - "id": "NCI0FTF3UD5K", + "id": "y8JYP7NjVEqk", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "cf399ae1-10cd-4490-a668-ae78b5549210" + "outputId": "1611a0ae-e89d-4087-e420-9dc0858ddfbd" }, "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": 12, + "execution_count": 23, "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": 23 } ] }, { "metadata": { - "id": "83IbAzVNUdVL", + "id": "n0AlH-OfVMUx", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 119 + "height": 34 }, - "outputId": "b4a9cf1f-c161-4a5f-9da7-0c76c63b0d1a" + "outputId": "18ffe17f-8dd6-4064-837f-9758220b40df" }, "cell_type": "code", "source": [ - "a = np.arange(9).reshape(3,3)\n", + "#to list\n", + "lst = arr.tolist()\n", + "lst" + ], + "execution_count": 24, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 24 + } + ] + }, + { + "metadata": { + "id": "jzeWGEYyvl0Z", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + }, + "outputId": "976f818a-5cae-4c9f-d04d-c745f8660113" + }, + "cell_type": "code", + "source": [ + "np.pad(np.zeros((10,10)), 1 ,'constant',constant_values=1)" + ], + "execution_count": 25, + "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": 25 + } + ] + }, + { + "metadata": { + "id": "PmRNpQ13vl2t", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 153 + }, + "outputId": "1fcf7618-032e-428d-d7ab-11b8d8cff7f1" + }, + "cell_type": "code", + "source": [ + "\n", + "h=int(8/2)\n", + "w=int(8/2)\n", + "np.tile(np.eye(2),(h,w))\n", + "#whats slicing+striding approach?" + ], + "execution_count": 26, + "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": 26 + } + ] + }, + { + "metadata": { + "id": "eaDbh7kSvl6J", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "import numpy as np" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "vLLxM1d5vl9g", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "f5e09459-55b5-4069-f505-ba0e43a716fa" + }, + "cell_type": "code", + "source": [ + "\n", + "a = np.array([1, 2, 3]) # Create a rank 1 array\n", "print(a)\n", - "print(a[:, [1,0,2]])" + "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": 13, + "execution_count": 28, "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" } @@ -458,29 +721,45 @@ }, { "metadata": { - "id": "LuICdiXgUdXl", + "id": "tzn3SZKMvl5M", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 85 + "height": 221 }, - "outputId": "c2326eda-6d41-43ad-9e83-b394dbd7364e" + "outputId": "c13b3d2b-5341-400d-c9c8-a096db463669" }, "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": 14, + "execution_count": 29, "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.12628375 0.03405262]\n", + " [0.72124696 0.92923562]]\n" ], "name": "stdout" } @@ -488,30 +767,28 @@ }, { "metadata": { - "id": "0kuweqHbUdcL", + "id": "Pd707GoTv9_p", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 71 + "height": 51 }, - "outputId": "c8c10a5e-1a45-477a-8e06-993607f97750" + "outputId": "194fd490-6b25-45e9-b7b2-03a7288564a0" }, "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" + "a = np.arange(10)\n", + "b = np.linspace(0,10, num=6)\n", + "print(a)\n", + "print(b)" ], - "execution_count": 15, + "execution_count": 30, "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" } @@ -519,44 +796,40 @@ }, { "metadata": { - "id": "Xi05YqvdUdfG", + "id": "qsRkvkfev-CD", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 153 + "height": 51 }, - "outputId": "05284b17-bfd1-46bf-e308-85f831c0edec" + "outputId": "c6735a42-d123-484a-9955-109e2182662c" }, "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", + "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", - " array1[len(array1)-1]=c\n", - " print (array1)" + "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": 17, + "execution_count": 31, "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" } @@ -564,40 +837,42 @@ }, { "metadata": { - "id": "lR3LytRdUwNO", + "id": "4TKsk-F5v-Fd", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 238 + "height": 119 }, - "outputId": "0fc128f3-6974-47a4-d48e-b5ebd02ed873" + "outputId": "f9530aed-d5a2-4638-95f9-adcc03329a43" }, "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)" + "\n", + "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": 18, + "execution_count": 32, "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" } @@ -605,27 +880,31 @@ }, { "metadata": { - "id": "UFB0j74IU2-K", + "id": "e1uoAiZSwKpa", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 34 + "height": 68 }, - "outputId": "4adab402-d414-4738-b9c3-2af78c0cb521" + "outputId": "80893bde-d93e-41fd-e902-1aa1ab2f98e4" }, "cell_type": "code", "source": [ - "set1 = {2, 4, 5, 6} \n", - "set2 = {4, 6, 7, 8} \n", - "print(\"set1 intersection set2 : \", set1.intersection(set2)) \n", - " " + "\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": 19, + "execution_count": 33, "outputs": [ { "output_type": "stream", "text": [ - "set1 intersection set2 : {4, 6}\n" + "10\n", + "[4 6]\n", + "[3 7]\n" ], "name": "stdout" } @@ -633,33 +912,34 @@ }, { "metadata": { - "id": "mNBAkgdDU7_V", + "id": "4oD_B6rbwOfu", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 85 + "height": 68 }, - "outputId": "8a2b5cb6-4325-4b42-babf-04d6cf082af9" + "outputId": "e4bdd0ce-d4a6-46ef-ed38-455db0b39a22" }, "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)" + "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": 20, + "execution_count": 34, "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" } @@ -667,36 +947,27 @@ }, { "metadata": { - "id": "8VlS5hf9U8vH", + "id": "3WZiMhzYwOip", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 119 + "height": 34 }, - "outputId": "b63fc2e6-381f-4c74-fefd-c855751caf3f" + "outputId": "91b21dac-4319-4a52-fdb1-ec6e3d632990" }, "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)\n" ], - "execution_count": 21, + "execution_count": 35, "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" } @@ -704,53 +975,32 @@ }, { "metadata": { - "id": "y8JYP7NjVEqk", + "id": "EJiPplKvwOms", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 425 + "height": 119 }, - "outputId": "613367f3-e680-44f3-c885-92d8b6ef3a49" + "outputId": "6c0a2587-d5c5-4e3a-809b-bfcf4b296ff3" }, "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": 22, + "execution_count": 36, "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", - "1 on the border and 0 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" } @@ -758,42 +1008,49 @@ }, { "metadata": { - "id": "n0AlH-OfVMUx", + "id": "VH_wixPDwbe9", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 170 + "height": 119 }, - "outputId": "aa95025a-e1bd-4524-9858-aeddca4d67e3" + "outputId": "a000d990-6bec-4044-d73d-9cb26958acb8" }, "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": 23, + "execution_count": 37, "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": "3-BtwJktwbh9", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] } ] } \ No newline at end of file