diff --git a/.ipynb_checkpoints/Numpy_Examples_1-checkpoint.ipynb b/.ipynb_checkpoints/Numpy_Examples_1-checkpoint.ipynb new file mode 100644 index 0000000..b5cd205 --- /dev/null +++ b/.ipynb_checkpoints/Numpy_Examples_1-checkpoint.ipynb @@ -0,0 +1,504 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "view-in-github" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/nabhoneel/Assignment-2/blob/nabhoneel/Numpy_Examples_1.ipynb)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "3pSVAeWfuPcq" + }, + "source": [ + "# Numpy Examples\n", + "\n", + "## What is numpy?\n", + "\n", + "#### Python has built-in:\n", + "\n", + "- containers: lists (costless insertion and append), dictionnaries (fast lookup)\n", + "- high-level number objects: integers, floating point\n", + "\n", + "#### Numpy is:\n", + "\n", + " - extension package to Python for multidimensional arrays\n", + " - closer to hardware (efficiency)\n", + " - designed for scientific computation (convenience)\n", + "\n", + "\n", + "#### Import numpy\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "ozUi4_X55UHE" + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "3-1ghFDF5N2z" + }, + "source": [ + "### Uncomment Print statement and run each cell to see the output\n", + "\n", + "#### Create numpy arrays\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "atYpk2ert0b-" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3]\n", + "\n", + "(2, 3)\n", + "1 2 4\n" + ] + } + ], + "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])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "Kro5ZOwXue5n" + }, + "source": [ + "#### Some basic functions for creating arrays. Print all the defined arrays and see the results." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "V3rdzgr9uhHS" + }, + "outputs": [ + { + "name": "stdout", + "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.96713436 0.12079029]\n", + " [0.68911551 0.5792308 ]]\n" + ] + } + ], + "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)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "8RPW_SutukjF" + }, + "source": [ + "#### Execute and understand :)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "-8JuqYt4upeo" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0. 0.]\n", + " [0. 0.]]\n", + "[[1. 1. 1.]\n", + " [1. 1. 1.]\n", + " [1. 1. 1.]]\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.\n", + " \"\"\"Entry point for launching an IPython kernel.\n", + "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:2: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.\n", + " \n" + ] + } + ], + "source": [ + "a == np.arange(10)\n", + "b == np.linspace(0,10, num=6)\n", + "print(a)\n", + "print(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MRHhbjx4uvYN" + }, + "source": [ + "#### Array Indexing" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "grF5_yUSuxVK" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "77\n" + ] + } + ], + "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\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "s400Gijxu0kO" + }, + "source": [ + "#### Slicing" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "kubpegh2u4zF" + }, + "outputs": [ + { + "name": "stdout", + "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" + ] + } + ], + "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)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "TmGnCO3AvE8t" + }, + "source": [ + "#### Aritmetic operations" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "YvBw3ImjvGqD" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "[4 6]\n", + "[3 7]\n" + ] + } + ], + "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]\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "uaVY3ZzD4pC2" + }, + "source": [ + "#### Using Boolean Mask" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "-PNfOMvh4_Gp" + }, + "outputs": [ + { + "name": "stdout", + "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" + ] + } + ], + "source": [ + "b = np.arange(10)\n", + "\n", + "print(b)\n", + "\n", + "mask = b%2!=0 #perform computations on the list \n", + "\n", + "print(mask)\n", + "\n", + "print(b[mask]) #applying the mask on the numpy array\n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "HbEPBbz-5J9K" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 0 -1 2 -1 4 -1 6 -1 8 -1]\n" + ] + } + ], + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "print(modified_b)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "zgSd71EEAHC7" + }, + "source": [ + "#### Swapping two columns in a 2d numpy array" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "-cvqeXd_AGo1" + }, + "outputs": [ + { + "name": "stdout", + "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" + ] + } + ], + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[:, [1,0,2]])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "U7ifiLY3Ayky" + }, + "source": [ + "#### Swapping two rows in a 2d numpy array" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "0FrOURRDAZNP" + }, + "outputs": [ + { + "name": "stdout", + "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" + ] + } + ], + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[[1,0,2], :])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "include_colab_link": true, + "name": "Numpy_Examples 1.ipynb", + "provenance": [], + "version": "0.3.2" + }, + "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.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/List.ipynb b/List.ipynb new file mode 100644 index 0000000..bc7b6e7 --- /dev/null +++ b/List.ipynb @@ -0,0 +1,290 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "List", + "version": "0.3.2", + "provenance": [], + "collapsed_sections": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/nabhoneel/Assignment-2/blob/nabhoneel/List.ipynb)" + ] + }, + { + "metadata": { + "id": "up7nFkrYIbui", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "import numpy as np" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "05f1yqOULgfd", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "43184d3e-7abb-471f-e314-1c9c3249308c" + }, + "cell_type": "code", + "source": [ + "dummy_list = [5, 6, 1, 2, 3] # random list\n", + "print(dummy_list)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[5, 6, 1, 2, 3]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "KZmpEjFgUTS7", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "2dbd11ec-866f-4102-9392-1d39e0fca80f" + }, + "cell_type": "code", + "source": [ + "print(dummy_list[::-1]) # reversed the random list" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[3, 2, 1, 6, 5]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "jLAMyuBiUZW0", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "a1e87b81-8774-4cde-8dd2-16942a94576a" + }, + "cell_type": "code", + "source": [ + "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "dummy_list = dummy_list + dummy_list_2 # added dummy_list_2 to original dummy_list\n", + "print(dummy_list)" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[5, 6, 1, 2, 3, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "40h4QgzHUbXi", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "4f7b51d7-6a0b-4d19-8a97-08a8aad5828d" + }, + "cell_type": "code", + "source": [ + "# frequency of each element in dummy_list\n", + "dummy_list_keys = list(set(dummy_list))\n", + "dummy_dict = {x: dummy_list.count(x) for x in dummy_list_keys}\n", + "\n", + "# print dummy_dict\n", + "print(dummy_dict)" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{0: 1, 1: 2, 2: 2, 3: 1, 4: 1, 5: 1, 6: 1, 200: 1, 9.45: 1, 12.01: 1, 45.67: 1, 12.02: 1, 16: 1, 90: 1}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "vHqzUbnoVqkj", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + }, + "outputId": "5ac41384-2590-4c6d-9419-1dc3258b3fa7" + }, + "cell_type": "code", + "source": [ + "dummy_list.sort() # ascending order sort\n", + "print(dummy_list)\n", + "\n", + "dummy_list.sort(reverse=True) # descending order sort\n", + "print(dummy_list)" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 2, 3, 4, 5, 6, 9.45, 12.01, 12.02, 16, 45.67, 90, 200]\n", + "[200, 90, 45.67, 16, 12.02, 12.01, 9.45, 6, 5, 4, 3, 2, 2, 1, 1, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "fOVj9RrbWM7b", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "c27fcfcb-2468-4660-fc7c-426544e171e7" + }, + "cell_type": "code", + "source": [ + "x = 200\n", + "dummy_list.remove(x) # remove first occurrence of 'x' in the list\n", + "print(dummy_list)" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[90, 45.67, 16, 12.02, 12.01, 9.45, 6, 5, 4, 3, 2, 2, 1, 1, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "3QgQK_efW1Aj", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "43841b69-1bd5-478e-849e-267651deccbd" + }, + "cell_type": "code", + "source": [ + "del dummy_list[4] # delete element at position 4\n", + "print(dummy_list)" + ], + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[90, 45.67, 16, 12.02, 5, 4, 3, 2, 2, 1, 1, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "kM2R32bxXT0K", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 171 + }, + "outputId": "d1ca4b44-a5c3-44c8-a850-1ca1dba6400b" + }, + "cell_type": "code", + "source": [ + "# attempting to delete element at position length + 1\n", + "del dummy_list[len(dummy_list) + 1]" + ], + "execution_count": 19, + "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[0;32m----> 1\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0mdummy_list\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[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range" + ] + } + ] + }, + { + "metadata": { + "id": "Kh9lTau7XVIM", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "8a374a5b-1b9b-4771-dc6f-52729b403e78" + }, + "cell_type": "code", + "source": [ + "# cleared the list\n", + "dummy_list.clear()\n", + "print(dummy_list)" + ], + "execution_count": 20, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/NumPy_Exercises.ipynb b/NumPy_Exercises.ipynb new file mode 100644 index 0000000..5acc5d3 --- /dev/null +++ b/NumPy_Exercises.ipynb @@ -0,0 +1,327 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "NumPy Exercises ", + "version": "0.3.2", + "provenance": [], + "collapsed_sections": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/nabhoneel/Assignment-2/blob/nabhoneel/NumPy_Exercises.ipynb)" + ] + }, + { + "metadata": { + "id": "ibRSjDA1YUiF", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "import numpy as np" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "J2uAs0dPZJDP", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 253 + }, + "outputId": "3b2439ca-2689-4558-b4bc-7364167ed061" + }, + "cell_type": "code", + "source": [ + "# create a uniform subdivision of the interval -1.3 to 2.5 with 64 subdivisions\n", + "np.linspace(-1.3, 2.5, 64)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([-1.3 , -1.23968254, -1.17936508, -1.11904762, -1.05873016,\n", + " -0.9984127 , -0.93809524, -0.87777778, -0.81746032, -0.75714286,\n", + " -0.6968254 , -0.63650794, -0.57619048, -0.51587302, -0.45555556,\n", + " -0.3952381 , -0.33492063, -0.27460317, -0.21428571, -0.15396825,\n", + " -0.09365079, -0.03333333, 0.02698413, 0.08730159, 0.14761905,\n", + " 0.20793651, 0.26825397, 0.32857143, 0.38888889, 0.44920635,\n", + " 0.50952381, 0.56984127, 0.63015873, 0.69047619, 0.75079365,\n", + " 0.81111111, 0.87142857, 0.93174603, 0.99206349, 1.05238095,\n", + " 1.11269841, 1.17301587, 1.23333333, 1.29365079, 1.35396825,\n", + " 1.41428571, 1.47460317, 1.53492063, 1.5952381 , 1.65555556,\n", + " 1.71587302, 1.77619048, 1.83650794, 1.8968254 , 1.95714286,\n", + " 2.01746032, 2.07777778, 2.13809524, 2.1984127 , 2.25873016,\n", + " 2.31904762, 2.37936508, 2.43968254, 2.5 ])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 5 + } + ] + }, + { + "metadata": { + "id": "SyeIT1vZaYXe", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + }, + "outputId": "5502a6e2-2c3c-4e67-a9b5-6d5a5487ade2" + }, + "cell_type": "code", + "source": [ + "# Generate an array of length 3n filled with the cyclic pattern 1, 2, 3\n", + "np.resize(np.array([1, 2, 3]), 30)" + ], + "execution_count": 12, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1,\n", + " 2, 3, 1, 2, 3, 1, 2, 3])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 12 + } + ] + }, + { + "metadata": { + "id": "8jFQNXUOdwTk", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "c5afe067-2e2c-4934-f118-0418ff31d2a6" + }, + "cell_type": "code", + "source": [ + "# Create an array of the first 10 odd integers\n", + "np.arange(1, 10 * 2 + 1, 2)" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 14 + } + ] + }, + { + "metadata": { + "id": "uVm8Z_tqeGvI", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "ffd90ce8-5573-4fd1-bb16-c6244eb4314d" + }, + "cell_type": "code", + "source": [ + "# find intersection of a and b\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])\n", + "\n", + "np.intersect1d(a, b)" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([2, 4])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 15 + } + ] + }, + { + "metadata": { + "id": "mxffY6DgeYfx", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + }, + "outputId": "0bbc4ba0-8a90-4157-bb82-bef7e618045c" + }, + "cell_type": "code", + "source": [ + "# reshape 1D array to 2D array of size 2x5\n", + "a = np.arange(10)\n", + "a.reshape(2, 5)" + ], + "execution_count": 16, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[0, 1, 2, 3, 4],\n", + " [5, 6, 7, 8, 9]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 16 + } + ] + }, + { + "metadata": { + "id": "q9sYIsC6ejZi", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "8d593125-43c7-46ba-f0cb-0b38aed5edc7" + }, + "cell_type": "code", + "source": [ + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "np.array(a) # list to numpy array\n", + "np.array(a).tolist() # numpy array to list" + ], + "execution_count": 27, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 27 + } + ] + }, + { + "metadata": { + "id": "k9q8AU2hfz17", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 235 + }, + "outputId": "7e34c1b3-06bf-4251-be45-8b8c33ddf78b" + }, + "cell_type": "code", + "source": [ + "# Create a 10 x 10 arrays of zeros and then \"frame\" it with a border of ones.\n", + "\n", + "bordered_array = np.zeros(shape = (10, 10))\n", + "bordered_array = np.pad(bordered_array, (1, 1), 'constant', constant_values=(1, 1)) # framing segment\n", + "\n", + "print(bordered_array)" + ], + "execution_count": 47, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[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" + } + ] + }, + { + "metadata": { + "id": "eJ7rub5Fi0tD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 162 + }, + "outputId": "3b1a7673-e97d-4a09-b726-4fb4d2ee674d" + }, + "cell_type": "code", + "source": [ + "# checkerboard style with slice and stride\n", + "\n", + "checkerboard = np.ones(shape = (8, 8))\n", + "\n", + "checkerboard[0::2, 1:8:2] = 0\n", + "checkerboard[1::2, 0:8:2] = 0\n", + "\n", + "print(checkerboard)" + ], + "execution_count": 88, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[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.]]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/Numpy_Examples_1.ipynb b/Numpy_Examples_1.ipynb new file mode 100644 index 0000000..b5cd205 --- /dev/null +++ b/Numpy_Examples_1.ipynb @@ -0,0 +1,504 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "view-in-github" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/nabhoneel/Assignment-2/blob/nabhoneel/Numpy_Examples_1.ipynb)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "3pSVAeWfuPcq" + }, + "source": [ + "# Numpy Examples\n", + "\n", + "## What is numpy?\n", + "\n", + "#### Python has built-in:\n", + "\n", + "- containers: lists (costless insertion and append), dictionnaries (fast lookup)\n", + "- high-level number objects: integers, floating point\n", + "\n", + "#### Numpy is:\n", + "\n", + " - extension package to Python for multidimensional arrays\n", + " - closer to hardware (efficiency)\n", + " - designed for scientific computation (convenience)\n", + "\n", + "\n", + "#### Import numpy\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "ozUi4_X55UHE" + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "3-1ghFDF5N2z" + }, + "source": [ + "### Uncomment Print statement and run each cell to see the output\n", + "\n", + "#### Create numpy arrays\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "atYpk2ert0b-" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3]\n", + "\n", + "(2, 3)\n", + "1 2 4\n" + ] + } + ], + "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])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "Kro5ZOwXue5n" + }, + "source": [ + "#### Some basic functions for creating arrays. Print all the defined arrays and see the results." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "V3rdzgr9uhHS" + }, + "outputs": [ + { + "name": "stdout", + "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.96713436 0.12079029]\n", + " [0.68911551 0.5792308 ]]\n" + ] + } + ], + "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)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "8RPW_SutukjF" + }, + "source": [ + "#### Execute and understand :)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "-8JuqYt4upeo" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0. 0.]\n", + " [0. 0.]]\n", + "[[1. 1. 1.]\n", + " [1. 1. 1.]\n", + " [1. 1. 1.]]\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.\n", + " \"\"\"Entry point for launching an IPython kernel.\n", + "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:2: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.\n", + " \n" + ] + } + ], + "source": [ + "a == np.arange(10)\n", + "b == np.linspace(0,10, num=6)\n", + "print(a)\n", + "print(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "MRHhbjx4uvYN" + }, + "source": [ + "#### Array Indexing" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "grF5_yUSuxVK" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "77\n" + ] + } + ], + "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\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "s400Gijxu0kO" + }, + "source": [ + "#### Slicing" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "kubpegh2u4zF" + }, + "outputs": [ + { + "name": "stdout", + "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" + ] + } + ], + "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)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "TmGnCO3AvE8t" + }, + "source": [ + "#### Aritmetic operations" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "YvBw3ImjvGqD" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "[4 6]\n", + "[3 7]\n" + ] + } + ], + "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]\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "uaVY3ZzD4pC2" + }, + "source": [ + "#### Using Boolean Mask" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "-PNfOMvh4_Gp" + }, + "outputs": [ + { + "name": "stdout", + "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" + ] + } + ], + "source": [ + "b = np.arange(10)\n", + "\n", + "print(b)\n", + "\n", + "mask = b%2!=0 #perform computations on the list \n", + "\n", + "print(mask)\n", + "\n", + "print(b[mask]) #applying the mask on the numpy array\n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "HbEPBbz-5J9K" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 0 -1 2 -1 4 -1 6 -1 8 -1]\n" + ] + } + ], + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "print(modified_b)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "zgSd71EEAHC7" + }, + "source": [ + "#### Swapping two columns in a 2d numpy array" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "-cvqeXd_AGo1" + }, + "outputs": [ + { + "name": "stdout", + "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" + ] + } + ], + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[:, [1,0,2]])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "U7ifiLY3Ayky" + }, + "source": [ + "#### Swapping two rows in a 2d numpy array" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "0FrOURRDAZNP" + }, + "outputs": [ + { + "name": "stdout", + "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" + ] + } + ], + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[[1,0,2], :])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "include_colab_link": true, + "name": "Numpy_Examples 1.ipynb", + "provenance": [], + "version": "0.3.2" + }, + "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.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +}