diff --git a/Lists.ipynb b/Lists.ipynb new file mode 100644 index 0000000..5d68619 --- /dev/null +++ b/Lists.ipynb @@ -0,0 +1,401 @@ +{ + "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,2,4,3,31,67,98]" + ], + "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": "806d8595-b1bd-4c20-ec52-9d910de8c5a4" + }, + "cell_type": "code", + "source": [ + "print(dummy_list)" + ], + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 2, 2, 4, 3, 31, 67, 98]\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": "dbef7341-5d37-4c93-ef35-9df5a01ffcf8" + }, + "cell_type": "code", + "source": [ + "dummy_list.reverse()\n", + "print(dummy_list)" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[98, 67, 31, 3, 4, 2, 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": "d0a06d9f-a600-42b4-ce59-df4c05adf0e3" + }, + "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": 18, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[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" + } + ] + }, + { + "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": "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": "c09e2010-1f6e-4377-b364-81596e550dc6" + }, + "cell_type": "code", + "source": [ + "print(dummy_dict)" + ], + "execution_count": 20, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{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" + } + ] + }, + { + "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": "8968c0ee-1c8f-441d-d54e-5532c261b7c0" + }, + "cell_type": "code", + "source": [ + "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": 21, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[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" + } + ] + }, + { + "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": 50 + }, + "outputId": "ef05e2ac-29ce-452a-9dd7-1334b7d2bd3d" + }, + "cell_type": "code", + "source": [ + "x = 200\n", + "print(dummy_list)\n", + "dummy_list.remove(x)\n", + "# Let's play: try the same with something which is not in the list to get the ValueError\n", + "try :\n", + " x = -23\n", + " dummy_list.remove(x)\n", + "except ValueError:\n", + " print(str(x)+' not present in list')" + ], + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[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" + } + ] + }, + { + "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": 195 + }, + "outputId": "999f0f36-f5e5-4b4b-b4c8-8fe9b2f58d54" + }, + "cell_type": "code", + "source": [ + "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.remove(len(dummy_list) + 5)" + ], + "execution_count": 23, + "outputs": [ + { + "output_type": "error", + "ename": "ValueError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 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" + ] + } + ] + }, + { + "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": 101 + }, + "outputId": "e5bd9358-8ae0-4d8e-8335-72b600f88a75" + }, + "cell_type": "code", + "source": [ + "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(dummy_list_2)\n", + "print(ascending_list)\n", + "print(descending_list)\n", + "print(dummy_dict)" + ], + "execution_count": 24, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[]\n", + "[]\n", + "[]\n", + "[]\n", + "{}\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..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 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 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 -}