Skip to main content

NumPy CheatSheet for Developers

Introduction-What-is-NumPy?

A library consisting of multidimensional array objects and a collection of routines for processing those arrays.

Key and Imports

We use following shorthand in the cheat sheet:

CommandDescription
npimport numpy library
np.arrayThe array object in NumPy
np.array.shapeThe shape of an array is the number of elements in each dimension.
np.array.reshapeReshaping means changing the shape of an array(Example 1-D to 2-D)
np.zeros(3)1D array of length 3 all zeros
np.zeros((2,3))2D array of all zeros
np.zeros((3,2,4))3D array of all zeros
np.full((3,4),2)3x4 array with all values 2
np.random.rand(3,5)3x5 array of random floats between 0 and 1
np.ones((3,4))3x4 array with all values 1
np.eye(4)4x4 array of 0 with 1 on diagonal

Data Types In NumPy

NumPy has some extra data types, and refer to data types with one character, like i for integers, u for unsigned integers etc.

Below is a list of all data types in NumPy and the characters used to represent them.

CommandDescription
iinteger
b boolean
uunsigned integer
ffloat
ccomplex float
mtimedelta
Mdatetime
Oobject
Sstring
U unicode string
Vfixed chunk of memory for other type ( void )

Save And Load Data

Text/CSV files:

CommandDescription
np.loadtxt('New_file.txt')From a text file
np.genfromtxt('New_file.csv',delimiter=',') From a CSV file
np.savetxt('New_file.txt',arr,delimiter=' ') Writes to a text file
np.savetxt('New_file.csv',arr,delimiter=',') Writes to a CSV file

Properties:

CommandDescription
array.sizeReturns number of elements in array
array.shapeReturns dimensions of array(rows,columns)
array.dtypeReturns type of elements in array

Operations

KeywordsDescriptionAction
np.copy(array)Copies array to new memory array.Copying
view(dtype)Creates view of array elements with type dtypeCopying
array.sort()Sorts arraySorting
array.sort(axis=0)Sorts specific axis of arraySorting
array.reshape(2,3)Reshapes array to 2 rows, 3 columns without changing data.Sorting
np.append(array,values)Appends values to end of arrayAdding
np.insert(array,4,values)Inserts values into array before index 4Adding
np.delete(array,2,axis=0)Deletes row on index 2 of arrayRemoving
np.delete(array,3,axis=1)Deletes column on index 3 of arrayRemoving
np.concatenate((array1,array2),axis=0)Adds array2 as rows to the end of array1Combining
np.concatenate((array1,array2),axis=1)Adds array2 as columns to end of array1Combining
np.split(array,3)Splits array into 3 subarrays
a[0]=5Assigns array element on index 0 the value 5Indexing
a[2,3]=1Assigns array element on index [2][3] the value 1Indexing
a[2]Returns the element of index 2 in array a.Subseting
a[3,5]Returns the 2D array element on index [3][5]Subseting
a[0:4]Returns the elements at indices 0,1,2,3Slicing
a[0:4,3]Returns the elements on rows 0,1,2,3 at column 3Slicing
a[:2]Returns the elements at indices 0,1Slicing
a[:,1]Returns the elements at index 1 on all rowsSlicing

Array Mathematics

Operation typeSyntaxAction
Additionnp.add(a,b)Arithmetic Operations
Subtractionnp.subtract(a,b)Arithmetic Operations
Multiplicationnp.multiply(a,b)Arithmetic Operations
Divisionnp.divide(a,b)Arithmetic Operations
Exponentiationnp.exp(a)Arithmetic Operations
Square Rootnp.sqrt(b)Arithmetic Operations
Element-wisea==bComparison
Array-wisenp.array_equal(a,b)Comparison

Functions

Operation TypeSyntax
Array-wise Suma.sum()
Array-wise min valuea.min()
Array row max valuea.max(axis=0)
Meana.mean()
Mediana.median()