Show Menu
Cheatography

Numpy Cheat Sheet Cheat Sheet (DRAFT) by

Numpy cheats sheet test

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Creating Arrays

Create simple array
np.arr­ay(­[list])
Array filled with zeros
np.zer­os(­size)
Array filled with ones
np.one­s(size)
Array filled with a number
np.ful­l(s­ize­,nu­mber)
Array from a range of numbers (last not included)
np.ara­nge­(st­art­,st­op,­step)
Array from a range of numbers (last included)
np.lin­spa­ce(­sta­rt,­sto­p,c­ount)
Array filled with random numbers (float)
np.ran­dom.ra­ndo­m(size)
Array filled with random numbers (int)
np.ran­dom.ra­ndi­nt(­sta­rt,­sto­p,size)
Create identity Matrix
np.eye(k)
Create empty Matrix
np.emp­ty(k)
Size can be the following :
1) one-di­men­sional (Array) : where size = k = (k)
eg : np.zer­os(2) , creates an array of 2 elements
2) two-di­men­sional (Matrix) : where size = (a,b)
eg : np.zer­os(­(2,3)) , creates a matrix of 2 rows and one column
3) n-dime­nsional : where size = (a,b,c....etc) > 2
eg : np.zer­os(­(2,­3,5)) , creates a 3 dimens­ional array

Reading Array From Text File

Inspecting the file (without reading it)
!head file_s­ource
!head data/a­rra­y_f­loa­t.csv
Loading File Into Array
np.loa­dtx­t(s­ource, delimiter, dtype)
B = np.loa­dtx­t("d­ata­/ar­ray­_in­t.c­sv", delimiter = "­,", dtype=int)
Loading Array Into File
np.sav­etx­t(s­ource, array, fmt ,delim­iter)
np.sav­etx­t("d­ata­/my­_ar­ray.cs­v", B, fmt="%3­i", delimi­ter­=",")
Make sure that you are typing the right datatype of the text file or you will receive an error

The fmt keyword is used to specify the spacing in our text file where :
-
fmt = "­%3i­"
means 3 spaces between row elements (numbers are integers)
-
fmt = "­%5.1­i"
means 5 spaces between row elements and only one number after floating point

Numpy Array Attributes

Returns number of dimensions
array.ndim
Returns size of each dimension
array.s­hape
Returns number of elements
array.size
Returns type of elements
array.d­type
Returns size of each element in bytes
array.i­te­msize
Returns size of all elements in bytes
array.n­bytes

Array Indexing

Accessing array element
array_­nam­e[i­ndex]
x1[5]
Modify array element
array_­nam­e[i­ndex] = new_value
x1[5] = 69
Accessing matrix element
matrix­_na­me[­row­,co­lumn]
x2[2, -1]
Modify matrix element
matrix­_na­me[­row­,co­lumn] = new_value
x2[2, -1] = -5
Accessing sub-arrays
array_­nam­e[s­tar­t,s­top­,st­ep=1]
x[1:5]
x[1:5:3]
Accessing multi-dim arrays
array_­nam­e[r­ow_­ran­ge,­col­umn­_range]
x2[::-1, ::-1]
Creating subarrays as no-copy views
sub_array = origin­al_­arr­ay[­slice]
x2_sub = x2[:2, :2]
Creating subarrays as copy views
sub_array = origin­al_­arr­ay[­sli­ce].copy()
x2_sub­_copy = x2[:2, :2].copy()
We can access multiple matrix rows and columns when both row and columns are lists :

x2[[1,­2],­[1,1]] -> this means access # Second and Third row ([1,2]) , Second and Second column

Reshaping of Arrays

Reshape array
array.r­es­hap­e((­row­s,c­olu­mns))
x2 = x1.res­hap­e((3, 3))
Reshape into row vector
array.r­es­hap­e((­1,c­olu­mns))
x2 = x1.res­hap­e((1, 3))
Reshape into row vector using np.newaxis
array[­np.n­ew­axis, :]
x2 = x1[np.n­ew­axis, :]
Reshape into column vector
array.r­es­hap­e((­row­s,1))
x2 = x1.res­hap­e((3, 1))
Reshape into column vector using np.newaxis
array[:, np.new­axis]
x2 = x1[:, np.new­axis]
It is very important to note that number of rows multiplied by the number of columns must be equal to the number of array elements

Array Concat­enation and Splitting

Concat­enate Arrays vertically (default)
np.con­cat­ena­te(­[x1­,x2­,x3....])
np.con­cat­ena­te([x, y])
Concat­enate Arrays horizo­ntally
np.con­cat­ena­te(­[x1­,x2­,x3....] , axis=1)
np.con­cat­ena­te([x, y], axis=1)
Stack Arrays vertically
np.vst­ack­([x1, x2,x3....])
np.vst­ack([x, y])
Stack Arrays horizo­ntally
np.hst­ack­([x1, x2,x3....])
np.hst­ack([x, y])
Spliting Arrays
np.spl­it(­array, [first­_sp­lit­_point, second­_sp­lit­_point , third_­spl­it_­poi­nt....])
x1, x2, x3 = np.spl­it(x, [3, 5])
Splitting Matrixes vertically
np.vsp­lit­(ma­trix, [frist­_ro­w_i­nde­x,s­eco­nd_­row­_in­dex....])
upper, lower = np.vsp­lit­(grid, [2])
Splitting Matrixes horizo­ntally
np.hsp­lit­(ma­trix, [frist­_co­l_i­nde­x,s­eco­nd_­col­_in­dex....])
left, right = np.hsp­lit­(grid, [2])
NOTE :
concat­enate or stack vertically : must have same number of columns
concat­enate or stack horizo­ntally : must have same number of rows

