{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "s1", "content", "l1" ] }, "source": [ "# Numpy\n", "\n", "## Arrays and Lists\n", "\n", "Before getting introduced to Numpy library, we need to be familiar with a very widely used data structure called 'array'. An array is a collection of homogenous variables. Here homogenous means variables of the same data type. And so an array can be a collection of integers (int datatypes), collection of fractions/decimal values (float datatypes) or a collection of characters (char datatype) also referred to as a string.\n", "\n", "A list in very similar to an array in structure differing only by the characteristic that, a list is a collection of heterogenous variables, i.e., it can contain a mixture of variables with various datatypes.\n", "\n", "\n", "\n", "Both arrays and lists can be accessed in the same way, using an index, which is a reference to the position of the element within the collection. Index begins from zero and ends with n-1, where n is the total length of the array (which is the number of elements in the array or list).\n", "\n", "## NumPy Introduction\n", "\n", "### Basics of NumPy.\n", "\n", "NumPy is a library in Python that supports large, multidimensional arrays and matrices. It facilitates scientific computing with high level mathematical functions to operate on these arrays.\n", "\n", "NumPy object is a multi-dimensional array. It is a table of elements of the same type, indexed by a tuple of positive integers. In Numpy, dimensions are called axes. The number of axes is rank. (ref: scipy.org)\n", "\n", "Consider a 3D space of x, y and z coordinates. A point in 3D space [1.0, 3.0, 5.0] is an array of rank 1. If there are several such points as shown below, then the dimensions are 4 and 3.\n", "```python\n", "[[ 1.0, 0.3, 4.5],\n", " [ 0.5, 1.5, 2.3],\n", " [ 6.0, 4.6, 3.5],\n", " [ 4.5, 3.5, 6.3]]\n", "```\n", "To use a library, we need to import them. Any python library can be referenced by an alias that is mentioned during the import. For example, NumPy library is most commonly imported in the short form as np:\n", "```python\n", "import numpy as np\n", "```\n", "\n", "There are several ways to initialize an array in numpy :\n", "* a = np.array([0,1,2,3]) ... creates an array of rank 1\n", "* a = np.array([[0,1,2,3],[4,5,6,7]]) ... creates a 2x4 matrix \n", "* a = np.ones((3,3)) ... creates a 3x3 matrix with all 1s\n", "* a = np.zeroes((2,2)) ... creates a 2x2 matrix with all 0s\n", "* a = np.eye(3) ... creates a 3x3 matrix with 1s at the diagonal and 0s otherwise\n", " \n", " \n", " \n", "Ref: http://www.numpy.org\n", "\n", "\n", "### Exercise:\n", "\n", "The command shape is used to determine the dimensions of the array. The library numpy is imported initially. Numpy directly could be used to run operations such as numpy.array(). However, using short forms of libraries such as np is a common practice.\n", "\n", "Find the shape of the array below and assign the shape to the variable: two_d_shape and print it out.\n", "\n", "```python\n", "[[1, 0.3, 4.5],\n", "[2, 4.0, 6.0]]\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true, "tags": [ "s1", "ce", "l1" ] }, "outputs": [], "source": [ "import numpy as np\n", "\n", "two_d_array = np.array([[ 1 , 0.3, 4.5],\n", " [2, 4.0, 6.0]])\n" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "s1", "l1", "hint" ] }, "source": [ "

Note that few numbers are float and few are int. Note the values are converted to float after the initiliazation. 

" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "tags": [ "s1", "l1", "ans" ] }, "outputs": [], "source": [ "two_d_shape = two_d_array.shape\n", "print(two_d_shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "tags": [ "s1", "hid", "l1" ] }, "outputs": [], "source": [ "ref_tmp_var = False\n", "\n", "\n", "try:\n", " ref_assert_var = False\n", " two_d_shape_ = two_d_array.shape\n", " \n", " if two_d_shape == two_d_shape_:\n", " ref_assert_var = True\n", " else:\n", " ref_assert_var = False\n", " \n", "except Exception:\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "else:\n", " if ref_assert_var:\n", " ref_tmp_var = True\n", " else:\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "\n", "\n", "assert ref_tmp_var" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "l2", "content", "s2" ] }, "source": [ "\n", "\n", "

\n", "## NumPy Array Operations\n", "\n", "### Operations\n", " \n", "\n", "In this lesson we shall study various operations on the numpy arrays. \n", "\n", "#### Addition of two arrays\n", "\n", "To add another array of the same dimension use the function add.\n", "```python\n", "C = numpy.add(A, B)\n", "```\n", "\n", "#### Subtraction of two arrays\n", "\n", "To subtract an array from another array use \n", "```python\n", "D = A - B\n", "```\n", "\n", "#### Multiplication\n", "\n", "To multiply a constant with the elements of the array, the * symbol is used:\n", "```python\n", "K = 10\n", "Y = K * np.array([1, 2, 3, 4, 5])\n", "```\n", "To multiply a vector with another vector:\n", "```python\n", "Y = A.dot(B)\n", "```\n", "\n", "#### Other Common Operations\n", "\n", "The other common operations such as square root and exponential functions can be computed with the extensions that are common to most other languages such as :\n", "```python\n", "numpy.sqrt(B), numpy.exp(A), \n", "```\n", "In this lesson, we can observe that most operations we can conceive of with respect to arrays in the area of data science, can be accessed through the commonly available extensions in other languages as well.\n", "\n", "\n", "### Exercise:\n", "\n", "Given two arrays:\n", "```python\n", "A = [1, 2, 3, 4]\n", "B = [2, 3, 4, 5]\n", "```\n", "\n", "- Initilize the above arrays as variables A & B.\n", "- Perform a dot product of the two vectors and assign it to the variable C.\n", "- Print C." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true, "tags": [ "l2", "ce", "s2" ] }, "outputs": [], "source": [ "import numpy as np\n" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "l2", "s2", "hint" ] }, "source": [ "

for power in B:

\n", "\n", "

  A**power will contain the array. Append this to another empty array.

" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "tags": [ "l2", "s2", "ans" ] }, "outputs": [], "source": [ "A = np.array([1, 2, 3, 4])\n", "B = np.array([2, 3, 4, 5])\n", "\n", "C = A.dot(B)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "tags": [ "l2", "hid", "s2" ] }, "outputs": [], "source": [ "ref_tmp_var = False\n", "\n", "\n", "try:\n", " ref_assert_var = False\n", " A_ = np.array([1, 2, 3, 4])\n", " B_ = np.array([2, 3, 4, 5])\n", " \n", " C_ = A_.dot(B_)\n", " \n", " if C == C_:\n", " ref_assert_var = True\n", " else:\n", " ref_assert_var = False\n", " \n", "except Exception:\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "else:\n", " if ref_assert_var:\n", " ref_tmp_var = True\n", " else:\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "\n", "\n", "assert ref_tmp_var" ] } ], "metadata": { "anaconda-cloud": {}, "executed_sections": [], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }