import numpy as np
a = np.array([0.0, 1, 2, 3, 4]) # 1-d Array
np.shape(a) # returns the dimensions
np.zeros((10,3)) #for generating array of zeros
np.ones((10,3)) #for generating an array of ones

np.ravel(a) # creates a 1-D array from a 2-D array - does not copy
a.flatten() # same as ravel but copies

a.astype(int) #converts a array to ineger arrary

a.squeeze() #function removes singleton dimensions from the array

#Random numbers
np.random.seed(123) # Setting the seed
np.random.normal(0,1,(2,3)) # Generate Normal random number matrix

print(np.arange(5))
print(np.arange(4, 10, 1.25))
print(np.reshape(np.arange(6),(2,3)))

print('\nConvert NumPy Array to string')
print(str(np.arange(1,3)).strip('[]')) 

print('\nConvert NumPy Array to List')
print(np.arange(10).tolist())
## [0 1 2 3 4]
## [ 4.    5.25  6.5   7.75  9.  ]
## [[0 1 2]
##  [3 4 5]]
## 
## Convert NumPy Array to string
## 1 2
## 
## Convert NumPy Array to List
## [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
import numpy as np
#Deleting rows or columns in an array
y=np.reshape(np.arange(24),(6,4))
print(np.size(y),np.ndim(y))
print(y)
print(np.delete(y,(2,3),0))
print(np.delete(y,(2,3),1))
## (24, 2)
## [[ 0  1  2  3]
##  [ 4  5  6  7]
##  [ 8  9 10 11]
##  [12 13 14 15]
##  [16 17 18 19]
##  [20 21 22 23]]
## [[ 0  1  2  3]
##  [ 4  5  6  7]
##  [16 17 18 19]
##  [20 21 22 23]]
## [[ 0  1]
##  [ 4  5]
##  [ 8  9]
##  [12 13]
##  [16 17]
##  [20 21]]
import numpy as np
#Shuffle (changes the variable) and Permutation (Creates a copy) function
x= np.array([ 4.  ,  5.25,  6.5 ,  7.75,  9.  ])
print(np.random.permutation(x))
print(np.random.shuffle(x))
## [ 9.    5.25  4.    7.75  6.5 ]
## None
import numpy as np
temp = [2,3]; 
print(np.tile(temp,[2,3]) )
## [[2 3 2 3 2 3]
##  [2 3 2 3 2 3]]
import numpy as np
arow=np.arange(0,4)
brow=np.arange(3,7)
print(arow)
print(brow)
print(np.vstack((arow,brow)))
print(np.hstack((arow,brow)))
## [0 1 2 3]
## [3 4 5 6]
## [[0 1 2 3]
##  [3 4 5 6]]
## [0 1 2 3 3 4 5 6]
#Upper and Lower triangular matrix operations with diagonal
import numpy as np
a = np.arange(16).reshape(4, 4) 
print(a)
print(a[np.triu_indices(len(a[:,0]),1)])
## [[ 0  1  2  3]
##  [ 4  5  6  7]
##  [ 8  9 10 11]
##  [12 13 14 15]]
## [ 1  2  3  6  7 11]
import numpy as np
#Selecting specific column in each row from a matrix using an array
matA = np.reshape(np.arange(20),(4,5))
selMat = tuple(np.array([[0,1,2,3],[0,2,1,4]]))
print(matA)
print(selMat)
print(matA[selMat])
## [[ 0  1  2  3  4]
##  [ 5  6  7  8  9]
##  [10 11 12 13 14]
##  [15 16 17 18 19]]
## (array([0, 1, 2, 3]), array([0, 2, 1, 4]))
## [ 0  7 11 19]
print(aa)
bb=np.hsplit(aa,[3])
print(bb[0])
print(bb[1])
## [[ 1.  0.  0.  0.]
##  [ 0.  1.  0.  0.]
##  [ 0.  0.  1.  0.]
##  [ 0.  0.  0.  1.]]
## vsplit
## [[ 1.  0.  0.]
##  [ 0.  1.  0.]
##  [ 0.  0.  1.]
##  [ 0.  0.  0.]]
## [[ 0.]
##  [ 0.]
##  [ 0.]
##  [ 1.]]
## similarly for hsplit
print(a)
print(b)
print(a*b)
print([a.shape,b.shape,(a*b).shape])
Multiplying a 1 dimensional array with two-dimensional array - in an element wise fashion - the length of the 1D array needs to be equal to the number of columns in the 2D array. See the image below for example. Note that this behavior is opposite of that in R.
[0 1 2]
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
[[ 0  2  6]
 [ 0  5 12]
 [ 0  8 18]
 [ 0 11 24]]
[(3L,), (4L, 3L), (4L, 3L)]
print(aa)
print(np.sum(aa,axis=0))
print(np.sum(aa,axis=1))
print(aa)
print(bb)
print(cc)
print(np.where(aa,bb,cc))
np.sum function
[[0 1]
 [0 5]]
[0 6]
[1 5]
np.where function
[[True, False], [True, True]]
[[1 2]
 [3 4]]
[[9 8]
 [7 6]]
[[1 8]
 [3 4]]