NOTE 2 :
The number of split points when splitting an array depends on the number of arrays generated where :
Number of split points = number of arrays outputted - 1

x1, x2, x3 = np.spl­it(x, [3, 5])
-> split on the third element to x1 , split on the fifth element to x2 (from forth element) , give the rest to x3

Numpy Operations & UFuncs

Addition
Array + number
x = [0 1 2 3]
x+5
output : [5 6 7 8]
Substr­action
Array - number
x = [5 6 7 8]
x-5
output : [0 1 2 3]
Multip­lic­ation
Array * number
x*2
output : [0 2 4 6]
Division
Array / number
x/2
output : [0,0.5­,1,1.5]
Floor Division
Array // number
x//2
output : [0,0,1,1]
Expone­ntial
Array ** number
x*2
output : [0 1 4 9]*
Modulus
Array % number
x % 2
output : [0 1 0 1]
Dot Multip­lic­ation
matrix1 @ matrix2
check the notes
UFunc Addition
np.add­(ar­ray­,nu­mber)
np.add(x,5)
output : [5 6 7 8]
UFunc Substr­action
np.sub­tra­ct(­arr­ay,­number)
np.subtract(x,5)
output : [0 1 2 3]
UFunc Multip­lic­ation
np.mul­tip­ly(­arr­ay,­number)
np.multiply(x,2)
output : [0 2 4 6]
UFunc Dot
np.dot­(ma­tri­x1,­mat­rix2)
check the notes
UFunc Absolute Value
np.abs(x)
x = np.arr­ay([-2, -1, 0, 1, 2])
np.abs(x)
output : [2,1,0­,1,2]
UFunc Sinus
np.sin(x)
self-e­xpl­anatory
UFunc Cosinus
np.cos(x)
self-e­xpl­anatory
UFunc Tangent
np.tan(x)
self-e­xpl­anatory
UFunc Arcsinus (inverse)
np.arc­sin(x)
self-e­xpl­anatory
UFunc Arccosinus (inverse)
np.arc­cos(x)
self-e­xpl­anatory
UFunc Arctangent (inverse)
np.arc­tan(x)
self-e­xpl­anatory
For matrix dot operation, the number of columns in the first matrix (array) must be equal to the number of rows in the second matrix (array).

Aggregates Types

array.u­fu­nc.r­ed­uce()
This aggregate help apply the universal function between elements and returns (reduces) the result toone value (last value)
x = np.ara­nge(1, 6)
np.add.reduce(x)
result : 15
array.u­fu­nc.a­cc­umu­late()
This aggregate help apply the universal function between elements but returns the result of each element inside a list
x = np.ara­nge(1, 6)
np.add.accumulate(x)
result : array([ 1, 3, 6, 10, 15])
array.u­fu­nc.o­uter()
This aggregate help apply the ufunc operation to all pairs (a, b) with a in A and b in B.
np.mul­tip­ly.o­ut­er([1, 2, 3], [4, 5, 6])
result : array([[ 4, 5, 6],[ 8, 10, 12],[12, 15, 18]])
The aggregate for multip­lic­ation is : array.m­ul­tip­ly.r­ed­uce()
The aggregate for subtra­ction is : array.s­ub­tra­ct.r­ed­uce()
The aggregate for division is : array.d­iv­ide.re­duce()
The aggregate for addition array.a­dd.re­duce() is equivalent to np.sum­(array)
The aggregate for multip­lic­ation array.m­ul­tip­ly.r­ed­uce() is equivalent to np.pro­d(a­rray)

Other Aggregate Function

np.sum
np.nansum
Compute sum of elements
np.prod
np.nanprod
Compute product of elements
np.mean
np.nanmean
Compute mean of elements
np.std
np.nanstd
Compute standard deviation
np.var
np.nanvar
Compute variance
np.min
np.nanmin
Find minimum value
np.max
np.nanmax
Find maximum value
np.argmin
np.nan­argmin
Find index of minimum value
np.argmax
np.nan­argmax
Find index of maximum value
np.median
np.nan­median
Compute median of elements
np.per­centile
np.nan­per­centile
Compute rank-based statistics of elements
np.any­(co­ndi­tion)
N/A
Evaluate whether any elements are true
np.all­(co­ndi­tion)
N/A
Evaluate whether all elements are true

Python Broadc­asting

Broadc­asting allows you to perform operations on arrays with different shapes in a way that makes sense, without the need to explicitly reshape or replicate the arrays

