From f8d8663781e10cdbfbf42fa19ac8e17eb31f766c Mon Sep 17 00:00:00 2001 From: Subham <40177225+loneWolf148@users.noreply.github.com> Date: Tue, 22 Jan 2019 18:50:20 +0530 Subject: [PATCH 1/8] Created using Colaboratory --- Lists.ipynb | 420 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 420 insertions(+) create mode 100644 Lists.ipynb diff --git a/Lists.ipynb b/Lists.ipynb new file mode 100644 index 0000000..a270312 --- /dev/null +++ b/Lists.ipynb @@ -0,0 +1,420 @@ +{ + "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": [ + "\"Open" + ] + }, + { + "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 = [1,2,45,32,57,3,90,2,3,41]" + ], + "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": "0bb7306d-d8e9-45a0-c29a-3ecbbf299bb6" + }, + "cell_type": "code", + "source": [ + "print(dummy_list)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 2, 45, 32, 57, 3, 90, 2, 3, 41]\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": "d5bedc7f-2f50-4dab-9bd7-79dccbd30e3b" + }, + "cell_type": "code", + "source": [ + "dummy_list.reverse()\n", + "print(dummy_list)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[41, 3, 2, 90, 3, 57, 32, 45, 2, 1]\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": "ec786bd3-23bc-4b07-de50-c907a9aa6e01" + }, + "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.extend(dummy_list_2)\n", + "print(dummy_list)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[41, 3, 2, 90, 3, 57, 32, 45, 2, 1, 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 = {key:dummy_list.count(key) for key in set(dummy_list)}" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "Am8q0wJvgh1h", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "" + ] + }, + { + "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": 34 + }, + "outputId": "2dda8b74-3e16-4d9f-d03c-108536958f91" + }, + "cell_type": "code", + "source": [ + "print(dummy_dict)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{32: 1, 1: 2, 2: 3, 3: 2, 4: 1, 0: 1, 200: 1, 41: 1, 9.45: 1, 12.01: 1, 45: 1, 45.67: 1, 12.02: 1, 16: 1, 57: 1, 90: 2}\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": 50 + }, + "outputId": "89f3053c-326c-48e0-e122-406a7668adcb" + }, + "cell_type": "code", + "source": [ + "ascending_dummy_list = sorted(dummy_list, reverse=False)\n", + "descending_dummy_list = sorted(dummy_list, reverse=True)\n", + "print(ascending_dummy_list)\n", + "print(descending_dummy_list)" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 2, 2, 3, 3, 4, 9.45, 12.01, 12.02, 16, 32, 41, 45, 45.67, 57, 90, 90, 200]\n", + "[200, 90, 90, 57, 45.67, 45, 41, 32, 16, 12.02, 12.01, 9.45, 4, 3, 3, 2, 2, 2, 1, 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": 67 + }, + "outputId": "3472875e-674e-47e6-b533-ba121ca00ecf" + }, + "cell_type": "code", + "source": [ + "x = 200\n", + "print('List Before Removal : '+str(dummy_list))\n", + "dummy_list.remove(x)\n", + "print('List After Removal : '+str(dummy_list))\n", + "\n", + "# Let's play: try the same with something which is not in the list to get the ValueError\n", + "try:\n", + " x = 300\n", + " dummy_list.remove(x)\n", + "except ValueError:\n", + " print('ValueError raise as '+str(x)+' not present in list')" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "List Before Removal : [41, 3, 2, 90, 3, 57, 32, 45, 2, 1, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "List After Removal : [41, 3, 2, 90, 3, 57, 32, 45, 2, 1, 2, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "ValueError raise as 300 not present in list\n" + ], + "name": "stdout" + } + ] + }, + { + "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": { + "base_uri": "https://localhost:8080/", + "height": 212 + }, + "outputId": "6f4a5e2e-1e0d-4843-b61a-73eb472d3399" + }, + "cell_type": "code", + "source": [ + "from random import randint\n", + "x = randint(0,len(dummy_list))\n", + "removed_item = dummy_list[x]\n", + "print(str(removed_item)+' removed')\n", + "dummy_list.pop(x)\n", + "# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get\n", + "dummy_list.pop(len(dummy_list)+5)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2 removed\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 5\u001b[0m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\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----> 7\u001b[0;31m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\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;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m: pop index out of range" + ] + } + ] + }, + { + "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": 84 + }, + "outputId": "763214ef-8641-4c69-847a-33f17674db87" + }, + "cell_type": "code", + "source": [ + "dummy_list.clear()\n", + "ascending_dummy_list.clear()\n", + "descending_dummy_list.clear()\n", + "dummy_dict.clear()\n", + "print(dummy_list)\n", + "print(ascending_dummy_list)\n", + "print(descending_dummy_list)\n", + "print(dummy_dict)" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[]\n", + "[]\n", + "[]\n", + "{}\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From 39bd6444d93cff5ee210babad667c07dfe10891a Mon Sep 17 00:00:00 2001 From: Subham <40177225+loneWolf148@users.noreply.github.com> Date: Tue, 22 Jan 2019 18:51:59 +0530 Subject: [PATCH 2/8] Created using Colaboratory --- Numpy_Examples_1.ipynb | 524 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 524 insertions(+) create mode 100644 Numpy_Examples_1.ipynb diff --git a/Numpy_Examples_1.ipynb b/Numpy_Examples_1.ipynb new file mode 100644 index 0000000..f955712 --- /dev/null +++ b/Numpy_Examples_1.ipynb @@ -0,0 +1,524 @@ +{ + "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": 84 + }, + "outputId": "2b55c78d-6ca9-4baf-b3e7-0062b1b92ccc" + }, + "cell_type": "code", + "source": [ + "a = np.array([1, 2, 3]) # Create a rank 1 array\n", + "print(a)\n", + "print(type(a)) #print type of a\n", + "\n", + "b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array\n", + "print(b.shape) # Prints \"(2, 3)\"\n", + "print(b[0, 0], b[0, 1], b[1, 0])" + ], + "execution_count": 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": 218 + }, + "outputId": "d70ea2c5-27a1-47aa-b57b-7c238cf7b391" + }, + "cell_type": "code", + "source": [ + "a = np.zeros(shape=(2,2))\n", + "b = np.ones(shape = (3,3))\n", + "c = np.eye(2)\n", + "d = np.full(shape=(3,3), fill_value=5)\n", + "e = np.random.random((2,2))\n", + "\n", + "print('a', a)\n", + "print('b',b)\n", + "print('c',c)\n", + "print('d',d)\n", + "print('e',e)" + ], + "execution_count": 4, + "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.26525234 0.7239169 ]\n", + " [0.6221951 0.66992727]]\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": 188 + }, + "outputId": "b9269bc7-e6ca-410b-c347-ee0f74ca175a" + }, + "cell_type": "code", + "source": [ + "a == np.arange(10)\n", + "b == np.linspace(0,10, num=6)\n", + "print(a)\n", + "print(b)" + ], + "execution_count": 5, + "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": 50 + }, + "outputId": "74680dd7-bf09-4ee4-fdd2-32d1d7a33443" + }, + "cell_type": "code", + "source": [ + "a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n", + "\n", + "# Use slicing to pull out the subarray consisting of the first 2 rows\n", + "# and columns 1 and 2; b is the following array of shape (2, 2):\n", + "# [[2 3]\n", + "# [6 7]]\n", + "b = a[:2, 1:3]\n", + "\n", + "# A slice of an array is a view into the same data, so modifying it\n", + "# will modify the original array.\n", + "\n", + "print(a[0, 1]) # Prints \"2\"\n", + "\n", + "b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]\n", + "print(a[0, 1]) # Prints \"77\"" + ], + "execution_count": 6, + "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": 118 + }, + "outputId": "db5a4996-9afa-4d80-e06d-7686c8c0e7b3" + }, + "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": 7, + "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": 67 + }, + "outputId": "02a017db-7f90-4d00-dcc7-76a37a54a853" + }, + "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": 8, + "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": 67 + }, + "outputId": "30ef471c-9baa-4b2c-a9ce-964d820000ac" + }, + "cell_type": "code", + "source": [ + "b = np.arange(10)\n", + "\n", + "print(b)\n", + "\n", + "mask = b%2!=0 #perform computations on the list \n", + "\n", + "print(mask)\n", + "\n", + "print(b[mask]) #applying the mask on the numpy array\n" + ], + "execution_count": 9, + "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": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "59650165-66f0-41cb-c7cf-111e75ffab6c" + }, + "cell_type": "code", + "source": [ + "modified_b = b\n", + "modified_b[mask] = -1\n", + "\n", + "print(modified_b)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 0 -1 2 -1 4 -1 6 -1 8 -1]\n" + ], + "name": "stdout" + } + ] + }, + { + "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": 118 + }, + "outputId": "2b170a3d-8fd8-446e-b837-869f953634bd" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[:, [1,0,2]])" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0 1 2]\n", + " [3 4 5]\n", + " [6 7 8]]\n", + "[[1 0 2]\n", + " [4 3 5]\n", + " [7 6 8]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "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": 118 + }, + "outputId": "4e91b06f-9a42-4bf0-a4dc-cab75d80bf67" + }, + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3,3)\n", + "print(a)\n", + "\n", + "print(a[[1,0,2], :])" + ], + "execution_count": 13, + "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" + } + ] + } + ] +} \ No newline at end of file From aacb9c4ffe3e4f5683fb7ab0a521fae1d04ed635 Mon Sep 17 00:00:00 2001 From: Subham <40177225+loneWolf148@users.noreply.github.com> Date: Tue, 22 Jan 2019 19:02:47 +0530 Subject: [PATCH 3/8] Created using Colaboratory --- Numpy_Exercises.ipynb | 336 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 336 insertions(+) create mode 100644 Numpy_Exercises.ipynb diff --git a/Numpy_Exercises.ipynb b/Numpy_Exercises.ipynb new file mode 100644 index 0000000..5d3962a --- /dev/null +++ b/Numpy_Exercises.ipynb @@ -0,0 +1,336 @@ +{ + "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": 50 + }, + "outputId": "6269c67f-d5f2-4f6f-8a2e-71190daac9f1" + }, + "cell_type": "code", + "source": [ + "cyclic_array = np.resize([1,2,3], 3* int(input()))\n", + "print(cyclic_array)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "6\n", + "[1 2 3 1 2 3 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": "469f8a68-8bdd-4956-e950-1605d2bfb4ec" + }, + "cell_type": "code", + "source": [ + "odds = np.arange(1,20,2)\n", + "print(odds)" + ], + "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": "9a1b6165-7a96-4628-d499-3a2ec5410fe6" + }, + "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", + "intersection = np.intersect1d(a,b)\n", + "print(intersection)" + ], + "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": 50 + }, + "outputId": "c32d28ad-1b59-406a-93c9-5cffe280dead" + }, + "cell_type": "code", + "source": [ + "a = np.arange(10)\n", + "reshaped_array = a.reshape((2,5))\n", + "print(reshaped_array)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[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": 50 + }, + "outputId": "bf722685-650a-43cf-e519-3105d453d366" + }, + "cell_type": "code", + "source": [ + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "numpy_array = np.array(a)\n", + "print(numpy_array)\n", + "converted_list = list(numpy_array)\n", + "print(converted_list)" + ], + "execution_count": 7, + "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" + } + ] + }, + { + "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": 185 + }, + "outputId": "14dc3b0e-1115-4264-8ecb-dbc6c1bb7596" + }, + "cell_type": "code", + "source": [ + "zeros = np.zeros((10,10))\n", + "zeros[0] = 1\n", + "zeros[9] = 1\n", + "zeros[:,0] = 1 \n", + "zeros[:,9] = 1\n", + "print(zeros)" + ], + "execution_count": 8, + "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" + } + ] + }, + { + "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": 151 + }, + "outputId": "5737f62b-1867-402e-d3ee-d0a658081092" + }, + "cell_type": "code", + "source": [ + "checkerboard = np.zeros((8,8))\n", + "checkerboard[::,::2] = 1\n", + "print(checkerboard)" + ], + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[1. 0. 1. 0. 1. 0. 1. 0.]\n", + " [1. 0. 1. 0. 1. 0. 1. 0.]\n", + " [1. 0. 1. 0. 1. 0. 1. 0.]\n", + " [1. 0. 1. 0. 1. 0. 1. 0.]\n", + " [1. 0. 1. 0. 1. 0. 1. 0.]\n", + " [1. 0. 1. 0. 1. 0. 1. 0.]\n", + " [1. 0. 1. 0. 1. 0. 1. 0.]\n", + " [1. 0. 1. 0. 1. 0. 1. 0.]]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From d77f09d18635a02a054f49de48495c737273a665 Mon Sep 17 00:00:00 2001 From: Subham <40177225+loneWolf148@users.noreply.github.com> Date: Tue, 22 Jan 2019 19:42:36 +0530 Subject: [PATCH 4/8] Created using Colaboratory --- Numpy_Exercises.ipynb | 176 +++++++----------------------------------- 1 file changed, 28 insertions(+), 148 deletions(-) diff --git a/Numpy_Exercises.ipynb b/Numpy_Exercises.ipynb index 5d3962a..ff59f6a 100644 --- a/Numpy_Exercises.ipynb +++ b/Numpy_Exercises.ipynb @@ -63,28 +63,14 @@ "metadata": { "id": "4TxT66309n1o", "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 50 - }, - "outputId": "6269c67f-d5f2-4f6f-8a2e-71190daac9f1" + "colab": {} }, "cell_type": "code", "source": [ - "cyclic_array = np.resize([1,2,3], 3* int(input()))\n", - "print(cyclic_array)" + "" ], - "execution_count": 3, - "outputs": [ - { - "output_type": "stream", - "text": [ - "6\n", - "[1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3]\n" - ], - "name": "stdout" - } - ] + "execution_count": 0, + "outputs": [] }, { "metadata": { @@ -100,27 +86,14 @@ "metadata": { "id": "ebhEUZq29r32", "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "outputId": "469f8a68-8bdd-4956-e950-1605d2bfb4ec" + "colab": {} }, "cell_type": "code", "source": [ - "odds = np.arange(1,20,2)\n", - "print(odds)" + "" ], - "execution_count": 4, - "outputs": [ - { - "output_type": "stream", - "text": [ - "[ 1 3 5 7 9 11 13 15 17 19]\n" - ], - "name": "stdout" - } - ] + "execution_count": 0, + "outputs": [] }, { "metadata": { @@ -136,30 +109,16 @@ "metadata": { "id": "gOlfuJCo-JwF", "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "outputId": "9a1b6165-7a96-4628-d499-3a2ec5410fe6" + "colab": {} }, "cell_type": "code", "source": [ "#expected output array([2, 4])\n", "a = np.array([1,2,3,2,3,4,3,4,5,6])\n", - "b = np.array([7,2,10,2,7,4,9,4,9,8])\n", - "intersection = np.intersect1d(a,b)\n", - "print(intersection)" + "b = np.array([7,2,10,2,7,4,9,4,9,8])" ], - "execution_count": 5, - "outputs": [ - { - "output_type": "stream", - "text": [ - "[2 4]\n" - ], - "name": "stdout" - } - ] + "execution_count": 0, + "outputs": [] }, { "metadata": { @@ -175,29 +134,14 @@ "metadata": { "id": "2E8b55_2Cjx5", "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 50 - }, - "outputId": "c32d28ad-1b59-406a-93c9-5cffe280dead" + "colab": {} }, "cell_type": "code", "source": [ - "a = np.arange(10)\n", - "reshaped_array = a.reshape((2,5))\n", - "print(reshaped_array)" + "a = np.arange(10)" ], - "execution_count": 6, - "outputs": [ - { - "output_type": "stream", - "text": [ - "[[0 1 2 3 4]\n", - " [5 6 7 8 9]]\n" - ], - "name": "stdout" - } - ] + "execution_count": 0, + "outputs": [] }, { "metadata": { @@ -213,31 +157,14 @@ "metadata": { "id": "tcBCyhXPEp9C", "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 50 - }, - "outputId": "bf722685-650a-43cf-e519-3105d453d366" + "colab": {} }, "cell_type": "code", "source": [ - "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", - "numpy_array = np.array(a)\n", - "print(numpy_array)\n", - "converted_list = list(numpy_array)\n", - "print(converted_list)" + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n" ], - "execution_count": 7, - "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" - } - ] + "execution_count": 0, + "outputs": [] }, { "metadata": { @@ -253,40 +180,14 @@ "metadata": { "id": "4bjP3JAc9vRD", "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 185 - }, - "outputId": "14dc3b0e-1115-4264-8ecb-dbc6c1bb7596" + "colab": {} }, "cell_type": "code", "source": [ - "zeros = np.zeros((10,10))\n", - "zeros[0] = 1\n", - "zeros[9] = 1\n", - "zeros[:,0] = 1 \n", - "zeros[:,9] = 1\n", - "print(zeros)" + "" ], - "execution_count": 8, - "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" - } - ] + "execution_count": 0, + "outputs": [] }, { "metadata": { @@ -302,35 +203,14 @@ "metadata": { "id": "No7fx0Xy9zEh", "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 151 - }, - "outputId": "5737f62b-1867-402e-d3ee-d0a658081092" + "colab": {} }, "cell_type": "code", "source": [ - "checkerboard = np.zeros((8,8))\n", - "checkerboard[::,::2] = 1\n", - "print(checkerboard)" + "" ], - "execution_count": 22, - "outputs": [ - { - "output_type": "stream", - "text": [ - "[[1. 0. 1. 0. 1. 0. 1. 0.]\n", - " [1. 0. 1. 0. 1. 0. 1. 0.]\n", - " [1. 0. 1. 0. 1. 0. 1. 0.]\n", - " [1. 0. 1. 0. 1. 0. 1. 0.]\n", - " [1. 0. 1. 0. 1. 0. 1. 0.]\n", - " [1. 0. 1. 0. 1. 0. 1. 0.]\n", - " [1. 0. 1. 0. 1. 0. 1. 0.]\n", - " [1. 0. 1. 0. 1. 0. 1. 0.]]\n" - ], - "name": "stdout" - } - ] + "execution_count": 0, + "outputs": [] } ] } \ No newline at end of file From f7071f34f2dcfce55bf4d2862bcf075518700196 Mon Sep 17 00:00:00 2001 From: Subham <40177225+loneWolf148@users.noreply.github.com> Date: Tue, 22 Jan 2019 20:37:04 +0530 Subject: [PATCH 5/8] Created using Colaboratory --- Lists.ipynb | 123 ++++++++++++++++++++++------------------------------ 1 file changed, 52 insertions(+), 71 deletions(-) diff --git a/Lists.ipynb b/Lists.ipynb index a270312..5d68619 100644 --- a/Lists.ipynb +++ b/Lists.ipynb @@ -52,7 +52,7 @@ }, "cell_type": "code", "source": [ - "dummy_list = [1,2,45,32,57,3,90,2,3,41]" + "dummy_list = [1,2,2,4,3,31,67,98]" ], "execution_count": 0, "outputs": [] @@ -75,18 +75,18 @@ "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "0bb7306d-d8e9-45a0-c29a-3ecbbf299bb6" + "outputId": "806d8595-b1bd-4c20-ec52-9d910de8c5a4" }, "cell_type": "code", "source": [ "print(dummy_list)" ], - "execution_count": 3, + "execution_count": 16, "outputs": [ { "output_type": "stream", "text": [ - "[1, 2, 45, 32, 57, 3, 90, 2, 3, 41]\n" + "[1, 2, 2, 4, 3, 31, 67, 98]\n" ], "name": "stdout" } @@ -110,19 +110,19 @@ "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "d5bedc7f-2f50-4dab-9bd7-79dccbd30e3b" + "outputId": "dbef7341-5d37-4c93-ef35-9df5a01ffcf8" }, "cell_type": "code", "source": [ "dummy_list.reverse()\n", "print(dummy_list)" ], - "execution_count": 4, + "execution_count": 17, "outputs": [ { "output_type": "stream", "text": [ - "[41, 3, 2, 90, 3, 57, 32, 45, 2, 1]\n" + "[98, 67, 31, 3, 4, 2, 2, 1]\n" ], "name": "stdout" } @@ -146,7 +146,7 @@ "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "ec786bd3-23bc-4b07-de50-c907a9aa6e01" + "outputId": "d0a06d9f-a600-42b4-ce59-df4c05adf0e3" }, "cell_type": "code", "source": [ @@ -154,12 +154,12 @@ "dummy_list.extend(dummy_list_2)\n", "print(dummy_list)" ], - "execution_count": 5, + "execution_count": 18, "outputs": [ { "output_type": "stream", "text": [ - "[41, 3, 2, 90, 3, 57, 32, 45, 2, 1, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + "[98, 67, 31, 3, 4, 2, 2, 1, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" ], "name": "stdout" } @@ -183,21 +183,11 @@ }, "cell_type": "code", "source": [ - "dummy_dict = {key:dummy_list.count(key) for key in set(dummy_list)}" + "dummy_dict = {key : dummy_list.count(key) for key in set(dummy_list)}" ], "execution_count": 0, "outputs": [] }, - { - "metadata": { - "id": "Am8q0wJvgh1h", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "" - ] - }, { "metadata": { "id": "RgCYpFXGou6q", @@ -216,18 +206,18 @@ "base_uri": "https://localhost:8080/", "height": 34 }, - "outputId": "2dda8b74-3e16-4d9f-d03c-108536958f91" + "outputId": "c09e2010-1f6e-4377-b364-81596e550dc6" }, "cell_type": "code", "source": [ "print(dummy_dict)" ], - "execution_count": 7, + "execution_count": 20, "outputs": [ { "output_type": "stream", "text": [ - "{32: 1, 1: 2, 2: 3, 3: 2, 4: 1, 0: 1, 200: 1, 41: 1, 9.45: 1, 12.01: 1, 45: 1, 45.67: 1, 12.02: 1, 16: 1, 57: 1, 90: 2}\n" + "{0: 1, 1: 2, 98: 1, 3: 1, 67: 1, 4: 2, 2: 3, 200: 1, 9.45: 1, 12.01: 1, 45.67: 1, 12.02: 1, 16: 1, 90: 1, 31: 1}\n" ], "name": "stdout" } @@ -251,22 +241,22 @@ "base_uri": "https://localhost:8080/", "height": 50 }, - "outputId": "89f3053c-326c-48e0-e122-406a7668adcb" + "outputId": "8968c0ee-1c8f-441d-d54e-5532c261b7c0" }, "cell_type": "code", "source": [ - "ascending_dummy_list = sorted(dummy_list, reverse=False)\n", - "descending_dummy_list = sorted(dummy_list, reverse=True)\n", - "print(ascending_dummy_list)\n", - "print(descending_dummy_list)" + "ascending_list = sorted(dummy_list, reverse=False)\n", + "descending_list = sorted(dummy_list, reverse=True)\n", + "print(ascending_list)\n", + "print(descending_list)" ], - "execution_count": 8, + "execution_count": 21, "outputs": [ { "output_type": "stream", "text": [ - "[0, 1, 1, 2, 2, 2, 3, 3, 4, 9.45, 12.01, 12.02, 16, 32, 41, 45, 45.67, 57, 90, 90, 200]\n", - "[200, 90, 90, 57, 45.67, 45, 41, 32, 16, 12.02, 12.01, 9.45, 4, 3, 3, 2, 2, 2, 1, 1, 0]\n" + "[0, 1, 1, 2, 2, 2, 3, 4, 4, 9.45, 12.01, 12.02, 16, 31, 45.67, 67, 90, 98, 200]\n", + "[200, 98, 90, 67, 45.67, 31, 16, 12.02, 12.01, 9.45, 4, 4, 3, 2, 2, 2, 1, 1, 0]\n" ], "name": "stdout" } @@ -288,32 +278,29 @@ "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 67 + "height": 50 }, - "outputId": "3472875e-674e-47e6-b533-ba121ca00ecf" + "outputId": "ef05e2ac-29ce-452a-9dd7-1334b7d2bd3d" }, "cell_type": "code", "source": [ "x = 200\n", - "print('List Before Removal : '+str(dummy_list))\n", + "print(dummy_list)\n", "dummy_list.remove(x)\n", - "print('List After Removal : '+str(dummy_list))\n", - "\n", "# Let's play: try the same with something which is not in the list to get the ValueError\n", - "try:\n", - " x = 300\n", + "try :\n", + " x = -23\n", " dummy_list.remove(x)\n", "except ValueError:\n", - " print('ValueError raise as '+str(x)+' not present in list')" + " print(str(x)+' not present in list')" ], - "execution_count": 9, + "execution_count": 22, "outputs": [ { "output_type": "stream", "text": [ - "List Before Removal : [41, 3, 2, 90, 3, 57, 32, 45, 2, 1, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", - "List After Removal : [41, 3, 2, 90, 3, 57, 32, 45, 2, 1, 2, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", - "ValueError raise as 300 not present in list\n" + "[98, 67, 31, 3, 4, 2, 2, 1, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "-23 not present in list\n" ], "name": "stdout" } @@ -335,38 +322,29 @@ "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 212 + "height": 195 }, - "outputId": "6f4a5e2e-1e0d-4843-b61a-73eb472d3399" + "outputId": "999f0f36-f5e5-4b4b-b4c8-8fe9b2f58d54" }, "cell_type": "code", "source": [ - "from random import randint\n", - "x = randint(0,len(dummy_list))\n", - "removed_item = dummy_list[x]\n", - "print(str(removed_item)+' removed')\n", - "dummy_list.pop(x)\n", + "from random import randint \n", + "x = randint(0, len(dummy_list))\n", + "dummy_list.remove(x)\n", "# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get\n", - "dummy_list.pop(len(dummy_list)+5)" + "dummy_list.remove(len(dummy_list) + 5)" ], - "execution_count": 10, + "execution_count": 23, "outputs": [ - { - "output_type": "stream", - "text": [ - "2 removed\n" - ], - "name": "stdout" - }, { "output_type": "error", - "ename": "IndexError", + "ename": "ValueError", "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 5\u001b[0m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\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----> 7\u001b[0;31m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\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;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mIndexError\u001b[0m: pop index out of range" + "\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 3\u001b[0m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\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----> 5\u001b[0;31m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\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;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" ] } ] @@ -387,22 +365,24 @@ "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", - "height": 84 + "height": 101 }, - "outputId": "763214ef-8641-4c69-847a-33f17674db87" + "outputId": "e5bd9358-8ae0-4d8e-8335-72b600f88a75" }, "cell_type": "code", "source": [ "dummy_list.clear()\n", - "ascending_dummy_list.clear()\n", - "descending_dummy_list.clear()\n", + "dummy_list_2.clear()\n", + "ascending_list.clear()\n", + "descending_list.clear()\n", "dummy_dict.clear()\n", "print(dummy_list)\n", - "print(ascending_dummy_list)\n", - "print(descending_dummy_list)\n", + "print(dummy_list_2)\n", + "print(ascending_list)\n", + "print(descending_list)\n", "print(dummy_dict)" ], - "execution_count": 11, + "execution_count": 24, "outputs": [ { "output_type": "stream", @@ -410,6 +390,7 @@ "[]\n", "[]\n", "[]\n", + "[]\n", "{}\n" ], "name": "stdout" From 2b101f5ae93bd9beca361cca415ba4cb86dee43e Mon Sep 17 00:00:00 2001 From: Subham <40177225+loneWolf148@users.noreply.github.com> Date: Tue, 22 Jan 2019 20:48:50 +0530 Subject: [PATCH 6/8] Delete loneWolf148.ipynb --- loneWolf148.ipynb | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 loneWolf148.ipynb diff --git a/loneWolf148.ipynb b/loneWolf148.ipynb deleted file mode 100644 index 9e2543a..0000000 --- a/loneWolf148.ipynb +++ /dev/null @@ -1,32 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.5.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From b498aef29d4e169860a9027b3189e32537cc3b08 Mon Sep 17 00:00:00 2001 From: Subham <40177225+loneWolf148@users.noreply.github.com> Date: Tue, 22 Jan 2019 20:50:42 +0530 Subject: [PATCH 7/8] Delete Numpy_Exercises.ipynb --- Numpy_Exercises.ipynb | 216 ------------------------------------------ 1 file changed, 216 deletions(-) delete mode 100644 Numpy_Exercises.ipynb diff --git a/Numpy_Exercises.ipynb b/Numpy_Exercises.ipynb deleted file mode 100644 index ff59f6a..0000000 --- a/Numpy_Exercises.ipynb +++ /dev/null @@ -1,216 +0,0 @@ -{ - "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": {} - }, - "cell_type": "code", - "source": [ - "" - ], - "execution_count": 0, - "outputs": [] - }, - { - "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": {} - }, - "cell_type": "code", - "source": [ - "" - ], - "execution_count": 0, - "outputs": [] - }, - { - "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": {} - }, - "cell_type": "code", - "source": [ - "#expected output array([2, 4])\n", - "a = np.array([1,2,3,2,3,4,3,4,5,6])\n", - "b = np.array([7,2,10,2,7,4,9,4,9,8])" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "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": {} - }, - "cell_type": "code", - "source": [ - "a = np.arange(10)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "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": {} - }, - "cell_type": "code", - "source": [ - "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n" - ], - "execution_count": 0, - "outputs": [] - }, - { - "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": {} - }, - "cell_type": "code", - "source": [ - "" - ], - "execution_count": 0, - "outputs": [] - }, - { - "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": {} - }, - "cell_type": "code", - "source": [ - "" - ], - "execution_count": 0, - "outputs": [] - } - ] -} \ No newline at end of file From 30ff6db79fdd4ce6d1d7d3dbd2a75868c6f7c42d Mon Sep 17 00:00:00 2001 From: Subham <40177225+loneWolf148@users.noreply.github.com> Date: Tue, 22 Jan 2019 21:00:57 +0530 Subject: [PATCH 8/8] Created using Colaboratory --- Numpy_Exercises.ipynb | 337 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 337 insertions(+) create mode 100644 Numpy_Exercises.ipynb diff --git a/Numpy_Exercises.ipynb b/Numpy_Exercises.ipynb new file mode 100644 index 0000000..334d525 --- /dev/null +++ b/Numpy_Exercises.ipynb @@ -0,0 +1,337 @@ +{ + "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": 50 + }, + "outputId": "6f0b86e0-b198-40c0-fef7-4ec041367f51" + }, + "cell_type": "code", + "source": [ + "cyclic_array = np.resize([1,2,3], 3*int(input()))\n", + "print(cyclic_array)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "4\n", + "[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": "09d09ba4-022e-4473-8abb-959c97a348bb" + }, + "cell_type": "code", + "source": [ + "odds = np.arange(1,20,2)\n", + "print(odds)" + ], + "execution_count": 8, + "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": "174d4f93-9b43-47d4-9e29-27568c8aabaa" + }, + "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", + "intersection = np.intersect1d(a,b)\n", + "print(intersection)" + ], + "execution_count": 9, + "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": 50 + }, + "outputId": "827c5cd3-eb58-4f00-dc22-d86aa4866295" + }, + "cell_type": "code", + "source": [ + "a = np.arange(10)\n", + "reshaped_array = a.reshape((2,5))\n", + "print(reshaped_array)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[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": 50 + }, + "outputId": "efc118e9-7bc1-487d-b28d-08aa396ca859" + }, + "cell_type": "code", + "source": [ + "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "numpy_array = np.array(a)\n", + "converted_list = list(a)\n", + "print(numpy_array)\n", + "print(converted_list)" + ], + "execution_count": 12, + "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" + } + ] + }, + { + "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": 185 + }, + "outputId": "8dccf8a7-b7e8-4367-be8d-12d0f5fd2aa2" + }, + "cell_type": "code", + "source": [ + "zeros = np.zeros((10,10))\n", + "zeros[0,:] = 1\n", + "zeros[9,:] = 1\n", + "zeros[:,0] = 1\n", + "zeros[:,9] = 1\n", + "print(zeros)" + ], + "execution_count": 22, + "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" + } + ] + }, + { + "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": 151 + }, + "outputId": "7c75d531-e711-4301-bc06-b9ae819c0e53" + }, + "cell_type": "code", + "source": [ + "checkerboard = np.zeros((8,8))\n", + "checkerboard[1::2, ::2] = 1\n", + "checkerboard[::2, 1::2] = 1\n", + "print(checkerboard)" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[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