diff --git a/Lists.ipynb b/Lists.ipynb new file mode 100644 index 0000000..4883ff0 --- /dev/null +++ b/Lists.ipynb @@ -0,0 +1,393 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Lists.ipynb", + "version": "0.3.2", + "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": [ + "[View in Colaboratory](https://colab.research.google.com/github/sumitra1/Assignment-2/blob/sumitra1/Lists.ipynb)" + ] + }, + { + "metadata": { + "id": "e0R1W0Vzm4UU", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Exercise - List" + ] + }, + { + "metadata": { + "id": "TrO7XNQnnQZ7", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "1) Create any random list and assign it to a variable dummy_list" + ] + }, + { + "metadata": { + "id": "bjl-2QkznWid", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "dummy_list = [84,65.6,65,8.3,80.83,3.2,74.73,74,99.99,9.5]" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "cDjddNGfngnp", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "2) print dummy_list" + ] + }, + { + "metadata": { + "id": "RVL5178inz9M", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "22774ca7-934e-4958-ad45-b8cbc7410142" + }, + "cell_type": "code", + "source": [ + "print(dummy_list)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[84, 65.6, 65, 8.3, 80.83, 3.2, 74.73, 74, 99.99, 9.5]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "15jKDXxkn16M", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "3) Reverse dummy_list and print" + ] + }, + { + "metadata": { + "id": "bYa9gFOOn-4o", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "8afe6cd6-6699-413b-f733-e68a820f160a" + }, + "cell_type": "code", + "source": [ + "dummy_list.reverse()\n", + "print(dummy_list)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[9.5, 99.99, 74, 74.73, 3.2, 80.83, 8.3, 65, 65.6, 84]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "EShv0nfXpUys", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "4) Add the list dummy_list_2 to the previous dummy_list and now print dummy_list" + ] + }, + { + "metadata": { + "id": "Ngkc7hnYphg6", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "19d85b35-c291-42f7-a92c-5536b9f9bef7" + }, + "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_2\n", + "print(dummy_list)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[9.5, 99.99, 74, 74.73, 3.2, 80.83, 8.3, 65, 65.6, 84, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Le1aRTuYoDzS", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "5) Create a dictionary named dummy_dict which contains all the elements of dummy_list as keys and frequency as values. " + ] + }, + { + "metadata": { + "id": "VHfSR_Csthnk", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "dummy_dict = {}" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "rtPC6WkSos-y", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "\n", + "for item in range(len(dummy_list)):\n", + " dummy_dict[dummy_list[item]] = dummy_list.count(dummy_list[item])\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "RgCYpFXGou6q", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "6) print dummy_dict" + ] + }, + { + "metadata": { + "id": "qe5E5IgxpTWU", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "outputId": "564a1b7b-37af-42d3-9146-653a2e903df1" + }, + "cell_type": "code", + "source": [ + "print(dummy_dict)\n" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{9.5: 1, 99.99: 1, 74: 1, 74.73: 1, 3.2: 1, 80.83: 1, 8.3: 1, 65: 1, 65.6: 1, 84: 1, 2: 1, 200: 1, 16: 1, 4: 1, 1: 1, 0: 1, 9.45: 1, 45.67: 1, 90: 1, 12.01: 1, 12.02: 1}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "8n_nsBDup4--", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "7) Sort dummy_list in ascending order as well as descending order and print the changed lists " + ] + }, + { + "metadata": { + "id": "Z_m7vr26qKnK", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "a65d6fa7-e8c2-4591-9e0c-f0a20e58280f" + }, + "cell_type": "code", + "source": [ + "dummy_list.sort()\n", + "print(dummy_list)\n", + "dummy_list.reverse()\n", + "print(dummy_list)" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 1, 2, 3.2, 4, 8.3, 9.45, 9.5, 12.01, 12.02, 16, 45.67, 65, 65.6, 74, 74.73, 80.83, 84, 90, 99.99, 200]\n", + "[200, 99.99, 90, 84, 80.83, 74.73, 74, 65.6, 65, 45.67, 16, 12.02, 12.01, 9.5, 9.45, 8.3, 4, 3.2, 2, 1, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Znm5Qo4LqPKA", + "colab_type": "text" + }, + "cell_type": "markdown", + "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." + ] + }, + { + "metadata": { + "id": "1-8mlngDqYvS", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 231 + }, + "outputId": "1c84ca38-f79d-4431-b30b-e64bfc29e837" + }, + "cell_type": "code", + "source": [ + "x = 200\n", + "#dummy_list.insert(0,200)\n", + "dummy_list.remove(x)\n", + "print(dummy_list)\n", + "\n", + "# Let's play: try the same with something which is not in the list to get the ValueError\n", + "dummy_list.remove(200)\n", + "# 200 is not in the dummy_list , so we get error\n" + ], + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[99.99, 90, 84, 80.83, 74.73, 74, 65.6, 65, 45.67, 16, 12.02, 12.01, 9.5, 9.45, 8.3, 4, 3.2, 2, 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 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\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----> 7\u001b[0;31m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m200\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;31m# 200 is not in the dummy_list , so we get error\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" + ] + } + ] + }, + { + "metadata": { + "id": "QPB6iGbeqviN", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "9) Remove the item at position x. x is any random integer" + ] + }, + { + "metadata": { + "id": "aMyo1gmRrVHo", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "bqQnnsr8rm6G", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "10) Let's clean everything clear the list and then print" + ] + }, + { + "metadata": { + "id": "qBC8lKpLrtJW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "8d327a40-1298-4a35-9b88-3e8dce54024f" + }, + "cell_type": "code", + "source": [ + "dummy_list.clear()\n", + "print(dummy_list)" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[]\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..b17c64d --- /dev/null +++ b/Numpy_Examples_1.ipynb @@ -0,0 +1,513 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Numpy_Examples 1.ipynb", + "version": "0.3.2", + "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" + ] + }, + { + "metadata": { + "id": "3pSVAeWfuPcq", + "colab_type": "text" + }, + "cell_type": "markdown", + "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" + ] + }, + { + "metadata": { + "id": "ozUi4_X55UHE", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "import numpy as np" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "3-1ghFDF5N2z", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "### Uncomment Print statement and run each cell to see the output\n", + "\n", + "#### Create numpy arrays\n" + ] + }, + { + "metadata": { + "id": "atYpk2ert0b-", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "ac6f6604-0d6e-4884-9b96-3f9a66d7bd28" + }, + "cell_type": "code", + "source": [ + "a = np.array([1, 2, 3])\n", + "print(a)\n", + "print(type(a))\n", + "\n", + "b = np.array([[1,2,3],[4,5,6]])\n", + "print(b.shape) \n", + "print(b[0, 0], b[0, 1], b[1, 0])" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3]\n", + "\n", + "(2, 3)\n", + "1 2 4\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Kro5ZOwXue5n", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Some basic functions for creating arrays. Print all the defined arrays and see the results." + ] + }, + { + "metadata": { + "id": "V3rdzgr9uhHS", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 272 + }, + "outputId": "e64cbeb9-b332-428b-9d59-10aef956b123" + }, + "cell_type": "code", + "source": [ + "a = np.zeros(shape=(2,2))\n", + "b = np.ones(shape = (3,3))\n", + "c = np.eye(5)\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)\n", + "\n" + ], + "execution_count": 7, + "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. 0. 0. 0.]\n", + " [0. 1. 0. 0. 0.]\n", + " [0. 0. 1. 0. 0.]\n", + " [0. 0. 0. 1. 0.]\n", + " [0. 0. 0. 0. 1.]]\n", + "d [[5 5 5]\n", + " [5 5 5]\n", + " [5 5 5]]\n", + "e [[0.8083337 0.99715686]\n", + " [0.3363978 0.32292771]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "8RPW_SutukjF", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Execute and understand :)" + ] + }, + { + "metadata": { + "id": "-8JuqYt4upeo", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 170 + }, + "outputId": "ca7d27c4-da3c-4426-8ce2-3ae300774a17" + }, + "cell_type": "code", + "source": [ + "a == np.arange(10)\n", + "b == np.linspace(0,10, num=6)\n", + "print(a)\n", + "print(b)" + ], + "execution_count": 8, + "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" + } + ] + }, + { + "metadata": { + "id": "MRHhbjx4uvYN", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Array Indexing" + ] + }, + { + "metadata": { + "id": "grF5_yUSuxVK", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "5db919e7-3f26-4061-fa55-e0fa36e7893b" + }, + "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])\n", + "\n", + "b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]\n", + "print(a[0, 1]) \n" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n", + "77\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "s400Gijxu0kO", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Slicing" + ] + }, + { + "metadata": { + "id": "kubpegh2u4zF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "07d56fa2-10b6-4609-d41c-63a7f686d64e" + }, + "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": 14, + "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": "TmGnCO3AvE8t", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Aritmetic operations" + ] + }, + { + "metadata": { + "id": "YvBw3ImjvGqD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "d1f41bc1-4838-44ed-8990-d5cfbf44debe" + }, + "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": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10\n", + "[4 6]\n", + "[3 7]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "uaVY3ZzD4pC2", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Using Boolean Mask" + ] + }, + { + "metadata": { + "id": "-PNfOMvh4_Gp", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "ba6765f6-b6e2-442c-8589-1d9c231ad4b4" + }, + "cell_type": "code", + "source": [ + "b = np.arange(10)\n", + "\n", + "print(b)\n", + "\n", + "mask = b%2!=0 #perform computations on the list \n", + "#checking the condition given above\n", + "\n", + "print(mask)\n", + "\n", + "print(b[mask]) #applying the mask on the numpy array" + ], + "execution_count": 15, + "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": "HbEPBbz-5J9K", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "#print(modified_b)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "zgSd71EEAHC7", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Swapping two columns in a 2d numpy array" + ] + }, + { + "metadata": { + "id": "-cvqeXd_AGo1", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "adb8476b-1b89-4347-b5a9-4e7212544cc9" + }, + "cell_type": "code", + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "print(modified_b)" + ], + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 0 -1 2 -1 4 -1 6 -1 8 -1]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "U7ifiLY3Ayky", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "#### Swapping two rows in a 2d numpy array" + ] + }, + { + "metadata": { + "id": "0FrOURRDAZNP", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "57c3bfa8-f083-4b38-da9a-a9fa11f44f11" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "# arrenging the order of column by given index \n", + "print(a[:, [1,0,2]])" + ], + "execution_count": 17, + "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" + } + ] + } + ] +} \ No newline at end of file diff --git a/Numpy_Exercises.ipynb b/Numpy_Exercises.ipynb new file mode 100644 index 0000000..3da9d47 --- /dev/null +++ b/Numpy_Exercises.ipynb @@ -0,0 +1,382 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Numpy_Exercises.ipynb", + "version": "0.3.2", + "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" + ] + }, + { + "metadata": { + "id": "a_4UupTr9fbX", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "# Numpy Exercises\n", + "\n", + "1) Create a uniform subdivision of the interval -1.3 to 2.5 with 64 subdivisions" + ] + }, + { + "metadata": { + "id": "LIP5u4zi0Nmg", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "import numpy as np #import numpy" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "dBoH_A7M9jjL", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "2) Generate an array of length 3n filled with the cyclic pattern 1, 2, 3" + ] + }, + { + "metadata": { + "id": "4TxT66309n1o", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "c5af0e96-5796-4139-c68f-9d523b3464b6" + }, + "cell_type": "code", + "source": [ + "x = np.array([1,2,3])\n", + "y = np.resize(x,12)\n", + "\n", + "print(y)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3 1 2 3 1 2 3 1 2 3]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Vh-UKizx9oTp", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "3) Create an array of the first 10 odd integers." + ] + }, + { + "metadata": { + "id": "ebhEUZq29r32", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "7fe10eac-4bde-4110-ebb5-660ba75545c5" + }, + "cell_type": "code", + "source": [ + "b = np.arange(1,20,2)\n", + "print(b)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 1 3 5 7 9 11 13 15 17 19]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "QfJRdMat90f4", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "4) Find intersection of a and b" + ] + }, + { + "metadata": { + "id": "gOlfuJCo-JwF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "c83996db-13be-404c-9ac9-f4e889317524" + }, + "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])\n", + "\n", + "c = np.intersect1d(a,b)\n", + "print(c)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[2 4]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "RtVCf0UoCeB8", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "5) Reshape 1d array a to 2d array of 2X5" + ] + }, + { + "metadata": { + "id": "2E8b55_2Cjx5", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "e3743b1b-a7db-4d01-ac59-7780c86f1124" + }, + "cell_type": "code", + "source": [ + "\n", + "a = np.arange(10)\n", + "print(a)\n", + "print('---------')\n", + "a = np.arange(10).reshape(2,5)\n", + "print(a)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0 1 2 3 4 5 6 7 8 9]\n", + "---------\n", + "[[0 1 2 3 4]\n", + " [5 6 7 8 9]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "dVrSBW1zEjp2", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "6) Create a numpy array to list and vice versa" + ] + }, + { + "metadata": { + "id": "tcBCyhXPEp9C", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "16e8d270-7326-4818-a2f7-d926a2e09a5e" + }, + "cell_type": "code", + "source": [ + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "a = np.array(a)\n", + "print(a)\n", + "print(type(a))\n", + "\n", + "a = list(a)\n", + "print(a)\n", + "print(type(a))\n" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3 4 5 6 7 8 9]\n", + "\n", + "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "JNqX8wnz9sQJ", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "7) Create a 10 x 10 arrays of zeros and then \"frame\" it with a border of ones." + ] + }, + { + "metadata": { + "id": "4bjP3JAc9vRD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 374 + }, + "outputId": "044340a9-c2ec-4a53-d766-6eb95d717047" + }, + "cell_type": "code", + "source": [ + "a = np.zeros((10,10))\n", + "print(a)\n", + "print(' ***** with frame *****')\n", + "a[0] = a[9] = 1\n", + "a[:,0] = a[:,9] = 1\n", + "print(a)" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]\n", + " ***** with frame *****\n", + "[[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" + } + ] + }, + { + "metadata": { + "id": "xaQgf8tT9v-n", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "8) Create an 8 x 8 array with a checkerboard pattern of zeros and ones using a slicing+striding approach." + ] + }, + { + "metadata": { + "id": "No7fx0Xy9zEh", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 204 + }, + "outputId": "f538ac16-282f-4e1f-b0da-56395a18ab7b" + }, + "cell_type": "code", + "source": [ + "chess = np.arange(64)\n", + "\n", + "mask1 = chess%2 != 0\n", + "mask2 = chess%2 == 0\n", + "\n", + "new_chess = chess\n", + "new_chess[mask1] = 1\n", + "new_chess[mask2] = 0\n", + "\n", + "print(new_chess)\n", + "\n", + "a0 = new_chess[:8]\n", + "a1 = new_chess[8:16]\n", + "a2 = new_chess[16:24]\n", + "a3 = new_chess[24:32]\n", + "a4 = new_chess[32:40]\n", + "a5 = new_chess[40:48]\n", + "a6 = new_chess[48:56]\n", + "a7 = new_chess[56:64]\n", + "\n", + "print(' ***** Chess board *****')\n", + "x = np.array([a0,a1[::-1],a2,a3[::-1],a4,a5[::-1],a6,a7[::-1]])\n", + "print(x)\n" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n", + " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1]\n", + " ***** Chess board *****\n", + "[[0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file