Numpy Tutorial


In [1]:
import numpy as np  # include numpy package and renaming it to be np instead of numpy

Different Initialization Methods


In [44]:
array1 = np.array([[2,3,5],[7,8,9]]) # hard coded initialization of size 2x3
array2 = np.empty((2,3),dtype='float32') # empty array of size 2x3 , data is float32
array3 = np.zeros((2,3)) # array of zeros of size 2x3
array4 = np.ones((2,3)) # array of ones of size 2x3
array5 = np.random.rand(2,3) # array of random numbers of size 2x3
array6 = np.eye(3,3) #array of ones on diagonal and zer elsewhere 
array7 = np.identity(3) #square identity array of size 3x3 
array8 = np.empty_like(array1)   # Create an empty matrix with the same shape as array1e
array9 = np.copy(array1) # it'll genearate a copy of array1 with the same data 
array10 = np.zeros_like(array1) # It'll generate array of zeros with the same shape as array1
array11 = np.ones_like(array1) # It'll generate array of ones with the same shape as array1
array12 = np.full((2,3),3,dtype='float') # array of size 2x3 filled with value=3
vectorOfNumbers = np.arange(1,10,1) # generate vector of numbers from 1 to 9 with step 1
vectorOfLineSpacedPoints = np.linspace(1,10,5) #Return evenly spaced numbers over a specified interval
vectorOfLogSpacedPoints = np.logspace(1,10,5) #Return log spaced numbers over a specified interval

Array Size and Shape


In [121]:
array2D  =np.array([[2,3,5],[7,8,9]]) #initialze 2D array of shape 2x3
print array2D
print "Number of dimensions of the array is ", array2D.ndim #print the dimension of the array 
print "The shape of the 2D array is : ",array2D.shape # print array shape
print "Number of elements inside the array are : ",array2D.size  # print array size
print "Number of rows are: ",array2D.shape[0]  # print array number of rows 
print "Number of columns are: ",array2D.shape[1]  # print array number of columns 
print "Element at 2nd row and 3rd column is : ",array2D[1,2] #print element at 2nd row and 3rd column


[[2 3 5]
 [7 8 9]]
Number of dimensions of the array is  2
The shape of the 2D array is :  (2, 3)
Number of elements inside the array are :  6
Number of rows are:  2
Number of columns are:  3
Element at 2nd row and 3rd column is :  9

Array Slicing


In [98]:
firstCol = array2D[:,0]     #extract 1st column of array
print "First column is: ", firstCol             #print 1st column
firstRow = array2D[0,:]     #extract 1st row of array
print "First row is: ", firstRow  #print 1st row
subMatrix = array2D[0:2,0:2]
print "sub matrix is: ", subMatrix  #print first 2 rows and columns


First column is:  [2 7]
First row is:  [2 3 5]
sub matrix is:  [[2 3]
 [7 8]]

Initialize Array Dynamically


In [100]:
numberOfRows = 100       #Number of rows
numberOfColumns = 400    #Number of columns 
emptyMatrix = np.empty((numberOfRows,numberOfColumns)) #Initialize an empty array with very small random numbers 

# This section to fill the array dynamically through two for loops 
for row in xrange(emptyMatrix.shape[0]):   #first loop to loop over the number of rows
    for col in xrange(emptyMatrix.shape[1]): #second loop to loop over the number of columns
        emptyMatrix[row,col] = row+col # fill every element of the array with the sum of row number + column number

Element Wise Mathematical Operations


In [140]:
A = np.array([[1,2,3],[3,4,6]])
B = np.array([[1,2,3],[2,4,5]])
 
print "Sum of 2 matrices: ", A + B        #Element wise sum
print "Sum of 2 matrices: ", np.add(A, B)        #Element wise sum using numpy function 
print "Subtraction of 2 matrices: ", A - B #Element wise subtraction
print "Subtraction of 2 matrices: ", np.subtract(A, B) #Element wise subtraction
print "Multiplication of 2 matrices (element wise) : ", A*B  #element wise multiplication 
print "Multiplication of 2 matrices (element wise) : ", np.multiply(A,B)#element wise multiplication using numpy functions
print "Division of 2 matrices (element wise) : ", A/ np.float32(B)#element wise division 
print "Division of 2 matrices (element wise) : ", np.divide(A, np.float32(B))#element wise division using numpy functions


Sum of 2 matrices:  [[ 2  4  6]
 [ 5  8 11]]
Sum of 2 matrices:  [[ 2  4  6]
 [ 5  8 11]]
