NumPy Cheat Sheet

A Programming › Python cheat sheet in English by Lavanya-M22

NumPy cheatsheet by Lavanya and

15,153 views 2,828 downloads 11 Sep 2021 History

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

ndarra­y.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.mea­n(a­rr,­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.arr­ay(­[(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.ran­dom.ra­ndi­nt(­low­,high)
Return random integers from low (inclu­sive) to high (exclu­sive)
np.lin­spa­ce(­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.sub­tra­ct(­arr_1, arr_2)
Subtract arguments, elemen­t-wise
np.mul­tip­ly(­arr_1, arr_2)
Multiply arguments, elemen­t-wise
np.div­ide­(arr_1, arr_2)
Divide arguments, elemen­t-wise
np.pow­er(­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

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.