Show Menu
Cheatography

NumPy cheatsheet by Lavanya and

Why NumPy?

NumPy is an open-s­ource numerical Python library used for working with arrays. It aims to provide an array object that is upto 50x faster than tradit­ional python list takes signif­icantly less amount of memory as compared to python lists.

How to Install Numpy

pip install numpy 
or
conda install numpy

Importing Library

import  numpy  as  np

Attributes of ndarray

ndarray.shape
Tuple of array shape
ndarra­y.ndim
Number of array dimensions as interger
ndarra­y.size
Number of elements in the array
ndarra­y,dtype
Data type of array’s elements
ndarra­y.base
To check if object has its own memory

Slicing

arr[0]
Returns the element at index 0
arr[1,2]
Returns array element on index [1][2]
arr[0:3]
Returns the elements at indices on outer dimension
arr[0:3,2]
Returns the elements on rows 0,1,2 at column 2
arr<n
Returns an array with boolean values
~arr
Returns an array with boolean values

Statistics

np.mean(arr,axis=0/1)
Compute the arithmetic mean along the specified axis.
arr.sum()
Sum of array elements over a given axis
arr.min()
Return the minimum along a given axis
arr.max()
Return the maximum along a given axis
np.var­(arr)
Compute the variance along the specified axis
np.std­(arr)
Compute the standard deviation along the specified axis.
arr.co­rrc­oef()
Return Pearson produc­t-m­oment correl­ation coeffi­cients
 

Creating Arrays

np.arr­ay(­object)
Creates an array
np.arr­ay(­[1,­2,3])
1D array
np.array([(1,2,3),(4,5,6)])
2D array
np.zer­os(­shape)
Return a new array of given shape and type, filled with zeros
np.one­s(s­hape)
Return a new array of given shape and type, filled with ones
np.eye(no. of rows)
Return a 2-D array with ones on the diagonal and zeros elsewhere
np.ara­nge­(st­art­,st­op,­step)
Return evenly spaced values within a given interval.
np.ran­dom.ra­nd(­shape)
Return array of random floats between 0–1 of fiven shape
np.random.randint(low,high)
Return random integers from low (inclu­sive) to high (exclu­sive)
np.linspace(start, stop, n)
Returns n evenly spaced numbers over a specified interval

commonly used methods

np.sor­t(arr)
Returns a sorted copy of the array
np.arg­sor­t(arr)
Returns the indices that would sort an array
np.res­ize(a, new_shape)
Return a new array with the specified shape
np.dot­(arr1, arr2)
Dot product of two arrays
arr.copy()
Returns a copy of the array
arr.view()
New view of array with the same data
arr.fl­atten()
Return a copy of the array collapsed into 1D
arr.re­sha­pe(­new­_shape)
Returns an array containing the same data with a new shape

Math operators

np.add(arr_1, arr_2)
Add arguments elemen­t-wise
np.subtract(arr_1, arr_2)
Subtract arguments, elemen­t-wise
np.multiply(arr_1, arr_2)
Multiply arguments, elemen­t-wise
np.divide(arr_1, arr_2)
Divide arguments, elemen­t-wise
np.power(arr_1, arr_2)
First array elements raised to powers from second array, elemen­t-wise
np.sqr­t(arr)
Return the non-ne­gative square­-root of an array, elemen­t-wise
np.log­(arr)
Natural logarithm, elemen­t-wise
np.cei­l(arr)
Rounds up to the nearest int , elemen­t-wise
np.flo­or(arr)
Rounds down to the nearest int ,eleme­nt-wise
np.abs­(arr)
Absolute value of each element in the array
np.rou­nd(arr)
Rounds to the nearest int

Useful links

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Python 1.1 Numpy Cheat Sheet