diff --git a/My_Numpy_Exercises.ipynb b/My_Numpy_Exercises.ipynb new file mode 100644 index 0000000..fc92803 --- /dev/null +++ b/My_Numpy_Exercises.ipynb @@ -0,0 +1,390 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Numpy_Exercises.ipynb", + "version": "0.3.2", + "provenance": [], + "collapsed_sections": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"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", + "outputId": "2578a466-c03f-4ecc-a1d8-fa2c615e9b2c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 208 + } + }, + "cell_type": "code", + "source": [ + "import numpy as np #import numpy\n", + "a = np.linspace(1.3,2.5,64)\n", + "print (a)" + ], + "execution_count": 76, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1.3 1.31904762 1.33809524 1.35714286 1.37619048 1.3952381\n", + " 1.41428571 1.43333333 1.45238095 1.47142857 1.49047619 1.50952381\n", + " 1.52857143 1.54761905 1.56666667 1.58571429 1.6047619 1.62380952\n", + " 1.64285714 1.66190476 1.68095238 1.7 1.71904762 1.73809524\n", + " 1.75714286 1.77619048 1.7952381 1.81428571 1.83333333 1.85238095\n", + " 1.87142857 1.89047619 1.90952381 1.92857143 1.94761905 1.96666667\n", + " 1.98571429 2.0047619 2.02380952 2.04285714 2.06190476 2.08095238\n", + " 2.1 2.11904762 2.13809524 2.15714286 2.17619048 2.1952381\n", + " 2.21428571 2.23333333 2.25238095 2.27142857 2.29047619 2.30952381\n", + " 2.32857143 2.34761905 2.36666667 2.38571429 2.4047619 2.42380952\n", + " 2.44285714 2.46190476 2.48095238 2.5 ]\n" + ], + "name": "stdout" + } + ] + }, + { + "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", + "outputId": "9f25b496-8c58-4735-c710-04686c30e58c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "cell_type": "code", + "source": [ + "a = np.array([1,2,3])\n", + "\n", + "x = np.resize(a,12)\n", + "print(x)" + ], + "execution_count": 77, + "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", + "outputId": "160c78ce-9337-467b-9609-9bab72eda317", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "cell_type": "code", + "source": [ + "a = np.arange(20)\n", + "\n", + "i = a%2!=0\n", + "\n", + "b = a[i]\n", + "print (b)" + ], + "execution_count": 78, + "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": "5daaadcc-4767-4f76-9c53-2bc2cdb504d7" + }, + "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", + "c = list()\n", + "k=0\n", + "for value in a:\n", + " for values in b:\n", + " if value == values and value not in c:\n", + " c.append(value)\n", + " break\n", + " \n", + "x = np.asarray(a)\n", + "x" + ], + "execution_count": 79, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 79 + } + ] + }, + { + "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", + "outputId": "929007aa-bb2d-4967-d57f-b454ef274315", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 69 + } + }, + "cell_type": "code", + "source": [ + "a = np.arange(10)\n", + "print(a)\n", + "\n", + "print(a.reshape(2,5))" + ], + "execution_count": 80, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0 1 2 3 4 5 6 7 8 9]\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", + "outputId": "3d060203-be6f-4813-c1e6-d9c1764f591a", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 69 + } + }, + "cell_type": "code", + "source": [ + "a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n", + "print(a)\n", + "#converting to list\n", + "b = a.tolist()\n", + "print (b)\n", + "#again converting it to an array\n", + "c =np.asarray(b)\n", + "print (c)" + ], + "execution_count": 81, + "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", + "[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", + "outputId": "054cba0e-5051-4b16-8b59-a3b5170aa2a7", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 191 + } + }, + "cell_type": "code", + "source": [ + "a = np.zeros(shape = (10,10))\n", + "for i in range(0,10):\n", + " for j in range(0,10):\n", + " if i == 0 or i == 9 or j == 0 or j == 9:\n", + " a[i,j] = 1\n", + " \n", + "print (a)" + ], + "execution_count": 82, + "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", + "outputId": "57a4dada-8e5a-4b13-8cd1-992ce702c7bd", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 156 + } + }, + "cell_type": "code", + "source": [ + "a = np.zeros(shape = (8,8)) \n", + " # fill with 1 the alternate rows and columns \n", + " \n", + "a[1::2, ::2] = 1\n", + "a[::2, 1::2] = 1\n", + " \n", + "print (a)" + ], + "execution_count": 83, + "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/NumpyExercises.ipynb b/NumpyExercises.ipynb deleted file mode 100644 index 2649f6a..0000000 --- a/NumpyExercises.ipynb +++ /dev/null @@ -1,270 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "Copy of 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": { - "base_uri": "https://localhost:8080/", - "height": 752 - }, - "outputId": "9a616a02-c5cb-40b3-dc3d-894c2a224f2c" - }, - "cell_type": "code", - "source": [ - "import numpy as np #import numpy\n", - "def frange(start, stop, step):\n", - " x = start\n", - " while x < stop:\n", - " yield x\n", - " x += step\n", - "\n", - "for i in frange(-1.3,2.6,0.1):\n", - " a = np.array(i)\n", - " print (a)\n", - " \n", - "print(np.true_divide(a,64))" - ], - "execution_count": 10, - "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": [ - "a = np.array([1, 2, 3])\n", - "\n", - "a = np.resize(a,12)\n", - "print (a)\n" - ], - "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": [ - "b = np.arange(20)\n", - "\n", - "i = b%2!=0\n", - "\n", - "print(b[i])\n" - ], - "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])\n", - "\n", - "x = np.intersect1d(a,b)\n", - "print (x)" - ], - "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)\n", - "print (a)\n", - "\n", - "a = np.arange(10).reshape(2,5)\n", - "print (a)" - ], - "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", - "a= np.arange(9).reshape(3, 3)\n", - "\n", - "print(\"Original array elements:\")\n", - "print(a)\n", - "\n", - "print(\"Array to list:\")\n", - "x = a.tolist()\n", - "print (x)\n", - "\n", - "print (\"list to Array\")\n", - "y = np.array(x)\n", - "print(y)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "JNqX8wnz9sQJ", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "\n", - "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": [ - "a = np.ones((10,10))\n", - "print(\"1 on the border and 0 inside in the array\")\n", - "a[1:-1,1:-1] = 0\n", - "print(a)\n" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "xaQgf8tT9v-n", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "8\n", - ") 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": [ - "x = np.ones((3,3))\n", - "print(\"Checkerboard pattern:\")\n", - "\n", - "x = np.zeros((8,8))\n", - "\n", - "x[1::2,::2] = 1\n", - "x[::2,1::2] = 1\n", - "\n", - "print(x)\n" - ], - "execution_count": 0, - "outputs": [] - } - ] -}