diff --git a/Copy_of_Excercise_numpy_.ipynb b/Copy_of_Excercise_numpy_.ipynb new file mode 100644 index 0000000..3afb816 --- /dev/null +++ b/Copy_of_Excercise_numpy_.ipynb @@ -0,0 +1,375 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Copy of Excercise numpy .ipynb", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a_4UupTr9fbX", + "colab_type": "text" + }, + "source": [ + "# Numpy Exercises\n", + "\n", + "1) Create a uniform subdivision of the interval -1.3 to 2.5 with 64 subdivisions" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "LIP5u4zi0Nmg", + "colab_type": "code", + "outputId": "e52b5c12-ebfd-48ac-a9a1-a97503a1a143", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 202 + } + }, + "source": [ + "import numpy as np #import numpy\n", + "arr1=np.linspace(-1.3,2.5,64)\n", + "print(arr1)" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[-1.3 -1.23968254 -1.17936508 -1.11904762 -1.05873016 -0.9984127\n", + " -0.93809524 -0.87777778 -0.81746032 -0.75714286 -0.6968254 -0.63650794\n", + " -0.57619048 -0.51587302 -0.45555556 -0.3952381 -0.33492063 -0.27460317\n", + " -0.21428571 -0.15396825 -0.09365079 -0.03333333 0.02698413 0.08730159\n", + " 0.14761905 0.20793651 0.26825397 0.32857143 0.38888889 0.44920635\n", + " 0.50952381 0.56984127 0.63015873 0.69047619 0.75079365 0.81111111\n", + " 0.87142857 0.93174603 0.99206349 1.05238095 1.11269841 1.17301587\n", + " 1.23333333 1.29365079 1.35396825 1.41428571 1.47460317 1.53492063\n", + " 1.5952381 1.65555556 1.71587302 1.77619048 1.83650794 1.8968254\n", + " 1.95714286 2.01746032 2.07777778 2.13809524 2.1984127 2.25873016\n", + " 2.31904762 2.37936508 2.43968254 2.5 ]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dBoH_A7M9jjL", + "colab_type": "text" + }, + "source": [ + "2) Generate an array of length 3n filled with the cyclic pattern 1, 2, 3" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "4TxT66309n1o", + "colab_type": "code", + "outputId": "ee2c0dcf-ed07-49d0-a4f6-9701d986cf81", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "arr2=np.arange(1,4)\n", + "print(arr2)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Vh-UKizx9oTp", + "colab_type": "text" + }, + "source": [ + "3) Create an array of the first 10 odd integers." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ebhEUZq29r32", + "colab_type": "code", + "outputId": "ab79b833-9ae8-457b-89b5-81f88f718864", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "arr3=np.arange(20)\n", + "oddArr=arr3[arr3%2!=0]\n", + "print(oddArr)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 1 3 5 7 9 11 13 15 17 19]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QfJRdMat90f4", + "colab_type": "text" + }, + "source": [ + "4) Find intersection of a and b" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "gOlfuJCo-JwF", + "colab_type": "code", + "outputId": "ed551b1a-7f2e-4b29-bd91-334b0404d156", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "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])\n", + "intersect=set(a) & set(b)\n", + "print(intersect)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{2, 4}\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RtVCf0UoCeB8", + "colab_type": "text" + }, + "source": [ + "5) Reshape 1d array a to 2d array of 2X5" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "2E8b55_2Cjx5", + "colab_type": "code", + "outputId": "cc7d88b8-7b4b-46c6-ecd1-412da9951e8e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 50 + } + }, + "source": [ + "a = np.arange(10).reshape(2,5)\n", + "print(a)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0 1 2 3 4]\n", + " [5 6 7 8 9]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dVrSBW1zEjp2", + "colab_type": "text" + }, + "source": [ + "6) Create a numpy array to list and vice versa" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tcBCyhXPEp9C", + "colab_type": "code", + "outputId": "960843ba-c7ed-4d65-dde7-7b7cc19dfdbc", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 50 + } + }, + "source": [ + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "a=np.array(a)\n", + "print(a,type(a))\n", + "a=list(a)\n", + "print(a,type(a))" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3 4 5 6 7 8 9] \n", + "[1, 2, 3, 4, 5, 6, 7, 8, 9] \n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JNqX8wnz9sQJ", + "colab_type": "text" + }, + "source": [ + "7) Create a 10 x 10 arrays of zeros and then \"frame\" it with a border of ones." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "4bjP3JAc9vRD", + "colab_type": "code", + "outputId": "0c988f22-eb38-4aae-acad-1def7b27752c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 185 + } + }, + "source": [ + "zeroArr=np.zeros((10,10),dtype=int)\n", + "zeroArr[:,[0,-1]]=1\n", + "zeroArr[[0,-1],:]=1\n", + "print(zeroArr)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[1 1 1 1 1 1 1 1 1 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 0 0 0 0 0 0 0 0 1]\n", + " [1 1 1 1 1 1 1 1 1 1]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xaQgf8tT9v-n", + "colab_type": "text" + }, + "source": [ + "8) Create an 8 x 8 array with a checkerboard pattern of zeros and ones using a slicing+striding approach." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "No7fx0Xy9zEh", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 151 + }, + "outputId": "8d45c65f-97bb-46ef-9900-aed59453a2c9" + }, + "source": [ + "chess=np.zeros((8,8),dtype=int)\n", + "chess[1::2,1::2]=1\n", + "chess[::2,::2]=1\n", + "chess\n" + ], + "execution_count": 2, + "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": 2 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tlOWop0MlZIp", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/Questions/List 1.ipynb b/Questions/List 1.ipynb index 144d52a..1ef665f 100644 --- a/Questions/List 1.ipynb +++ b/Questions/List 1.ipynb @@ -1 +1,414 @@ -{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"List 1.ipynb","provenance":[],"authorship_tag":"ABX9TyNLahDgX6PyIABRXbqfMC/+"},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"e0R1W0Vzm4UU","colab_type":"text"},"source":["# Exercise - List"]},{"cell_type":"markdown","metadata":{"id":"TrO7XNQnnQZ7","colab_type":"text"},"source":["1) Create any random list and assign it to a variable dummy_list"]},{"cell_type":"code","metadata":{"id":"bjl-2QkznWid","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"cDjddNGfngnp","colab_type":"text"},"source":["2) print dummy_list"]},{"cell_type":"code","metadata":{"id":"RVL5178inz9M","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"15jKDXxkn16M","colab_type":"text"},"source":["3) Reverse dummy_list and print"]},{"cell_type":"code","metadata":{"id":"bYa9gFOOn-4o","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"EShv0nfXpUys","colab_type":"text"},"source":["4) Add the list dummy_list_2 to the previous dummy_list and now print dummy_list"]},{"cell_type":"code","metadata":{"id":"Ngkc7hnYphg6","colab_type":"code","colab":{}},"source":["dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Le1aRTuYoDzS","colab_type":"text"},"source":["5) Create a dictionary named dummy_dict which contains all the elements of dummy_list as keys and frequency as values. "]},{"cell_type":"code","metadata":{"id":"VHfSR_Csthnk","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"rtPC6WkSos-y","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"RgCYpFXGou6q","colab_type":"text"},"source":["6) print dummy_dict"]},{"cell_type":"code","metadata":{"id":"qe5E5IgxpTWU","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"8n_nsBDup4--","colab_type":"text"},"source":["7) Sort dummy_list in ascending order as well as descending order and print the changed lists "]},{"cell_type":"code","metadata":{"id":"Z_m7vr26qKnK","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Znm5Qo4LqPKA","colab_type":"text"},"source":["8) Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item."]},{"cell_type":"code","metadata":{"id":"1-8mlngDqYvS","colab_type":"code","colab":{}},"source":["x = 200\n","\n","# Let's play: try the same with something which is not in the list to get the ValueError"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"QPB6iGbeqviN","colab_type":"text"},"source":["9) Remove the item at position x. x is any random integer"]},{"cell_type":"code","metadata":{"id":"aMyo1gmRrVHo","colab_type":"code","colab":{}},"source":["# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"bqQnnsr8rm6G","colab_type":"text"},"source":["10) Let's clean everything clear the list and then print"]},{"cell_type":"code","metadata":{"id":"qBC8lKpLrtJW","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} \ No newline at end of file +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "List 1.ipynb", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "e0R1W0Vzm4UU", + "colab_type": "text" + }, + "source": [ + "# Exercise - List" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TrO7XNQnnQZ7", + "colab_type": "text" + }, + "source": [ + "1) Create any random list and assign it to a variable dummy_list" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bjl-2QkznWid", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import random\n", + "dummy_list=random.sample(range(1,11),10)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cDjddNGfngnp", + "colab_type": "text" + }, + "source": [ + "2) print dummy_list" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "RVL5178inz9M", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "a48b6c56-637f-4f8b-e430-6d66d76e586d" + }, + "source": [ + "print(dummy_list)" + ], + "execution_count": 29, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[10, 8, 5, 3, 4, 2, 7, 9, 1, 6]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "15jKDXxkn16M", + "colab_type": "text" + }, + "source": [ + "3) Reverse dummy_list and print" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bYa9gFOOn-4o", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "23c604d4-ff1a-4c94-aee9-edd7d692ae28" + }, + "source": [ + "\n", + "dummy_list.reverse()\n", + "print(dummy_list)" + ], + "execution_count": 30, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[6, 1, 9, 7, 2, 4, 3, 5, 8, 10]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EShv0nfXpUys", + "colab_type": "text" + }, + "source": [ + "4) Add the list dummy_list_2 to the previous dummy_list and now print dummy_list" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Ngkc7hnYphg6", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "16069c7f-a9c5-4af7-f709-c5ed53360f14" + }, + "source": [ + "dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "dummy_list+=dummy_list_2\n", + "print(dummy_list)" + ], + "execution_count": 31, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[6, 1, 9, 7, 2, 4, 3, 5, 8, 10, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Le1aRTuYoDzS", + "colab_type": "text" + }, + "source": [ + "5) Create a dictionary named dummy_dict which contains all the elements of dummy_list as keys and frequency as values. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VHfSR_Csthnk", + "colab_type": "code", + "colab": {} + }, + "source": [ + "dummy_dict={}\n", + "dcount=[]\n", + "for i in dummy_list:\n", + " dcount.append(dummy_list.count(i))\n", + "for i,j in zip(dummy_list,dcount):\n", + " dummy_dict[i]=j\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RgCYpFXGou6q", + "colab_type": "text" + }, + "source": [ + "6) print dummy_dict" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qe5E5IgxpTWU", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "ee68105e-4428-4f18-b933-58683286bff5" + }, + "source": [ + "print(dummy_dict)" + ], + "execution_count": 33, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{6: 1, 1: 2, 9: 1, 7: 1, 2: 2, 4: 2, 3: 1, 5: 1, 8: 1, 10: 1, 200: 1, 16: 1, 0: 1, 9.45: 1, 45.67: 1, 90: 1, 12.01: 1, 12.02: 1}\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8n_nsBDup4--", + "colab_type": "text" + }, + "source": [ + "7) Sort dummy_list in ascending order as well as descending order and print the changed lists " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Z_m7vr26qKnK", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 50 + }, + "outputId": "8b4ba0cd-c07e-49f2-ee4e-1eefa96a2f28" + }, + "source": [ + "dummy_list.sort()\n", + "print(dummy_list)\n", + "dummy_list.reverse()\n", + "print(dummy_list)" + ], + "execution_count": 34, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 2, 3, 4, 4, 5, 6, 7, 8, 9, 9.45, 10, 12.01, 12.02, 16, 45.67, 90, 200]\n", + "[200, 90, 45.67, 16, 12.02, 12.01, 10, 9.45, 9, 8, 7, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Znm5Qo4LqPKA", + "colab_type": "text" + }, + "source": [ + "8) Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1-8mlngDqYvS", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 229 + }, + "outputId": "fcc4faca-066b-429e-aff4-94f45ae256fa" + }, + "source": [ + "x = 200\n", + "y=11\n", + "dummy_list.remove(x)\n", + "print(dummy_list)\n", + "# Let's play: try the same with something which is not in the list to get the ValueError\n", + "dummy_list.remove(y)\n", + "print(dummy_list)" + ], + "execution_count": 35, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[90, 45.67, 16, 12.02, 12.01, 10, 9.45, 9, 8, 7, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0]\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[1;32m 4\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[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\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[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\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 7\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[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QPB6iGbeqviN", + "colab_type": "text" + }, + "source": [ + "9) Remove the item at position x. x is any random integer" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "aMyo1gmRrVHo", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 229 + }, + "outputId": "2579243a-9812-4175-dd44-d7fb147dd763" + }, + "source": [ + "x=5\n", + "y=5+len(dummy_list)+1\n", + "dummy_list.remove(dummy_list[x])\n", + "print(dummy_list)\n", + "# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get\n", + "dummy_list.remove(dummy_list[y])\n", + "print(dummy_list)" + ], + "execution_count": 36, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[90, 45.67, 16, 12.02, 12.01, 9.45, 9, 8, 7, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0]\n" + ], + "name": "stdout" + }, + { + "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 4\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[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdummy_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m]\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 7\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[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mIndexError\u001b[0m: list index out of range" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bqQnnsr8rm6G", + "colab_type": "text" + }, + "source": [ + "10) Let's clean everything clear the list and then print" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qBC8lKpLrtJW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "cca07246-b0d6-4c6a-d026-0e8928552cc4" + }, + "source": [ + "dummy_list.clear()\n", + "print(dummy_list)" + ], + "execution_count": 38, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3C_M20WloG-G", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/Questions/Numpy Examples.ipynb b/Questions/Numpy Examples.ipynb index 0182c9b..813c11e 100644 --- a/Questions/Numpy Examples.ipynb +++ b/Questions/Numpy Examples.ipynb @@ -1 +1,525 @@ -{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Numpy Examples.ipynb","provenance":[],"authorship_tag":"ABX9TyPHtZk/+aGJemGCOSs2QFMH"},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"3pSVAeWfuPcq","colab_type":"text"},"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","metadata":{"id":"ozUi4_X55UHE","colab_type":"code","colab":{}},"source":["import numpy as np"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"3-1ghFDF5N2z","colab_type":"text"},"source":["### Uncomment Print statement and run each cell to see the output\n","\n","#### Create numpy arrays\n"]},{"cell_type":"code","metadata":{"id":"atYpk2ert0b-","colab_type":"code","colab":{}},"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":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Kro5ZOwXue5n","colab_type":"text"},"source":["#### Some basic functions for creating arrays. Print all the defined arrays and see the results."]},{"cell_type":"code","metadata":{"id":"V3rdzgr9uhHS","colab_type":"code","colab":{}},"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":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"8RPW_SutukjF","colab_type":"text"},"source":["#### Execute and understand :)"]},{"cell_type":"code","metadata":{"id":"-8JuqYt4upeo","colab_type":"code","colab":{}},"source":["a == np.arange(10)\n","b == np.linspace(0,10, num=6)\n","#print(a)\n","#print(b)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"MRHhbjx4uvYN","colab_type":"text"},"source":["#### Array Indexing"]},{"cell_type":"code","metadata":{"id":"grF5_yUSuxVK","colab_type":"code","colab":{}},"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":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"s400Gijxu0kO","colab_type":"text"},"source":["#### Slicing"]},{"cell_type":"code","metadata":{"id":"kubpegh2u4zF","colab_type":"code","colab":{}},"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":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"TmGnCO3AvE8t","colab_type":"text"},"source":["#### Aritmetic operations"]},{"cell_type":"code","metadata":{"id":"YvBw3ImjvGqD","colab_type":"code","colab":{}},"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":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"uaVY3ZzD4pC2","colab_type":"text"},"source":["#### Using Boolean Mask"]},{"cell_type":"code","metadata":{"id":"-PNfOMvh4_Gp","colab_type":"code","colab":{}},"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"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"HbEPBbz-5J9K","colab_type":"code","colab":{}},"source":["modified_b = b\n","modified_b[mask] = -1\n","\n","#print(modified_b)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"zgSd71EEAHC7","colab_type":"text"},"source":["#### Swapping two columns in a 2d numpy array"]},{"cell_type":"code","metadata":{"id":"-cvqeXd_AGo1","colab_type":"code","colab":{}},"source":["a = np.arange(9).reshape(3,3)\n","#print(a)\n","\n","#print(a[:, [1,0,2]])"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"U7ifiLY3Ayky","colab_type":"text"},"source":["#### Swapping two rows in a 2d numpy array"]},{"cell_type":"code","metadata":{"id":"0FrOURRDAZNP","colab_type":"code","colab":{}},"source":["a = np.arange(9).reshape(3,3)\n","#print(a)\n","\n","#print(arr[[1,0,2], :])"],"execution_count":0,"outputs":[]}]} \ No newline at end of file +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Numpy Examples.ipynb", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "3pSVAeWfuPcq", + "colab_type": "text" + }, + "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", + "metadata": { + "id": "ozUi4_X55UHE", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import numpy as np" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3-1ghFDF5N2z", + "colab_type": "text" + }, + "source": [ + "### Uncomment Print statement and run each cell to see the output\n", + "\n", + "#### Create numpy arrays\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "atYpk2ert0b-", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 84 + }, + "outputId": "7555f5f1-397c-49f0-d007-b89a445de5c2" + }, + "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": 17, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3]\n", + "\n", + "(2, 3)\n", + "1 2 4\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Kro5ZOwXue5n", + "colab_type": "text" + }, + "source": [ + "#### Some basic functions for creating arrays. Print all the defined arrays and see the results." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "V3rdzgr9uhHS", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 218 + }, + "outputId": "bb839b05-2d11-4ef8-f10a-3f8b6bbf4332" + }, + "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": 18, + "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.52927303 0.78056511]\n", + " [0.16269328 0.55977899]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8RPW_SutukjF", + "colab_type": "text" + }, + "source": [ + "#### Execute and understand :)" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-8JuqYt4upeo", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 168 + }, + "outputId": "c61587ea-8d39-40ee-dc3c-7ab228c09698" + }, + "source": [ + "a == np.arange(10)\n", + "b == np.linspace(0,10, num=6)\n", + "print(a)\n", + "print(b)" + ], + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0. 0.]\n", + " [0. 0.]]\n", + "[[1. 1. 1.]\n", + " [1. 1. 1.]\n", + " [1. 1. 1.]]\n" + ], + "name": "stdout" + }, + { + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.6/dist-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", + "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:2: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.\n", + " \n" + ], + "name": "stderr" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MRHhbjx4uvYN", + "colab_type": "text" + }, + "source": [ + "#### Array Indexing" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "grF5_yUSuxVK", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 50 + }, + "outputId": "d653bfb5-75ca-41fe-d044-34e86b49c708" + }, + "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": 20, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n", + "77\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "s400Gijxu0kO", + "colab_type": "text" + }, + "source": [ + "#### Slicing" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "kubpegh2u4zF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 118 + }, + "outputId": "6a5f3b92-9951-4f78-d192-fb218d0c8847" + }, + "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": 21, + "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" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TmGnCO3AvE8t", + "colab_type": "text" + }, + "source": [ + "#### Aritmetic operations" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "YvBw3ImjvGqD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 67 + }, + "outputId": "c30cd499-70d8-4f13-b3fb-6a5a8d201aa0" + }, + "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": 22, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10\n", + "[4 6]\n", + "[3 7]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uaVY3ZzD4pC2", + "colab_type": "text" + }, + "source": [ + "#### Using Boolean Mask" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-PNfOMvh4_Gp", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 67 + }, + "outputId": "b3faa2db-01bc-4072-df5e-fbcc39b45811" + }, + "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" + ], + "execution_count": 23, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0 1 2 3 4 5 6 7 8 9]\n", + "[False True False True False True False True False True]\n", + "[1 3 5 7 9]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "HbEPBbz-5J9K", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "563c80a9-8605-4c98-b0be-ca01bdb927db" + }, + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "print(modified_b)" + ], + "execution_count": 24, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 0 -1 2 -1 4 -1 6 -1 8 -1]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zgSd71EEAHC7", + "colab_type": "text" + }, + "source": [ + "#### Swapping two columns in a 2d numpy array" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-cvqeXd_AGo1", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 118 + }, + "outputId": "63ae0a64-1acc-43ad-d737-4cb65c01ed9a" + }, + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[:, [1,0,2]])" + ], + "execution_count": 25, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0 1 2]\n", + " [3 4 5]\n", + " [6 7 8]]\n", + "[[1 0 2]\n", + " [4 3 5]\n", + " [7 6 8]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "U7ifiLY3Ayky", + "colab_type": "text" + }, + "source": [ + "#### Swapping two rows in a 2d numpy array" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "0FrOURRDAZNP", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 118 + }, + "outputId": "b3027975-acf3-45e0-e63f-9251591648e7" + }, + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[[1,0,2], :])" + ], + "execution_count": 27, + "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" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "F8wd5KY44gYO", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file