{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NumPy exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some of these come from / are inspired from https://github.com/rougier/numpy-100 and http://www.scipy-lectures.org/intro/numpy/exercises.html\n", "\n", "You might want to look over these lists as well." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q1\n", "\n", "We can use `np.random.random_sample()` to create an array with random values. By default, these will be in the range `[0.0, 1.0)`. You can\n", "multiple the output and add a scalar to it to get it to be in a different range.\n", "\n", "Create a 10 x 10 array initialized with random numbers that lie between 0 and 10.\n", "\n", "Then compute the average of the array (there is a numpy function for this, `np.mean()`)." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.893927257115085" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=np.random.random_sample(10)*10\n", "np.mean(a)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 6, 11],\n", " [ 2, 7, 12],\n", " [ 3, 8, 13],\n", " [ 4, 9, 14],\n", " [ 5, 10, 15]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=np.arange(1,16).reshape(3,5).transpose()\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q2\n", "\n", "Create the array: \n", "```\n", "[[1, 6, 11],\n", " [2, 7, 12],\n", " [3, 8, 13],\n", " [4, 9, 14],\n", " [5, 10, 15]]\n", "```\n", "with out explicitly typing it in.\n", "\n", "Now create a new array containing only its 2nd and 4th rows." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a=np.arange(1,16).reshape(3,5).transpose()\n", "a\n", "b=" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q3\n", "\n", "Create a 2d array with `1` on the border and `0` on the inside, e.g., like:\n", "```\n", "1 1 1 1 1\n", "1 0 0 0 1\n", "1 0 0 0 1\n", "1 1 1 1 1\n", "```\n", "\n", "Do this using array slice notation to let it work for an arbitrary-sized array" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q4\n", "\n", " * Create an array with angles in degrees 0, 15, 30, ... 90 (i.e., every 15 degrees up to 90).\n", "\n", " * Now create 3 new arrays with the sine, cosine, and tangent of the elements of the first array\n", " \n", " * Finally, calculate the inverse sine, inverse cosine, and inverse tangent the arrays above and compare to the original angles" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q5\n", "\n", "Given the array:\n", "```\n", "x = np.array([1, -1, 2, 5, 8, 4, 10, 12, 3])\n", "```\n", "calculate the difference of each element with its neighbor." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q6\n", "\n", "Here we will read in columns of numbers from a file and create a histogram, using NumPy routines. Make sure you have the data file\n", "\"`sample.txt`\" in the same directory as this notebook (you can download it from https://raw.githubusercontent.com/sbu-python-summer/python-tutorial/master/day-3/sample.txt\n", "\n", " * Use `np.loadtxt()` to read this file in. \n", "\n", " * Next, use `np.histogram()` to create a histogram array. The output returns both the count and an array of edges.\n", " \n", " * Finally, loop over the bins and print out the bin center (averaging the left and right edges of the bin) and the count for that bin." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q7\n", "\n", "NumPy has a standard deviation function, `np.std()`, but here we'll write our own that works on a 1-d array (vector). The standard\n", "deviation is a measure of the \"width\" of the distribution of numbers\n", "in the vector.\n", "\n", "Given an array, $a$, and an average $\\bar{a}$, the standard deviation\n", "is:\n", "$$\n", "\\sigma = \\left [ \\frac{1}{N} \\sum_{i=1}^N (a_i - \\bar{a})^2 \\right ]^{1/2}\n", "$$\n", "\n", "Write a function to calculate the standard deviation for an input array, `a`:\n", "\n", " * First compute the average of the elements in `a` to define $\\bar{a}$\n", " * Next compute the sum over the squares of $a - \\bar{a}$\n", " * Then divide the sum by the number of elements in the array\n", " * Finally take the square root (you can use `np.sqrt()`)\n", " \n", "Test your function on a random array, and compare to the built-in `np.std()`" ] }, { "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }