diff --git a/ASIF8240233397.ipynb b/ASIF8240233397.ipynb index 9e2543a..d97eb82 100644 --- a/ASIF8240233397.ipynb +++ b/ASIF8240233397.ipynb @@ -1,32 +1,1055 @@ { - "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": [], + "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/ASIF8240233397/Assignment-2/blob/ASIF8240233397/ASIF8240233397.ipynb)" + ] + }, + { + "metadata": { + "id": "H3RpbLQaBbhj", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "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": [] + }, + { + "metadata": { + "id": "w24qqVoFIXlj", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "eea0a5cb-0319-411f-9cb5-6ddd5c934882" + }, + "cell_type": "code", + "source": [ + "print(dummy_list)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[8, 4, 0, 2, 4, 2, 1, 9, 7, 9, 3]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "aq554l4s39gD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "a6bf55ee-c501-4a7d-d9ce-24095211d5a4" + }, + "cell_type": "code", + "source": [ + "print(list(reversed(dummy_list)))" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[3, 9, 7, 9, 1, 2, 4, 2, 0, 4, 8]\n" + ], + "name": "stdout" + } + ] + }, + { + "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": "1db8ef7e-f125-4e1f-e0be-15efd639fa7e" + }, + "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": [ + "[10, 204, 16, 6, 5, 2, 10.45, 54.67, 97, 21.009999999999998, 15.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "xjwmHEphKxyn", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "838809c5-dda6-43e3-91e6-3e049c93f9aa" + }, + "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": "CK4IwoIAEAuh", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "84ffb0dc-8da7-454c-9657-1b888c5d4ce8" + }, + "cell_type": "code", + "source": [ + "print(dummy_dict)" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{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" + } + ] + }, + { + "metadata": { + "id": "iCsX5yPoEtsD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "c276a0a2-eae2-4a19-c962-42b8be8d7158" + }, + "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": [ + "[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" + } + ] + }, + { + "metadata": { + "id": "l7pqcM4zFDJb", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "82e55244-4890-485e-a3e4-db25b43ab801" + }, + "cell_type": "code", + "source": [ + "print('Before',dummy_list)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Before [10, 204, 16, 6, 5, 2, 10.45, 54.67, 97, 21.009999999999998, 15.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "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": "03160024-91ff-4981-a1b3-d7cc804296f4" + }, + "cell_type": "code", + "source": [ + "\n", + "print('Before',dummy_list)\n", + "x=2\n", + "dummy_list.pop(x)\n", + "print('After',dummy_list)" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "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" + } + ] + }, + { + "metadata": { + "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": "080f55b3-3451-4470-8bd6-5be205c17d38" + }, + "cell_type": "code", + "source": [ + "print('Before',dummy_list)\n", + "dummy_list.clear()\n", + "print('After',dummy_list)" + ], + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Before [10, 204, 6, 5, 2, 10.45, 54.67, 97, 21.009999999999998, 15.02]\n", + "After []\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "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": 51 + }, + "outputId": "c2b8af30-80a7-483d-e647-89306c266faf" + }, + "cell_type": "code", + "source": [ + "n = int(input(\"n: \"))\n", + "arr = np.tile([1,2,3],3)\n", + "print(arr)" + ], + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "text": [ + "n: 3\n", + "[1 2 3 1 2 3 1 2 3]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "YDSnIObaGlkW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "db37885d-8037-4fb1-8135-a904026aed33" + }, + "cell_type": "code", + "source": [ + "arr = list(range(1,2*10,2))\n", + "print(arr, len(arr))" + ], + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19] 10\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "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": 34 + }, + "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": [ + "\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]\n", + " [5 6 7 8 9]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "hEUbmuKeijC-", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "9a29e0e2-7a13-41fd-9d96-756604cfcdcc" + }, + "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": 24, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 2, 3, 4, 5, 6, 7, 8, 9])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 24 + } + ] + }, + { + "metadata": { + "id": "6Ntc9RDrcQwV", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "6e6b8a61-5d52-448a-efe9-e3da376c9d89" + }, + "cell_type": "code", + "source": [ + "#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": [ + "[1 2 3]\n", + "\n", + "(2, 3)\n", + "1 2 4\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "HGeHakK44sRe", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + }, + "outputId": "7772d30a-9792-419f-c866-ec72d235b4f8" + }, + "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": 30, + "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.27869968 0.2071114 ]\n", + " [0.65202304 0.0191683 ]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "MNiQ75RH4sTv", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "17df09ea-00b4-4032-a45f-cc844d42cfc9" + }, + "cell_type": "code", + "source": [ + "\n", + "a = np.arange(10)\n", + "b = np.linspace(0,10, num=6)\n", + "print(a)\n", + "print(b)" + ], + "execution_count": 31, + "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": "QOCrZFP54sXZ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "a82cbd82-b2a6-471a-c526-87ce759ab2c2" + }, + "cell_type": "code", + "source": [ + "a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n", + "\n", + "# Use slicing to pull out the subarray consisting of the first 2 rows\n", + "# and columns 1 and 2; b is the following array of shape (2, 2):\n", + "# [[2 3]\n", + "# [6 7]]\n", + "b = a[:2, 1:3]\n", + "\n", + "# A slice of an array is a view into the same data, so modifying it\n", + "# will modify the original array.\n", + "\n", + "print(a[0, 1]) # Prints \"2\"\n", + "\n", + "b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]\n", + "print(a[0, 1]) # Prints \"77\"" + ], + "execution_count": 32, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n", + "77\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Z_pJbyi34sbe", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "dc2002ff-a00b-48e5-f502-494a4eb9dec8" + }, + "cell_type": "code", + "source": [ + "a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n", + "\n", + "row_r1 = a[1, :] # Rank 1 view of the second row of a\n", + "row_r2 = a[1:2, :] # Rank 2 view of the second row of a\n", + "\n", + "print(row_r1, row_r1.shape) # Prints \"[5 6 7 8] (4,)\"\n", + "print(row_r2, row_r2.shape) # Prints \"[[5 6 7 8]] (1, 4)\"\n", + "\n", + "col_r1 = a[:, 1]\n", + "col_r2 = a[:, 1:2]\n", + "\n", + "print(col_r1, col_r1.shape) # Prints \"[ 2 6 10] (3,)\"\n", + "print(col_r2, col_r2.shape)" + ], + "execution_count": 33, + "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": "bmCJ1vA44seQ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "40bb79a1-f59a-495e-cf83-de5361cbe4f0" + }, + "cell_type": "code", + "source": [ + "x = np.array([[1,2],[3,4]])\n", + "\n", + "print(np.sum(x)) # Compute sum of all elements; prints \"10\"\n", + "print(np.sum(x, axis=0)) # Compute sum of each column; prints \"[4 6]\"\n", + "print(np.sum(x, axis=1)) # Compute sum of each row; prints \"[3 7]\"" + ], + "execution_count": 34, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10\n", + "[4 6]\n", + "[3 7]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "ikgqGWj34sgr", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "240812f2-7609-4d07-de93-a102bcb32647" + }, + "cell_type": "code", + "source": [ + "\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": 35, + "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": "3hwPBYxF4sjr", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "dc432a24-4f86-48b2-861e-b97a0e093c47" + }, + "cell_type": "code", + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "print(modified_b)" + ], + "execution_count": 36, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 0 -1 2 -1 4 -1 6 -1 8 -1]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Rf0LbjgF4sma", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "1412354d-502f-4e08-fe16-7f1cbaaa16c6" + }, + "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", + "[[1 0 2]\n", + " [4 3 5]\n", + " [7 6 8]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Jwst5ENa4saH", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "466fdcc6-40a9-4eca-f46f-d4adda25a339" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[[1,0,2], :])" + ], + "execution_count": 38, + "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": "zHdvGd324sWb", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file