{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Numpy Tutorial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy is a computational library for Python that is optimized for operations on multi-dimensional arrays. In this notebook we will use numpy to work with 1-d arrays (often called vectors) and 2-d arrays (often called matrices).\n", "\n", "For a the full user guide and reference for numpy see: http://docs.scipy.org/doc/numpy/" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np # importing this way allows us to refer to numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating Numpy Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "New arrays can be made in several ways. We can take an existing list and convert it to a numpy array:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mylist = [1., 2., 3., 4.]\n", "mynparray = np.array(mylist)\n", "mynparray" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can initialize an array (of any dimension) of all ones or all zeroes with the ones() and zeros() functions:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "one_vector = np.ones(4)\n", "print one_vector # using print removes the array() portion" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "one2Darray = np.ones((2, 4)) # an 2D array with 2 \"rows\" and 4 \"columns\"\n", "print one2Darray" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "zero_vector = np.zeros(4)\n", "print zero_vector" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also initialize an empty array which will be filled with values. This is the fastest way to initialize a fixed-size numpy array however you must ensure that you replace all of the values." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "empty_vector = np.empty(5)\n", "print empty_vector" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Accessing array elements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accessing an array is straight forward. For vectors you access the index by referring to it inside square brackets. Recall that indices in Python start with 0." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mynparray[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2D arrays are accessed similarly by referring to the row and column index separated by a comma:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "my_matrix = np.array([[1, 2, 3], [4, 5, 6]])\n", "print my_matrix" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print my_matrix[1, 2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sequences of indices can be accessed using ':' for example" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print my_matrix[0:2, 2] # recall 0:2 = [0, 1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print my_matrix[0, 0:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also pass a list of indices. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fib_indices = np.array([1, 1, 2, 3])\n", "random_vector = np.random.random(10) # 10 random numbers between 0 and 1\n", "print random_vector" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print random_vector[fib_indices]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use true/false values to select values" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "my_vector = np.array([1, 2, 3, 4])\n", "select_index = np.array([True, False, True, False])\n", "print my_vector[select_index]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For 2D arrays you can select specific columns and specific rows. Passing ':' selects all rows/columns" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "select_cols = np.array([True, False, True]) # 1st and 3rd column\n", "select_rows = np.array([False, True]) # 2nd row" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print my_matrix[select_rows, :] # just 2nd row but all columns" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print my_matrix[:, select_cols] # all rows and just the 1st and 3rd column" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Operations on Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use the operations '\\*', '\\*\\*', '\\\\', '+' and '-' on numpy arrays and they operate elementwise." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "my_array = np.array([1., 2., 3., 4.])\n", "print my_array*my_array" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print my_array**2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print my_array - np.ones(4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print my_array + np.ones(4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print my_array / 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print my_array / np.array([2., 3., 4., 5.]) # = [1.0/2.0, 2.0/3.0, 3.0/4.0, 4.0/5.0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can compute the sum with np.sum() and the average with np.average()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print np.sum(my_array)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print np.average(my_array)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print np.sum(my_array)/len(my_array)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#The dot product" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An important mathematical operation in linear algebra is the dot product. \n", "\n", "When we compute the dot product between two vectors we are simply multiplying them elementwise and adding them up. In numpy you can do this with np.dot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "array1 = np.array([1., 2., 3., 4.])\n", "array2 = np.array([2., 3., 4., 5.])\n", "print np.dot(array1, array2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print np.sum(array1*array2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recall that the Euclidean length (or magnitude) of a vector is the squareroot of the sum of the squares of the components. This is just the squareroot of the dot product of the vector with itself:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "array1_mag = np.sqrt(np.dot(array1, array1))\n", "print array1_mag" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print np.sqrt(np.sum(array1*array1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use the dot product when we have a 2D array (or matrix). When you have an vector with the same number of elements as the matrix (2D array) has columns you can right-multiply the matrix by the vector to get another vector with the same number of elements as the matrix has rows. For example this is how you compute the predicted values given a matrix of features and an array of weights." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "my_features = np.array([[1., 2.], [3., 4.], [5., 6.], [7., 8.]])\n", "print my_features" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "my_weights = np.array([0.4, 0.5])\n", "print my_weights" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "my_predictions = np.dot(my_features, my_weights) # note that the weights are on the right\n", "print my_predictions # which has 4 elements since my_features has 4 rows" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly if you have a vector with the same number of elements as the matrix has *rows* you can left multiply them." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_matrix = my_features\n", "my_array = np.array([0.3, 0.4, 0.5, 0.6])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print np.dot(my_array, my_matrix) # which has 2 elements because my_matrix has 2 columns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Multiplying Matrices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we have two 2D arrays (matrices) matrix_1 and matrix_2 where the number of columns of matrix_1 is the same as the number of rows of matrix_2 then we can use np.dot() to perform matrix multiplication." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "matrix_1 = np.array([[1., 2., 3.],[4., 5., 6.]])\n", "print matrix_1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "matrix_2 = np.array([[1., 2.], [3., 4.], [5., 6.]])\n", "print matrix_2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print np.dot(matrix_1, matrix_2)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }