In [1]:
import numpy as np # include numpy package and renaming it to be np instead of numpy
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
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
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
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
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
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
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
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
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
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)
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=' ')
In [7]:
fileName = 'data.csv'
data = np.loadtxt(fileName,delimiter=',',comments=' ')
In [4]:
data.shape
Out[4]:
In [11]:
y = data[:,0]
x = data[:,1:]
In [10]:
print x.shape