Skip to content

Instantly share code, notes, and snippets.

@JeyDi
Last active December 6, 2020 18:27
Show Gist options
  • Select an option

  • Save JeyDi/70f6982b3b0850ec0ba3d4f38ee97661 to your computer and use it in GitHub Desktop.

Select an option

Save JeyDi/70f6982b3b0850ec0ba3d4f38ee97661 to your computer and use it in GitHub Desktop.
Numpy cheatsheet and useful functions and tips
## Numpy Cheatsheet and useful operations
## Reference:
# https://gist.github.com/flyudvik/ffc5f949d9da4aec7bc3ed96cf0038d6
# https://towardsdatascience.com/numpy-cheat-sheet-4e3858d0ff0e
#Import numpy
import numpy as np
#####################
### ARRAY METHODS ###
#####################
array_definition = (rows, columns, depth, 4th dim, 5th dim, ...)
# create array (2-dimensional)
np.array([[1,2,3,4,5],[6,7,8,9,10]],float)
# create array with range
np.array(range(10))
# create array with arange and specific type
np.arange(7,dtype=float)
# create array with 0 and 1
np.ones((3,3))
np.zeros((3,4),dtype=int)
# create array same dimension(shape) of another one
np.ones_like(np.array(range(10)))
np.zeros_like(np.ones((3,3)))
# fill array with some values
a = np.ones((3,3))
a.fill(10)
## Array Infos
a = np.array([[1,2,3,4,5],[6,7,8,9,10]],float)
a.shape #shape of the array
a.dtype #type of array
len(a) #len of array
# presence of element in array
( 8 in a , 11 in a )
## Access Array Methods (extract)
matx = np.array([[12,32,42,12],[2,34,55,21],[45,99,10,67]],dtype=int)
matx[3,0] = 100 # replace 4the element in the array
matx[1,2] # rows and columns start with zero
# copy an array
matx2 = matx.copy()
# tolist: transform array to list
mylist = matx.tolist()
# from array to string
string = matx.tostring()
# from string to array
np.fromstring(string)
# usefull test example (3 dimension matrix)
x = np.array([[['a11','b11','c11'],['a12','b12','c12'],['a13','b13','c13']],
[['a21','b21','c21'],['a22','b22','c22'],['a23','b23','c23']]
], dtype=str)
#######################################
### RESHAPING AND CHANGE DIMENSIONS ###
#######################################
# Slicing, select, get some rows and columns template
data[from:to, from:to]
# select all rows and all columns except the last one
X = [:, :-1]
# select all rows and index just the last column
y = [:, -1]
# reshape 1D array into 2D array
data = array([11, 22, 33, 44, 55],dtype=int)
data = data.reshape((data.shape[0], 1))
matx = np.array(range(15),dtype=int)
matx = matx.reshape((3,5)) # creates new array
# reshape 1D array into 3D array
data = array([11, 22, 33, 44, 55],dtype=int)
data = data.reshape((data.shape[0], 1, 1))
# reshape 2D array into 3D array
data = np.array([[11, 22],[33, 44],[55, 66]],dtype=int)
data = data.reshape((data.shape[0], data.shape[1], 1))
# reshape 3D Array into 4D Array
x = np.array([[['a11','b11','c11'],['a12','b12','c12'],['a13','b13','c13']],
[['a21','b21','c21'],['a22','b22','c22'],['a23','b23','c23']]
], dtype=str)
x = x.reshape(x.shape[0],x.shape[1],x.shape[2], 1)
# you can always reshape to a custom dimension
x = x.reshape(3, 3, 2, 1)
# reshape 3D array into 2D array
img = np.array([[[155, 33, 129],[161, 218, 6]],
[[215, 142, 235],[143, 249, 164]],
[[221, 71, 229],[56, 91, 120]],
[[236, 4, 177],[171, 105, 40]]],dtype=int)
img = img.reshape((img.shape[0]*img.shape[1]), img.shape[2])
img = img.transpose()
img.transpose(2,0,1).reshape(3,-1) #or with this handcrafted method
# reshape 4D array into 3D array
data = np.random.randint(0,9,(256,128,4,200))
m,n = data.shape[::2]
data_new = data.transpose(0,3,1,2).reshape(m,-1,n)
# another method
data_new = np.rollaxis(data,3,1).reshape(m,-1,n)
# reshape 4D array into 2D array
data = np.random.randint(0,9,(256,128,4,200))
np.reshape(data, (data.shape[0] * data.shape[1],data.shape[2] * data.shape[3]))
# Transpose array
matx = np.array([[1,2,3],[4,5,6]],dtype=int)
matx2 = matx.transpose()
# Flattern: generate list (vector) from array
matx = np.array([[1,2,3],[4,5,6]],dtype=int)
matx.flatten()
matx.reshape([1, matx.shape[0] * matx.shape[1]]) #you can use also reshape function
# Flattern:
# concatenating arrays
a = np.array(range(3))
b = np.array(range(5))
c = np.array(range(2))
np.concatenate((a,b,c))
# concatenating with higher dimensions
a = np.array([[1,2,3],[2,3,4],[3,4,7]])
b = np.array([[9,8,7],[6,5,7],[5,4,3]])
np.concatenate((a,b),axis=0) # vertical
np.concatenate((a,b),axis=1) #horizontal
# increasing dimensions of the array
a = np.array([[1,2,3],[2,3,4]])
a[np.newaxis,:]
# Insert new columns or raw in an existing Numpy Array
import numpy as np
N = 3
A = np.eye(N)
np.c_[ A, np.ones(N) ] # add a column
np.c_[ np.ones(N), A, np.ones(N) ] # Add two columns
np.r_[ A, [A[1]] ] # add a row
##########################
### ARRAY CALCULATIONS ###
##########################
a = np.array([[1,2,3],[4,5,6]])
# sum all elements
np.sum(a)
# multiply all elements
np.prod(a)
np.mean(a)
np.max(a)
np.min(a)
np.var(a)
np.std(a)
#calculations over columns or rows
# axis = 0 : over rows
# axis = 1 : over columns
np.mean(a,axis=0)
np.var(a,axis=1)
np.std(a,axis=1)
# return index of min or max element
np.argmin(a)
np.argmax(a)
# return unique elements
np.unique(a)
# extracting diagonals
a = np.array([range(1,5),range(6,10)])
a.diagonal()
########################
### ARRAY OPERATIONS ###
########################
# general comparisons elementwise
a = np.array([5,6,7,8])
b = np.array([1,2,3,6])
print(a>b)
print(a==b)
print(a<=b)
print(b>2)
# logical and , or , not
np.logical_and(a>2,b<5)
np.logical_or(a>b,b>2)
np.logical_not(b)
# creating new array using conditions on existing arrays
np.where(a>2,3,4)
# checking if values are zero or NaN
a.nonzero()
np.isnan(b)
# using conditions to select elements in array
b[b>2]
# use another array to select elements from an array
# can also be used with multi dimensional arrays
# np.take is also used to perfrom same operation
a = np.array([1,2,3,4])
b = np.array([3,4,5,2,7,8,9,0,2,3,5])
b[a]
######################
### RANDOM NUMBERS ###
######################
# random numbers are generted from a seed value
rd.seed(42)
# random numbers b/w 0 and 1
rd.rand(2,3) # where 2,3 are matrix dimensions
# generating random integers only
# randint(min,max)
rd.randint(2,10)
# shuffling numbers in a list or an array
a = np.array([[1,2,3],[4,5,6]])
rd.shuffle(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment