Skip to content

Instantly share code, notes, and snippets.

@khushmeeet
Created July 20, 2016 16:34
Show Gist options
  • Save khushmeeet/23236861ad43ac18012b623282f18928 to your computer and use it in GitHub Desktop.
Save khushmeeet/23236861ad43ac18012b623282f18928 to your computer and use it in GitHub Desktop.
Numpy cheatsheet
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Numpy CheatSheet"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Numpy stands for **Numerical Python**. \n",
" It is a python library that adds support for large arrays and matrices and high level mathematical functions to operate on these arrays. The core feature of numpy is the **ndarray** (n-dimensional array data structure).\n",
" As opposed to the lists in python these arrays must be **homogeneous**: all the elements must be of same type. \n",
" \n",
" It is created by **Travis Oliphant**."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from IPython.display import display"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Importing Numpy module"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"# linalg for linear algebra calculations\n",
"from numpy import linalg as lg\n",
"# random for generating random numbers\n",
"from numpy import random as rd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Array creation"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 2., 3., 4., 5.])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# from python list\n",
"a = np.array([1,2,3,4,5],float)\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 2., 3., 4., 5.],\n",
" [ 6., 7., 8., 9., 10.]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 2-dimensional array\n",
"b = np.array([[1,2,3,4,5],[6,7,8,9,10]],float) #float specifies the array datatype\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# another way using python range function\n",
"c = np.array(range(10))\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 1., 2., 3., 4., 5., 6.])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# using numpy's arange function\n",
"d = np.arange(7,dtype=float)\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 1., 1.],\n",
" [ 1., 1., 1.],\n",
" [ 1., 1., 1.]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# creating arrays using numpy's ones and zeros function\n",
"# It will have only ones and zeros as elements\n",
"e = np.ones((3,3)) # arguments -> (array size in tuple,datatype(optional))\n",
"e"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 0, 0],\n",
" [0, 0, 0, 0],\n",
" [0, 0, 0, 0]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = np.zeros((3,4),dtype=int)\n",
"f"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"g = \n",
" [[ 0. 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0. 0.]]\n",
"h = \n",
" [[ 1. 1. 1.]\n",
" [ 1. 1. 1.]\n",
" [ 1. 1. 1.]]\n"
]
}
],
"source": [
"# zeros_like and ones_like create new arrays using dimensions of existing arrays\n",
"g = np.zeros_like(b) #In[4]:\n",
"h = np.ones_like(e) #In[7]:\n",
"print('g = \\n',g)\n",
"print('h = \\n',h)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 0, 0],\n",
" [0, 1, 0],\n",
" [0, 0, 1]])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# for creating identitiy matrix\n",
"np.identity(3,dtype=int) #where 3 is the size of matrix"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0., 1., 0., 0.],\n",
" [ 0., 0., 1., 0.],\n",
" [ 0., 0., 0., 1.],\n",
" [ 0., 0., 0., 0.]])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# creating matrix with ones along the kth diagonal\n",
"np.eye(4,k=1,dtype=float)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Array info"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3, 3)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# getting dimensions of the array\n",
"h.shape # In[9]:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dtype('float64')"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# types of values staored in array\n",
"h.dtype # In[9]:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# array length\n",
"len(h) # In[9]:\n",
" # Not a numpy function"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(True, False)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# presence of element in an array\n",
"( 8 in b , 11 in b ) # In[4]:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Accessing array elements"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([12, 32, 42, 12, 23, 54])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# creating an array\n",
"matx = np.array([12,32,42,12,23,54],dtype=int)\n",
"matx"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# accessing array elements using bracket notation\n",
"matx[3] # 3 is the position in the array"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 12, 32, 42, 12, 100, 54])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# array modification \n",
"matx[4] = 100 # replace 4the element in the array\n",
"matx"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[12, 32, 42, 12],\n",
" [ 2, 34, 55, 21],\n",
" [45, 99, 10, 67]])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# accessing elements in n-dimensional array\n",
"# creating 2D array\n",
"matx = np.array([[12,32,42,12],[2,34,55,21],[45,99,10,67]],dtype=int)\n",
"matx"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"55"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"matx[1,2] # rows and columns start with zero"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Array modification"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[12, 32, 42, 12],\n",
" [ 2, 34, 55, 21],\n",
" [45, 99, 10, 67]])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# creating an array\n",
"matx = np.array([[12,32,42,12],[2,34,55,21],[45,99,10,67]],dtype=int)\n",
"matx"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 12, 32, 42, 12],\n",
" [ 2, 34, 55, 1100],\n",
" [ 45, 99, 10, 67]])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# changing elements in an array\n",
"matx[1,3] = 1100\n",
"matx"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14]])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# reshaping array to a new dimensions\n",
"# vector -> 3x5 matrix\n",
"matx = np.array(range(15),dtype=int)\n",
"matx = matx.reshape((3,5)) # creates new array\n",
"matx"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14]])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# copying an array\n",
"matx2 = matx.copy()\n",
"matx2"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14]], list)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# arrays to list\n",
"mylist = matx.tolist()\n",
"mylist , type(mylist)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x06\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x08\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\t\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\n\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x0b\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\r\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x0e\\x00\\x00\\x00\\x00\\x00\\x00\\x00'"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# converting array to string\n",
"string = matx.tostring()\n",
"string"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.00000000e+000, 4.94065646e-324, 9.88131292e-324,\n",
" 1.48219694e-323, 1.97626258e-323, 2.47032823e-323,\n",
" 2.96439388e-323, 3.45845952e-323, 3.95252517e-323,\n",
" 4.44659081e-323, 4.94065646e-323, 5.43472210e-323,\n",
" 5.92878775e-323, 6.42285340e-323, 6.91691904e-323])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# string to array\n",
"np.fromstring(string)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[10, 10, 10, 10, 10],\n",
" [10, 10, 10, 10, 10],\n",
" [10, 10, 10, 10, 10]])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# filling existing array with single values\n",
"matx2.fill(10) # In[24]:\n",
"matx2"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 5, 10],\n",
" [ 1, 6, 11],\n",
" [ 2, 7, 12],\n",
" [ 3, 8, 13],\n",
" [ 4, 9, 14]])"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Transpose of a arrays\n",
"matx2 = matx.transpose()\n",
"matx2"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 5, 10, 1, 6, 11, 2, 7, 12, 3, 8, 13, 4, 9, 14])"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# generating vectors from arrays\n",
"matx2 = matx2.flatten()\n",
"matx2"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 0, 1, 2, 3, 4, 0, 1])"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# concatenating arrays\n",
"a = np.array(range(3))\n",
"b = np.array(range(5))\n",
"c = np.array(range(2))\n",
"np.concatenate((a,b,c))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [2, 3, 4],\n",
" [3, 4, 7],\n",
" [9, 8, 7],\n",
" [6, 5, 7],\n",
" [5, 4, 3]])"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# concatenating with higher dimensions\n",
"a = np.array([[1,2,3],[2,3,4],[3,4,7]])\n",
"b = np.array([[9,8,7],[6,5,7],[5,4,3]])\n",
"np.concatenate((a,b),axis=0) # vertical"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3, 9, 8, 7],\n",
" [2, 3, 4, 6, 5, 7],\n",
" [3, 4, 7, 5, 4, 3]])"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.concatenate((a,b),axis=1) #horizontal"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[1, 2, 3],\n",
" [2, 3, 4]]])"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# increasing dimensions of the array\n",
"a = np.array([[1,2,3],[2,3,4]])\n",
"a[np.newaxis,:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Array mathematics"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 5, 7, 9],\n",
" [11, 13, 15]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([[-3, -3, -3],\n",
" [-3, -3, -3]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([[ 4, 10, 18],\n",
" [28, 40, 54]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([[ 0.25 , 0.4 , 0.5 ],\n",
" [ 0.57142857, 0.625 , 0.66666667]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([[ 1, 32, 729],\n",
" [ 16384, 390625, 10077696]])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# general arithmetic applied element by element\n",
"# dimensions need to match\n",
"a = np.array([[1,2,3],[4,5,6]],dtype=int)\n",
"b = np.array([[4,5,6],[7,8,9]],dtype=int)\n",
"display(a+b)\n",
"display(a-b)\n",
"display(a*b)\n",
"display(a/b)\n",
"display(a%b)\n",
"display(a**b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Several common mathematical operations are provided by numpy and these are performed elementwise. Like: **abs** , **sqrt** , **log** , **log10** , **sign** , **exp** , **sin** , **cos** , **tan** , **arcsin** , **arccos** , **arctan** , **arcsinh** , **arccosh** , **arctanh** , **sinh** , **cosh** , **tanh** , **floor** , **ceil** and **rint**."
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ,\n",
" -0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849])"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array(range(10))\n",
"np.sin(a)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.rint(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy also provides physical and mathematical constants. These include **pi** , **golden_ratio** , **c** , **speed_of_light** , **h** , **Plank** , **gravitational_constant** and many others."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3.141592653589793"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"2.718281828459045"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(np.pi)\n",
"display(np.e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Array Iteration"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4]\n",
"[6 7 8 9]\n"
]
}
],
"source": [
"# iterating over arrays using for loop\n",
"a = np.array([range(1,5),range(6,10)])\n",
"for i in a:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"15\n"
]
}
],
"source": [
"# taking more than one element , summing row as a whole\n",
"a = np.array([[1,2,3],[4,5,6]])\n",
"for (i,j,k) in a:\n",
" print(i+j+k)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Operating on arrays"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"21"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"720"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# summing and multiplying all elements of an array\n",
"display(a)\n",
"display(np.sum(a))\n",
"display(np.prod(a))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6]])"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#statistical functions\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3.5"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"2.9166666666666665"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"1.707825127659933"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"----------------\n"
]
},
{
"data": {
"text/plain": [
"array([ 2.5, 3.5, 4.5])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([ 0.66666667, 0.66666667])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([ 0.81649658, 0.81649658])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# mean , variance , standard deviation\n",
"# use axis parameter to calculate mean , variance and standard deviation along specified axis\n",
"# 0 -> column 1 -> rows\n",
"display(np.mean(a))\n",
"display(np.var(a))\n",
"display(np.std(a))\n",
"print('----------------')\n",
"display(np.mean(a,axis=0))\n",
"display(np.var(a,axis=1))\n",
"display(np.std(a,axis=1))"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"1"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# max and min values in array\n",
"display(np.max(a))\n",
"display(np.min(a))"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"5"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# argmin and argmax returns the indices of the min and max element\n",
"display(np.argmin(a))\n",
"display(np.argmax(a))"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 5, 6],\n",
" [1, 5, 9]])"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# sorting arrays\n",
"a = np.array([[1,5,6],[9,5,1]])\n",
"np.sort(a)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4, 5])"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# extracting unique elements\n",
"a = np.array([1,3,4,5,5,3,2,1,1,4])\n",
"np.unique(a)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 7])"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# extracting diagonals\n",
"a = np.array([range(1,5),range(6,10)])\n",
"a.diagonal()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Comparing arrays"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, True, True, True], dtype=bool)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([False, False, False, False], dtype=bool)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([False, False, False, False], dtype=bool)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([False, False, True, True], dtype=bool)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# general comparisons elementwise\n",
"a = np.array([5,6,7,8])\n",
"b = np.array([1,2,3,6])\n",
"display(a>b)\n",
"display(a==b)\n",
"display(a<=b)\n",
"display(b>2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**any** and **all** functions are used to check if any or all elements are true in an array."
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, True, True, False], dtype=bool)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([ True, True, True, True], dtype=bool)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([False, False, False, False], dtype=bool)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# logical and , or , not\n",
"display(np.logical_and(a>2,b<5))\n",
"display(np.logical_or(a>b,b>2))\n",
"display(np.logical_not(b))"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([5, 6, 7, 8])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([1, 2, 3, 6])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# creating new array using conditions on existing arrays\n",
"display(a)\n",
"display(b)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([3, 3, 3, 3])"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.where(a>2,3,4)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(array([0, 1, 2, 3]),)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([False, False, False, False], dtype=bool)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# checking if values are zero or NaN\n",
"display(a.nonzero())\n",
"display(np.isnan(b))"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([3, 6])"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# using conditions to select elements in array\n",
"b[b>2]"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([4, 5, 2, 7])"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# use another array to select elements from an array\n",
"# can also be used with multi dimensional arrays\n",
"# np.take is also used to perfrom same operation\n",
"a = np.array([1,2,3,4])\n",
"b = np.array([3,4,5,2,7,8,9,0,2,3,5])\n",
"b[a]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Above operation can also be used with multi dimensional arrays. **np.take( )** is used to perform the same above operation with an array and axis as an argument.\n",
"**np.put( )** does the opposite of **np.take( )** by putting the elements at the given indices in an array."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Matrix mathematics"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[23, 29],\n",
" [49, 61]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([[ 4, 5, 8, 9, 1, 2],\n",
" [ 8, 10, 16, 18, 2, 4],\n",
" [12, 15, 24, 27, 3, 6],\n",
" [12, 15, 24, 27, 3, 6],\n",
" [16, 20, 32, 36, 4, 8],\n",
" [20, 25, 40, 45, 5, 10]])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# dot and outer product\n",
"# same for inner and cross product\n",
"a = np.array([[1,2,3],[3,4,5]])\n",
"b = np.array([[4,5],[8,9],[1,2]])\n",
"display(np.dot(a,b))\n",
"display(np.outer(a,b))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy has many builtin mathematical functions for linear algebra calculations and can be found in **linalg** module."
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"-9.5161973539299405e-16"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# calculating determinant of a\n",
"a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
"lg.det(a)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1.61168440e+01, -1.11684397e+00, -9.75918483e-16])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([[-0.23197069, -0.78583024, 0.40824829],\n",
" [-0.52532209, -0.08675134, -0.81649658],\n",
" [-0.8186735 , 0.61232756, 0.40824829]])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# finding eigen values and eigen vectors \n",
"values,vectors = lg.eig(a)\n",
"display(values)\n",
"display(vectors)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 3.15251974e+15, -6.30503948e+15, 3.15251974e+15],\n",
" [ -6.30503948e+15, 1.26100790e+16, -6.30503948e+15],\n",
" [ 3.15251974e+15, -6.30503948e+15, 3.15251974e+15]])"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# inverse of a matrix\n",
"lg.inv(a)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(array([[-0.21483724, 0.88723069, 0.40824829],\n",
" [-0.52058739, 0.24964395, -0.81649658],\n",
" [-0.82633754, -0.38794278, 0.40824829]]),\n",
" array([ 1.68481034e+01, 1.06836951e+00, 3.33475287e-16]),\n",
" array([[-0.47967118, -0.57236779, -0.66506441],\n",
" [-0.77669099, -0.07568647, 0.62531805],\n",
" [-0.40824829, 0.81649658, -0.40824829]]))"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# singular value decomposition\n",
"lg.svd(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Polynomial mathematics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given a set of roots, its polynomial coefficients can be found using **np.poly( )** which corresponds to \n",
"\n",
"\\begin{array}{*{20}c} {ax^4 + bx^3 + cx^2 + dx + k} \\\\ \\end{array}"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., -4., -1., 16., -12.])"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.poly([1,-2,3,2])"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([-2., 3., 2., 1.])"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# now using coefficients to find roots\n",
"np.roots([1,-4,-1,16,-12])"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"310"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# evaluating polynomial at a particular value\n",
"# evaluates at x=5\n",
"np.polyval([2,3,-4,5],5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Statistical functions"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"5.0"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# finding mean\n",
"a = np.array([[1,2,3],[7,8,9]])\n",
"np.median(a)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 1.],\n",
" [ 1., 1.]])"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# covariance\n",
"np.cov(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Random Numbers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We use **random** module in numpy to produce pseudo random numbers."
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# random numbers are generted from a seed value\n",
"rd.seed(101196)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.66862748, 0.74182791, 0.26280242],\n",
" [ 0.96054286, 0.12304381, 0.43631083]])"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# random numbers b/w 0 and 1\n",
"rd.rand(2,3) # where 2,3 are matrix dimensions"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# generating random integers only\n",
"# randint(min,max)\n",
"rd.randint(2,10)"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[4, 5, 6],\n",
" [1, 2, 3]])"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# shuffling numbers in a list or an array\n",
"a = np.array([[1,2,3],[4,5,6]])\n",
"rd.shuffle(a)\n",
"a"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment