{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial Brief" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "numpy is a powerful set of tools to perform mathematical operations of on lists of numbers. It works faster than normal python lists operations and can manupilate high dimentional arrays too.\n", "\n", "Finding Help:\n", "\n", " - http://wiki.scipy.org/Tentative_NumPy_Tutorial\n", " - http://docs.scipy.org/doc/numpy/reference/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics, science, and engineering.\n", ">\n", "> *http://www.scipy.org/*\n", "\n", "So NumPy is a part of a bigger ecosystem of libraries that build on the optimized performance of NumPy NDArray.\n", "\n", "It contain these core packages:\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

NumPy

Base N-dimensional array package

SciPy

Fundamental library for scientific computing

Matplotlib

Comprehensive 2D Plotting

IPython

Enhanced Interactive Console

SymPy

Symbolic mathematics

Pandas

Data structures & analysis
\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Importig the library" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import numpy library as np\n", "This helps in writing code and it's almost a standard in scientific work" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Working with ndarray" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will generate an ndarray with np.arange method.\n", "\n", "####np.arange([start,] stop[, step,], dtype=None)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(10)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(1,10)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. ,\n", " 6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(1,10, 0.5)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 4, 7])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(1,10, 3)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 3., 5., 7., 9.])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(1,10, 2, dtype=np.float64)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Examining ndrray" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds = np.arange(1,10,2)\n", "ds.ndim" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5,)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.shape" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.size" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int32')" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.dtype" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.itemsize" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['\\x01',\n", " '\\x00',\n", " '\\x00',\n", " '\\x00',\n", " '\\x03',\n", " '\\x00',\n", " '\\x00',\n", " '\\x00',\n", " '\\x05',\n", " '\\x00',\n", " '\\x00',\n", " '\\x00',\n", " '\\x07',\n", " '\\x00',\n", " '\\x00',\n", " '\\x00',\n", " '\\t',\n", " '\\x00',\n", " '\\x00',\n", " '\\x00']" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x=ds.data\n", "list(x)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 3, 5, 7, 9])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Memory Usage\n", "ds.size * ds.itemsize" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Why to use numpy?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will compare the time it takes to create two lists and do some basic operations on them." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generate a list" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%%capture timeit_results\n", "# Regular Python\n", "%timeit python_list_1 = range(1,1000)\n", "python_list_1 = range(1,1000)\n", "python_list_2 = range(1,1000)\n", "\n", "#Numpy\n", "%timeit numpy_list_1 = np.arange(1,1000)\n", "numpy_list_1 = np.arange(1,1000)\n", "numpy_list_2 = np.arange(1,1000)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100000 loops, best of 3: 15 us per loop\n", "The slowest run took 5.51 times longer than the fastest. This could mean that an intermediate result is being cached.\n", "100000 loops, best of 3: 2.25 us per loop\n", "\n" ] } ], "source": [ "print timeit_results" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Function to calculate time in seconds\n", "def return_time(timeit_result):\n", " temp_time = float(timeit_result.split(\" \")[5])\n", " temp_unit = timeit_result.split(\" \")[6]\n", " if temp_unit == \"ms\":\n", " temp_time = temp_time * 1e-3\n", " elif temp_unit == \"us\":\n", " temp_time = temp_time * 1e-6\n", " elif temp_unit == \"ns\":\n", " temp_time = temp_time * 1e-9\n", " return temp_time" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "could not convert string to float: times", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mpython_time\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mreturn_time\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimeit_results\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"\\n\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mnumpy_time\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mreturn_time\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimeit_results\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"\\n\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"Python/NumPy: %.1f\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mpython_time\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mnumpy_time\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m\u001b[0m in \u001b[0;36mreturn_time\u001b[1;34m(timeit_result)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Function to calculate time in seconds\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mreturn_time\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimeit_result\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mtemp_time\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimeit_result\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\" \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mtemp_unit\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtimeit_result\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\" \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mtemp_unit\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"ms\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mValueError\u001b[0m: could not convert string to float: times" ] } ], "source": [ "python_time = return_time(timeit_results.stdout.split(\"\\n\")[0])\n", "numpy_time = return_time(timeit_results.stdout.split(\"\\n\")[1])\n", "\n", "print \"Python/NumPy: %.1f\" % (python_time/numpy_time)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic Operation" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%%capture timeit_python\n", "%%timeit\n", "# Regular Python\n", "[(x + y) for x, y in zip(python_list_1, python_list_2)]\n", "[(x - y) for x, y in zip(python_list_1, python_list_2)]\n", "[(x * y) for x, y in zip(python_list_1, python_list_2)]\n", "[(x / y) for x, y in zip(python_list_1, python_list_2)];" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print timeit_python" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%%capture timeit_numpy\n", "%%timeit\n", "#Numpy\n", "numpy_list_1 + numpy_list_2\n", "numpy_list_1 - numpy_list_2\n", "numpy_list_1 * numpy_list_2\n", "numpy_list_1 / numpy_list_2;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print timeit_numpy" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "python_time = return_time(timeit_python.stdout)\n", "numpy_time = return_time(timeit_numpy.stdout)\n", "\n", "print \"Python/NumPy: %.1f\" % (python_time/numpy_time)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Most Common Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List Creation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Parameters\n", "----------\n", "object : array_like\n", " An array, any object exposing the array interface, an\n", " object whose __array__ method returns an array, or any\n", " (nested) sequence.\n", "dtype : data-type, optional\n", " The desired data-type for the array. If not given, then\n", " the type will be determined as the minimum type required\n", " to hold the objects in the sequence. This argument can only\n", " be used to 'upcast' the array. For downcasting, use the\n", " .astype(t) method.\n", "copy : bool, optional\n", " If true (default), then the object is copied. Otherwise, a copy\n", " will only be made if __array__ returns a copy, if obj is a\n", " nested sequence, or if a copy is needed to satisfy any of the other\n", " requirements (`dtype`, `order`, etc.).\n", "order : {'C', 'F', 'A'}, optional\n", " Specify the order of the array. If order is 'C' (default), then the\n", " array will be in C-contiguous order (last-index varies the\n", " fastest). If order is 'F', then the returned array\n", " will be in Fortran-contiguous order (first-index varies the\n", " fastest). If order is 'A', then the returned array may\n", " be in any order (either C-, Fortran-contiguous, or even\n", " discontiguous).\n", "subok : bool, optional\n", " If True, then sub-classes will be passed-through, otherwise\n", " the returned array will be forced to be a base-class array (default).\n", "ndmin : int, optional\n", " Specifies the minimum number of dimensions that the resulting\n", " array should have. Ones will be pre-pended to the shape as\n", " needed to meet this requirement.\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.array([1,2,3,4,5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Multi Dimentional Array" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.array([[1,2],[3,4],[5,6]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### zeros(shape, dtype=float, order='C') and ones(shape, dtype=float, order='C')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Parameters\n", "----------\n", "shape : int or sequence of ints\n", " Shape of the new array, e.g., ``(2, 3)`` or ``2``.\n", "dtype : data-type, optional\n", " The desired data-type for the array, e.g., `numpy.int8`. Default is\n", " `numpy.float64`.\n", "order : {'C', 'F'}, optional\n", " Whether to store multidimensional data in C- or Fortran-contiguous\n", " (row- or column-wise) order in memory.\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.zeros((3,4))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.zeros((3,4), dtype=np.int64)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.ones((3,4))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.linspace(start, stop, num=50, endpoint=True, retstep=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Parameters\n", "----------\n", "start : scalar\n", " The starting value of the sequence.\n", "stop : scalar\n", " The end value of the sequence, unless `endpoint` is set to False.\n", " In that case, the sequence consists of all but the last of ``num + 1``\n", " evenly spaced samples, so that `stop` is excluded. Note that the step\n", " size changes when `endpoint` is False.\n", "num : int, optional\n", " Number of samples to generate. Default is 50.\n", "endpoint : bool, optional\n", " If True, `stop` is the last sample. Otherwise, it is not included.\n", " Default is True.\n", "retstep : bool, optional\n", " If True, return (`samples`, `step`), where `step` is the spacing\n", " between samples.\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.linspace(1,5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.linspace(0,2,num=4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.linspace(0,2,num=4,endpoint=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### random_sample(size=None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Parameters\n", "----------\n", "size : int or tuple of ints, optional\n", " Defines the shape of the returned array of random floats. If None\n", " (the default), returns a single float.\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.random.random((2,3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.random.random_sample((2,3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Statistical Analysis" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set = np.random.random((2,3))\n", "data_set" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.max(a, axis=None, out=None, keepdims=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Parameters\n", "----------\n", "a : array_like\n", " Input data.\n", "axis : int, optional\n", " Axis along which to operate. By default, flattened input is used.\n", "out : ndarray, optional\n", " Alternative output array in which to place the result. Must\n", " be of the same shape and buffer length as the expected output.\n", " See `doc.ufuncs` (Section \"Output arguments\") for more details.\n", "keepdims : bool, optional\n", " If this is set to True, the axes which are reduced are left\n", " in the result as dimensions with size one. With this option,\n", " the result will broadcast correctly against the original `arr`.\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.max(data_set)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.max(data_set, axis=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.max(data_set, axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.min(a, axis=None, out=None, keepdims=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.min(data_set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.mean(a, axis=None, dtype=None, out=None, keepdims=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.mean(data_set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.median(a, axis=None, out=None, overwrite_input=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.median(data_set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.std(data_set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.sum(a, axis=None, dtype=None, out=None, keepdims=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.sum(data_set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reshaping" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.reshape(a, newshape, order='C')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.reshape(data_set, (3,2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.reshape(data_set, (6,1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.reshape(data_set, (6))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.ravel(a, order='C')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.ravel(data_set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Slicing" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set = np.random.random((5,10))\n", "data_set" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set[1][0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set[1,0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Slicing a range" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set[2:4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set[2:4,0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set[2:4,0:2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set[:,0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Stepping" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set[2:4:1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set[::]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set[::2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set[2:4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_set[2:4,::2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix Operations" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "# Matrix A \n", "A = np.array([[1,2],[3,4]])\n", "# Matrix B\n", "B = np.array([[3,4],[5,6]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Addition" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "A+B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Subtraction" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "A-B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Multiplication (Element by Element)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "A*B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Multiplication (Matrix Multiplication)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[13, 16],\n", " [29, 36]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.dot(B)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Division" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "A/B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Square" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.square(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Power" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.power(A,3) #cube of matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Transpose" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 3],\n", " [2, 4]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.transpose()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Inverse" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-2. , 1. ],\n", " [ 1.5, -0.5]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linalg.inv(A)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "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.13" } }, "nbformat": 4, "nbformat_minor": 1 }