diff --git a/MONA7584908095.ipynb b/MONA7584908095.ipynb index 9e2543a..e12cc67 100644 --- a/MONA7584908095.ipynb +++ b/MONA7584908095.ipynb @@ -1,32 +1,1056 @@ { - "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 random" + ], + "execution_count": 0, + "outputs": [] + }, + { + "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": 34 + }, + "outputId": "4bf4572a-def6-4d73-abdc-72448595f778" + }, + "cell_type": "code", + "source": [ + "print(dummy_list)\n" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[9, 9, 6, 0, 8, 9, 5, 1, 1, 7, 7]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "ZvAw_CLdTBdg", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "b6fa5f59-04df-4255-8ce1-377540f74123" + }, + "cell_type": "code", + "source": [ + "print(list(reversed(dummy_list)))" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[7, 7, 1, 1, 5, 9, 8, 0, 6, 9, 9]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "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": "36b491ad-321b-449a-fdf9-f100b3f0500b" + }, + "cell_type": "code", + "source": [ + "#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": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[11, 209, 22, 4, 9, 9, 14.45, 46.67, 91, 19.009999999999998, 19.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "cz3d2zPJTTxe", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "8cd75476-605c-47f4-bbd0-131e81f36b2d" + }, + "cell_type": "code", + "source": [ + "dummy_dict = {}\n", + "[dummy_dict.update({i:dummy_list.count(i)}) for i in dummy_list]" + ], + "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": [ + "{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" + } + ] + }, + { + "metadata": { + "id": "G7XRNlIQT3-h", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "6e6879f6-664b-407f-b008-88252f5c4f3b" + }, + "cell_type": "code", + "source": [ + "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": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[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" + } + ] + }, + { + "metadata": { + "id": "uHtGAAptT7Lh", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "b2a98121-67e7-455c-a492-c7fd32ea4881" + }, + "cell_type": "code", + "source": [ + "print('Before',dummy_list)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Before [11, 209, 22, 4, 9, 9, 14.45, 46.67, 91, 19.009999999999998, 19.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "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": "979bc46d-0ea8-4534-82a1-0294e031128e" + }, + "cell_type": "code", + "source": [ + "print('Before',dummy_list)\n", + "x=2\n", + "dummy_list.pop(x)\n", + "print('After',dummy_list)" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "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" + } + ] + }, + { + "metadata": { + "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": "e2cb9a5a-1c64-40cb-c63e-e7161ae0bf1e" + }, + "cell_type": "code", + "source": [ + "print('Before',dummy_list)\n", + "dummy_list.clear()\n", + "print('After',dummy_list)" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Before [11, 209, 4, 9, 9, 14.45, 46.67, 91, 19.009999999999998, 19.02]\n", + "After []\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "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": 51 + }, + "outputId": "d6149ac4-afd7-49e3-ed45-a7732da09af7" + }, + "cell_type": "code", + "source": [ + "n = int(input(\"n: \"))\n", + "arr = np.tile([1,2,3],3)\n", + "print(arr)" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "text": [ + "n: 121\n", + "[1 2 3 1 2 3 1 2 3]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Xi05YqvdUdfG", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "965ac65d-b301-4077-ddd2-f5ac989aac11" + }, + "cell_type": "code", + "source": [ + "arr = list(range(1,2*10,2))\n", + "print(arr, len(arr))" + ], + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19] 10\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "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": 34 + }, + "outputId": "a4ac84c9-590d-434b-af8f-9e353fc9d70a" + }, + "cell_type": "code", + "source": [ + "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": 22, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0 1 2 3 4]\n", + " [5 6 7 8 9]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "y8JYP7NjVEqk", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "1611a0ae-e89d-4087-e420-9dc0858ddfbd" + }, + "cell_type": "code", + "source": [ + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "#to array\n", + "arr = np.array(a)\n", + "arr" + ], + "execution_count": 23, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 2, 3, 4, 5, 6, 7, 8, 9])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 23 + } + ] + }, + { + "metadata": { + "id": "n0AlH-OfVMUx", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "18ffe17f-8dd6-4064-837f-9758220b40df" + }, + "cell_type": "code", + "source": [ + "#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(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": 28, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3]\n", + "\n", + "(2, 3)\n", + "1 2 4\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "tzn3SZKMvl5M", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + }, + "outputId": "c13b3d2b-5341-400d-c9c8-a096db463669" + }, + "cell_type": "code", + "source": [ + "a = np.zeros(shape=(2,2))\n", + "b = np.ones(shape = (3,3))\n", + "c = np.eye(2)\n", + "d = np.full(shape=(3,3), fill_value=5)\n", + "e = np.random.random((2,2))\n", + "\n", + "print('a', a)\n", + "print('b',b)\n", + "print('c',c)\n", + "print('d',d)\n", + "print('e',e)" + ], + "execution_count": 29, + "outputs": [ + { + "output_type": "stream", + "text": [ + "a [[0. 0.]\n", + " [0. 0.]]\n", + "b [[1. 1. 1.]\n", + " [1. 1. 1.]\n", + " [1. 1. 1.]]\n", + "c [[1. 0.]\n", + " [0. 1.]]\n", + "d [[5 5 5]\n", + " [5 5 5]\n", + " [5 5 5]]\n", + "e [[0.12628375 0.03405262]\n", + " [0.72124696 0.92923562]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Pd707GoTv9_p", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "194fd490-6b25-45e9-b7b2-03a7288564a0" + }, + "cell_type": "code", + "source": [ + "a = np.arange(10)\n", + "b = np.linspace(0,10, num=6)\n", + "print(a)\n", + "print(b)" + ], + "execution_count": 30, + "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": "qsRkvkfev-CD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "c6735a42-d123-484a-9955-109e2182662c" + }, + "cell_type": "code", + "source": [ + "\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", + "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": 31, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n", + "77\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "4TKsk-F5v-Fd", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "f9530aed-d5a2-4638-95f9-adcc03329a43" + }, + "cell_type": "code", + "source": [ + "\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": 32, + "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": "e1uoAiZSwKpa", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "80893bde-d93e-41fd-e902-1aa1ab2f98e4" + }, + "cell_type": "code", + "source": [ + "\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": 33, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10\n", + "[4 6]\n", + "[3 7]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "4oD_B6rbwOfu", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "e4bdd0ce-d4a6-46ef-ed38-455db0b39a22" + }, + "cell_type": "code", + "source": [ + "b = np.arange(10)\n", + "\n", + "print(b)\n", + "\n", + "mask = b%2!=0 #perform computations on the list \n", + "\n", + "print(mask)\n", + "\n", + "print(b[mask]) #applying the mask on the numpy array" + ], + "execution_count": 34, + "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": "3WZiMhzYwOip", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "91b21dac-4319-4a52-fdb1-ec6e3d632990" + }, + "cell_type": "code", + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "print(modified_b)\n" + ], + "execution_count": 35, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 0 -1 2 -1 4 -1 6 -1 8 -1]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "EJiPplKvwOms", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "6c0a2587-d5c5-4e3a-809b-bfcf4b296ff3" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[:, [1,0,2]])" + ], + "execution_count": 36, + "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": "VH_wixPDwbe9", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "a000d990-6bec-4044-d73d-9cb26958acb8" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[[1,0,2], :])" + ], + "execution_count": 37, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0 1 2]\n", + " [3 4 5]\n", + " [6 7 8]]\n", + "[[3 4 5]\n", + " [0 1 2]\n", + " [6 7 8]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "3-BtwJktwbh9", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file