Subtraction of 2 matrices:  [[0 0 0]
 [1 0 1]]
Subtraction of 2 matrices:  [[0 0 0]
 [1 0 1]]
Multiplication of 2 matrices (element wise) :  [[ 1  4  9]
 [ 6 16 30]]
Multiplication of 2 matrices (element wise) :  [[ 1  4  9]
 [ 6 16 30]]
Division of 2 matrices (element wise) :  [[ 1.   1.   1. ]
 [ 1.5  1.   1.2]]
Division of 2 matrices (element wise) :  [[ 1.   1.   1. ]
 [ 1.5  1.   1.2]]

Matrix Matheatical Operations


In [102]:
A = np.array([[1,2,3],[3,4,6]])
B = np.array([[1,2,3],[2,2,5],[2,4,1]])
inverse_B = np.linalg.inv(B) #matrix inverese 

print "Multiplication of 2 matrices : ", np.dot(A,B) #matrix dot multiplication
print "Division of 2 matrices : ", np.dot(A,inverse_B) #matrix division


Multiplication of 2 matrices :  [[11 18 16]
 [23 38 35]]
Division of 2 matrices :  [[  1.00000000e+00   0.00000000e+00  -5.55111512e-17]
 [  2.00000000e-01   1.00000000e+00   4.00000000e-01]]

Statistical Operators


In [174]:
A = np.array([[1,2,3],[2,2,5],[2,4,1]]) 
B = np.array([[1,2,3],[2,2,5]],dtype='float32')

maximumOfRows = np.max(A,axis=0)   #matrix maximum through the columns
print "Maximum of rows = ", maximumOfRows

maximumOfColumns = np.max(A,axis=1)  #matrix maximum through rows 
print "Maximum of columns = ", maximumOfColumns

maximum = np.max(A) #matrix maximm through the rows and columns 
print "Maximum of the matrix = ", maximum

indxOfMax = np.argmax(A)
print "Index of Maximum of the matrix = ", indxOfMax

minimum = np.min(A) #matrix minimum through the rows and columns 
print "Minimum of the matrix = ", minimum

indxOfMin = np.argmin(A)
print "Index of Minimum of the matrix = ", indxOfMin

meanOfRows = np.mean(A,axis=0)   #matrix mean through the columns
print "Mean of rows = ", meanOfRows

meanOfColumns = np.mean(A,axis=1)  #matrix mean through rows 
print "Mean of columns = ", meanOfColumns

mean = np.mean(A) #matrix mean through the rows and columns 
print "Mean of the matrix = ", mean

std = np.std(A) #matrix mean through the rows and columns 
print "Standard deviation of the matrix = ", std

median = np.median(A)
print "Median of the matrix = ", median


Maximum of rows =  [2 4 5]
Maximum of columns =  [3 5 4]
Maximum of the matrix =  5
Index of Maximum of the matrix =  5
Minimum of the matrix =  1
Index of Minimum of the matrix =  0
Mean of rows =  [ 1.66666667  2.66666667  3.        ]
Mean of columns =  [ 2.          3.          2.33333333]
Mean of the matrix =  2.44444444444
Standard deviation of the matrix =  1.25707872211
Median of the matrix =  2.0

Array Shape Manipulation


In [173]:
A = np.array([[1,2,3],[2,2,5],[2,4,1]]) 
B = np.array([[1,2,3],[2,2,5]],dtype='float32')

flattenedMatrix = np.ravel(A) #flattening array to be a vector (concatenating rows together)
print "Flattened Array = ",flattenedMatrix

resizedArray = np.resize(B, (3,2)) #resize array to be in shape 3x2
print "Resized array with the same size = ",resizedArray

resizedArray = np.resize(B, (4,4)) #resize array to be in shape 4x4 (padded with repeated version of B)
print "Resized array with different size = ",resizedArray

B.resize((4,3))
print "Resized array with bigger size padded with zeros", B

#Array reshape can't change the array size unlike array resize
reshapedArray = np.reshape(B,(2,6)) 
print "Reshaped array with bigger size padded with zeros", reshapedArray


Flattened Array =  [1 2 3 2 2 5 2 4 1]
Resized array with the same size =  [[ 1.  2.]
 [ 3.  2.]
 [ 2.  5.]]
Resized array with different size =  [[ 1.  2.  3.  2.]
 [ 2.  5.  1.  2.]
 [ 3.  2.  2.  5.]
 [ 1.  2.  3.  2.]]
