import numpy as np
np.__version__
my_list = [1,2,3]
arr = np.array(my_list)
arr
np.arange(0,10,1) #Start, stop, step #Does not include 10
np.linspace(0,10,10) # 10 points linearly spaced between 0 and 10 (inclusive)
np.zeros((3,5)) #or np.ones
np.random.normal(1,0.01,(3,5)) #mean,std,size
np.random.randint(0,10,(3,5)) #random int between 0 and 10(excluded)
##Reshape
arr = np.random.randint(0,10,(3,5))
print(arr)
print(arr.reshape(-1))
print(arr.reshape((5,-1)))
#Seed ensures that the sequence of random numbers generated is same in all computers
np.random.seed(101)
print(np.random.randint(0,100,10))
print(np.random.randint(0,100,10))
np.random.seed(101)
print(np.random.randint(0,100,10))
print(np.random.randint(0,100,10))
np.random.seed(101)
arr = np.random.randint(0,100,10)
arr
arr.max()
arr.min()
arr.mean()
#Index location of max value
arr.argmax()
#Index location of min value
arr.argmin()
#Slicing
mat = np.arange(0,100).reshape(10,10)
mat
mat[0,:] #First row
mat[[0,2]] #First and third row
mat[:,0] #First column
mat[:,[0,2]] #First and third column
mat[:2,:3] #slice first 2 rows and 3 columns
mat[2:4,5:-2] #slice from 2nd until 4th row, from 5th until last second column
mat[:5:2,:5:2]#slice every 2nd element from first 5 rows and column
mat[::2,::2]#slice every 2nd element in whole matrix
mat[2::2,3::2] #every 2nd element starting from index 2
mat[::-1,::-1] #Reversing the matrix
mat[3::-1,3::-1] #pick up the first 3 rows and column and reverse them
#Masking
my_filter = mat > 50
my_filter
#Mask all values greater than 50
mat[my_filter] #mat[mat>50]
mat[my_filter] = mat[my_filter]*-10
mat
np.where can also be used (returns a new array without modifying original)
mat = np.arange(0,100).reshape(10,10)
np.where(mat%2==0,-1,mat) #np.where(cond,if_true,else) #does not change mat
np.where(mat%2==0)#Returns indicies
Getting the elements from those indices
mat[idx]
Getting indicies zipped
idx = np.where(mat%2==0)
list(zip(idx[0],idx[1]))
#Get the idx of those elements with np.where
idx = np.where(mat%2==0)
def func(X):
X = X + X[::-1]*1.5
return X
mat[idx] = func(mat[idx])
print(mat)
#reset matrix
mat = np.arange(0,100).reshape(10,10)
a = np.arange(0,15,1).reshape(5,3)
a
Normalizing all columns
#axis=0 for columns
a = np.apply_along_axis(lambda x: (x - x.min())/ (x.max()-x.min()) ,axis=0,arr=a)
a
Normalizing all rows
#axis=1 for rows
a = np.arange(0,15,1).reshape(5,3)
a = np.apply_along_axis(lambda x: (x - x.min())/ (x.max()-x.min()) ,axis=1,arr=a)
a
When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions (from right), and works its way forward. Two dimensions are compatible when
1) they are equal, or
2) one of them is 1
If these conditions are not met, a ValueError: frames are not aligned exception is thrown,
Eg,
Image (3d array): 256 x 256 x 3
Scale (1d array): 3
Result (3d array): 256 x 256 x 3
A (2d array): 5 x 4
B (1d array): 1
Result (2d array): 5 x 4
A (3d array): 15 x 3 x 5
B (3d array): 15 x 1 x 5
Result (3d array): 15 x 3 x 5
arr = np.ones((5,4))
arr
arr + 1 #(5,4) + (1,)
arr + [1,2,3,4] #(5,4) + (4,)
arr + np.array([1,2,3,4,5]).reshape((5,1)) #(5,4) + (5,1)
features = np.random.randint(0,101,(50,3))
labels = np.random.randint(0,5,(50,1))
data = np.concatenate((features,labels),axis=1)
data
x = np.linspace(0,10,1000)
x.shape
Sample 10 random index
sample_size=10
rand_ind = np.random.randint(0,len(x),size=sample_size)
rand_ind
Grab the samples corresponding to those indices
x[rand_ind]