Rules of Broadc­asting :
Not any operation is permis­sible when both of arrays have different shapes where :

Rule 01 - Padded Dimensions :
when two have different dimensions we can always add a padding dimension to the left , meaning pushing the first one to the right (5,) => (1,5) ( adding new axis )

Rule 02 - Stretching Dimensions :
if any of the aligned dimensions is 1 , then we can stretch that dimension to match the other one

A.shape = (2,3) B.shape = (1,3) => we can stretch B to make it (2,3)

Example :
A.shape = (3, 1)
B.shape = (3,)
We can padd B => B.shape = (1,3)
This means we can strech both A and B (Both have 1 as aligned dimension) where :
A.shape = (3, 1) => (3,3)
B.shape = (1,3) => (3,3)
if any of these rules doesn't apply then broadc­asting is not possible

Numpy Fancy Indexing

Select Multiple Elements Using Fancy Indexing
select­_el­ements = array[­lis­t_o­f_i­ndexes]
select­_el­ements = array1[[1, 2, 5, 7]]
Fancy Indexing for Sorting NumPy Array
sorted­_array = array[­np.a­rg­sor­t(a­rray)]
np.argsort retruns indexes of array sorted (to make it reverse we just write np.arg­sort(- array))
Fancy Indexing to Assign New Values to Specific Elements
array[­lis­t_o­f_i­ndices] = new_values
array1[[1, 3, 6]] = [10, 20, 30]
Fancy Indexing on N-d Arrays
new_array = array1­[li­st_­of_­row­_in­dices, list_o­f_c­ol_­ind­ices]
select­ed_rows = array1­[[0,2], :]
In fancy indexing we can either put a list of indices or numpy array of indices

Numpy Sorting

Items sort 1
np.sor­t(a­rray)
by default it sorts the values in ascending order
Items sort 2
array.s­ort()
this one changes the content of the original array
Index Sorting
np.arg­sor­t(a­rray)
this returns the order of the indexes sorted ascend­ingly
Item Sorting using Index sorting
array[­np.a­rg­sor­t(a­rray)]
this returns the items sorted ascend­ingly
Matrix Sorting
np.sor­t(m­atr­ix,­axi­s=0/1)
this sorts either each matrix columns (axis=0) or each matrix row (axis=1)
Partit­ioning (Partial Sorting)
np.par­tit­ion­(array, n)
this takes the smallest n numbers to the left in no particular order (3 numbers if n = 3)
Matrix Partit­ioning
np.par­tit­ion­(array, n, axis=0/1)
this takes the smallest n numbers (of each row or column) to the left in no particular order
Index Partit­ioning
np.arg­par­tit­ion­(array, n, axis=0/1)
this is like argsort() but for matrixes

Array as a Data Structure

Empty Structured Arrays
np.zer­os(­str­uct­_size, dtype=­{'n­ame­s':­(co­l1,­col­2,c­ol3), 'forma­ts'­:(f­orm1, form1, form1)})
data = np.zer­os(4, dtype=­{'n­ame­s':­('name ', 'age', 'weight'), 'forma­ts'­:('­U10', 'i4', 'f8’)})
#output :
[('', 0, 0.) ('', 0, 0.) ('', 0, 0.) ('', 0, 0.)]
Adding Items to Structured Arrays
# List should be same size as the array structure
struc_array[colname] = list
data['­name'] = name4
data['age'] = age4
data['weight'] = weight4
#output:
[('Alice', 25, 55. ) ('Bob', 45, 85.5) ('Cathy', 37, 68. ) ('Doug', 19, 61.5)]
Accessing array strucure columns
struct­_ar­ray­[co­lname]
data['­name’]
Accessing array strucure rows
struct­_ar­ray­[index]
# Accessing last row
data[-1]
Sorting array strucure
np.sor­t(s­tru­ct_­array, order=­[co­umns])
np.sor­t(data, order=­['age', 'name’])
Partit­ioning Structured Arrays
np.par­tit­ion­(st­ruc­t_a­rray, n, order=­[co­lumns])
# getting the smallest 2 weights to the left
np.partition(data, 2, order=['weight’])
array([('Alice', 25, 55. ), ('Doug', 19, 61.5), ('Cathy', 37, 68. ), ('Bob', 45, 85.5)]
Defining a new data type
np.dty­pe(­[tu­ple­(co­l_n­ame­,ty­pe)])
np.dty­pe(­[('­name', 'U10'), ('age', 'i4'), ('weight', 'f8’)])
Appending new row
np.app­end­(st­ruc­t_a­rray, numpy_­array], dtype=dt))
np.app­end­(data, np.arr­ay(­[('­Joh­n',­64,­89)], dtype=dt))
[('Alice', 25, 55. ) ('Bob', 45, 85.5) ('Cathy', 37, 68. ) ('Doug', 19, 61.5) ('John', 64, 89. )]
The Format For Empty Structured Arrays are as follows :
- Integer : np.int32 or 'i4'
- Float : np.float32 or 'f8'
- String : np.str_ or 'U10'