Resized array with bigger size padded with zeros [[ 1.  2.  3.]
 [ 2.  2.  5.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]
Reshaped array with bigger size padded with zeros [[ 1.  2.  3.  2.  2.  5.]
 [ 0.  0.  0.  0.  0.  0.]]

Matrix Concatination


In [40]:
A = np.array([[1,2,3],[2,2,5],[2,4,1]]) 
B = np.arange(0,3,1)
B.shape = 1,3 # change the shape of vector B to be of shape 1x3

concatMatrix1 = np.concatenate((A,B),axis=0) # concatinate A and B over the rows
concatMatrix1 = np.vstack((A,B)) # same as previous
print "concatinate A and B over the rows ", concatMatrix1

concatMatrix2 = np.concatenate((A,B.T),axis=1) # concatinate A and B over the columns
concatMatrix2 = np.hstack((A,B.T)) # same as previous
print "concatinate A and B over the columns", concatMatrix2

concatMatrix3 = np.concatenate((A,B),axis=None) # concatinate A and B but as a flatten version of A and B
print "concatinate A and B but as a flatten version of A and B",concatMatrix3


concatinate A and B over the rows  [[1 2 3]
 [2 2 5]
 [2 4 1]
 [0 1 2]]
concatinate A and B over the columns [[1 2 3 0]
 [2 2 5 1]
 [2 4 1 2]]
concatinate A and B but as a flatten version of A and B [1 2 3 2 2 5 2 4 1 0 1 2]

Different Useful Opertors


In [18]:
A = np.array([[1,2,3],[2,2,5],[2,4,1]]) 
B = np.array([[1,2,3],[2,2,5]],dtype='float32')

matrixDeteminant = np.linalg.det(A) #matrix determinant (A must be a square matrix)
print "Determinant = ", matrixDeteminant

matrixInverse = np.linalg.inv(A) #matrix inverse (A must be a square non singular matrix)
print "Inverse = ", matrixInverse

matrixPseudoInverse = np.linalg.pinv(B)
print "Pseudo Inverse = ", matrixPseudoInverse

matrixNorm = np.linalg.norm(A)
print "Matrix Norm = ", matrixNorm

eigenValues = np.linalg.eig(A)[0]
print "Matrix Eigne Values = ", eigenValues

eigenVectors = np.linalg.eig(A)[1]
print "Matrix Eigne Vectors = ", eigenVectors

matrixRank = np.linalg.matrix_rank(A)
print "Matrix Rank = ", matrixRank

ceilNumber = np.ceil(10.2) # round up 10.2 to be 11
print "Number Ceil = ", ceilNumber

floorNumber = np.floor(10.2)#round down 10.2 to be 10
print "Number Floor = ", floorNumber

transposedMatrix = B.T      #matrix trnaspose 
transposedMatrix = np.transpose(B) #another way to do matrix transpose 
print "shape of transposed array is", transposedMatrix.shape 

diagonal = np.diag(A)
print "Matrix diagonal is ", diagonal

P, D, Q = np.linalg.svd(A, full_matrices=False)


Determinant =  10.0
Inverse =  [[-1.8  1.   0.4]
 [ 0.8 -0.5  0.1]
 [ 0.4  0.  -0.2]]
Pseudo Inverse =  [[-0.42857143  0.33333331]
 [ 1.14285719 -0.66666663]
 [-0.28571427  0.33333331]]
Matrix Norm =  8.24621125124
Matrix Eigne Values =  [ 7.50745042 -0.43329271 -3.07415771]
Matrix Eigne Vectors =  [[-0.46868451 -0.90438061 -0.23085249]
 [-0.68100346  0.39069608 -0.63524161]
 [-0.56264476  0.17161666  0.73700422]]
Matrix Rank =  3
Number Ceil =  11.0
Number Floor =  10.0
shape of transposed array is (3, 2)
Matrix diagonal is  [1 2 1]

Write Numpy Array to CSV File


In [8]:
data = np.random.rand(100,4) #random array of size 100x15
hdr = 'hight,weight,gender,age'
fileName = '../data.csv'
np.savetxt(fileName,data,header=hdr,delimiter=',',comments=' ')

Read Data From CSV File


In [7]:
fileName = 'data.csv'
data = np.loadtxt(fileName,delimiter=',',comments=' ')

In [4]:
data.shape


Out[4]:
(100, 4)

In [11]:
y = data[:,0]
x = data[:,1:]

In [10]:
print x.shape


(100, 3)