{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arrays" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# numpy array\n", "arr = np.array([0,1,2,3])\n", "arr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "numpy calc is faster than native python " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 loops, best of 3: 55.7 ms per loop\n" ] } ], "source": [ "# testing the same\n", "\n", "L = range(100000)\n", "%timeit [i**2 for i in L]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The slowest run took 4.17 times longer than the fastest. This could mean that an intermediate result is being cached.\n", "10000 loops, best of 3: 98.8 µs per loop\n" ] } ], "source": [ "L = np.arange(100000)\n", "%timeit L**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "there are so many ways to create array in numpy, we will see them one by one\n", "#### starting with 1-D array" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 5, 3, 5, 9])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([1,2,5,3,5,9])\n", "a" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,\n", " 18, 19])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(1, 20) # start, end\n", "a" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 4, 7, 10, 13, 16, 19])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(1, 20, 3) # start, end, step\n", "a" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dimension - 1\n", "size - (7,)\n", "length - 7\n" ] } ], "source": [ "# check dimension and size\n", "print(\"dimension - \", a.ndim)\n", "print(\"size - \", a.shape)\n", "print(\"length - \", len(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2-D array" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [2, 3, 4],\n", " [3, 4, 5]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.array([[1,2,3], [2,3,4], [3,4,5]])\n", "b" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dimension - 2\n", "size - (3, 3)\n", "length - 3\n" ] } ], "source": [ "# check dimension and size\n", "print(\"dimension - \", b.ndim)\n", "print(\"size - \", b.shape)\n", "print(\"length - \", len(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3-D array" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[[5, 7],\n", " [8, 9],\n", " [0, 7]],\n", "\n", " [[1, 2],\n", " [2, 3],\n", " [6, 7]],\n", "\n", " [[5, 7],\n", " [8, 9],\n", " [0, 7]]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = np.array([[[5,7], [8,9], [0,7]], [[1,2], [2,3], [6,7]], [[5,7], [8,9], [0,7]]])\n", "c" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dimension - 3\n", "size - (3, 3, 2)\n", "length - 3\n" ] } ], "source": [ "# check dimension and size\n", "print(\"dimension - \", c.ndim)\n", "print(\"size - \", c.shape)\n", "print(\"length - \", len(c))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simplest way to create test array" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 3 5 7 9]\n", "(5,)\n" ] } ], "source": [ "# by no\n", "a = np.arange(1, 10, 2)\n", "print(a)\n", "print(a.shape)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1. 4. 7. 10.]\n", "(4,)\n" ] } ], "source": [ "# by number of points\n", "a = np.linspace(1, 10, 4) # pick any 4 no from 1 to 10\n", "print(a)\n", "print(a.shape)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1. 2.5 4. 5.5 7. 8.5]\n", "(6,)\n" ] } ], "source": [ "# by number of points\n", "a = np.linspace(1, 10, 6, endpoint=False) # pick any 4 no from 1 to 10\n", "print(a)\n", "print(a.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Common arrays to use :" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1., 1., 1., 1.])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones(4)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 1., 1.],\n", " [ 1., 1., 1., 1.]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones((2,4))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 0., 0.],\n", " [ 0., 0., 0.]])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros((2,3))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 0., 0.],\n", " [ 0., 1., 0.],\n", " [ 0., 0., 1.]])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.eye((3))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 0, 0, 0],\n", " [0, 2, 0, 0],\n", " [0, 0, 3, 0],\n", " [0, 0, 0, 4]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.diag([1,2,3,4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Random no generation \n", "with help of numpy random library" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.77109549, 0.92437055, 0.13376104, 0.00209447, 0.99861705])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.rand(5) # pick any 5 value between 0 and 1" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([-0.26465512, 0.76965644, 0.61422423, 2.54456552, -0.99713591])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.randn(5) # includes negative no as well (Gaussian)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.randint(9) # this will pick any random no from 0 to 8 (integer)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.randint(10, 13) # start, end end exclusive" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([11, 11])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.randint(10, 13, 2) # start, end, no of element end exclusive" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([11, 10, 10, 10])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.randint(10, 13, 4) # start, end, no of element end exclusive" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[19, 11, 11],\n", " [18, 5, 17],\n", " [ 5, 13, 13],\n", " [15, 19, 16]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.randint(1, 20, (4, 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you set the np.random.seed(a_fixed_number) every time you call the numpy's other random function, the result will be the same:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([3, 3])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(123)\n", "np.random.randint(1, 10, 2)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([3, 3])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(123)\n", "np.random.randint(1, 10, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Data Type" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4 6 5]\n", "int32\n" ] } ], "source": [ "a = np.random.randint(3,9, 3)\n", "print(a)\n", "print(a.dtype)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0.42310646 0.9807642 0.68482974]\n", "float64\n" ] } ], "source": [ "a = np.random.rand(3)\n", "print(a)\n", "print(a.dtype)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can explicitly specify which data-type you want:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = np.array([1, 2, 3], dtype=float)\n", "c.dtype" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('int32')" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = np.array([1, 2, 3])\n", "c.dtype" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.ones((3, 3))\n", "a.dtype" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('complex128')" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# complex data type\n", "d = np.array([1+2j, 3+4j, 5+6*1j])\n", "d.dtype" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('bool')" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Boolean\n", "e = np.array([True, False, False, True])\n", "e.dtype" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('