def get_covariance_matrix(data):
    avg=get_average(data)
    avg=[n[0] for n in avg]
    centralized_matrix=[0]*len(data)  
    for i,item in enumerate(data):
        centralized_matrix[i]=Vector.minus(item,avg)
    res=Matrix.multiply(Matrix.transpose(centralized_matrix),centralized_matrix)
    N=len(data)-1
    return Matrix.multiply_integer(res, 1/N)
    
    
 def run(self, file_name):
     self.load_data(file_name)
     for index in range(len(self.data_X)):
         self.data_X[index] = [1] + self.data_X[index]
     temp = Matrix.multiply(Matrix.transpose(self.data_X), self.data_X)
     temp = Matrix.multiply(Matrix.inverse(temp), Matrix.transpose(self.data_X))
     temp = Matrix.multiply(temp, self.data_Y)
     return temp
예제 #3
0
def gaussian(x,u,C):
    res=(1/sqrt(2*math.pi*abs(Matrix.determinant(C))))
    temp=Matrix.transpose(Matrix.minus(x, u))
    temp=Matrix.multiply(temp, Matrix.inverse(C))
    temp=Matrix.multiply(temp,Matrix.minus(x, u))
    try:
        res*=exp(-0.5*temp[0][0])
    except:
        print(temp[0][0])
        print(x)
        print(u)
        print(C)
    return res
    def analysis(self,k):
#         Calculate the empirical mean
        means=Multi_Dimension_Data_Statictis.get_average(self.data)
#         Calculate the deviations from the mean
        deviations=Multi_Dimension_Data_Statictis.get_deviations(self.data) #unused
        mean_subtracted_data=Matrix.minus(self.data, Matrix.multiply([[1] for _ in range(len(self.data))], Matrix.transpose(means)))
#         Find the covariance matrix
        covariance_matrix=Multi_Dimension_Data_Statictis.get_covariance_matrix(mean_subtracted_data)
#         Find the eigenvectors and eigenvalues of the covariance matrix
        x= np.mat(covariance_matrix)
        eigenvalues,eigenvectors=np.linalg.eigh(x)
        eigenvalues=eigenvalues.tolist()
        eigenvectors=Matrix.transpose(eigenvectors.tolist())
#         Rearrange the eigenvectors and eigenvalues
        eigenvalue_and_eigenvector=[]
        for i in range(len(eigenvalues)):
            eigenvalue_and_eigenvector.append((eigenvalues[i],eigenvectors[i]))
        
        eigenvalue_and_eigenvector=sorted(eigenvalue_and_eigenvector, reverse=True)
#         Choosing k eigenvectors with the largest eigenvalues
        transform_matrix=[]
        for i in range(k):
            transform_matrix.append(eigenvalue_and_eigenvector[i][1])
        return Matrix.transpose(Matrix.multiply(transform_matrix,Matrix.transpose(self.data)))
예제 #5
0
 def run(self,file_name,k):
     self.load_data(file_name)
     self.set_distances()
     self.iteration(k)
     return Matrix.transpose(self.results);
 def iteration(self):
     A=Matrix.multiply(self.data_X, self.weights)
     E=Matrix.minus(self.g_function(A),self.data_Y)
     temp=Matrix.multiply_integer(Matrix.multiply(Matrix.transpose(self.data_X), E), self.learning_rate)
     self.weights=Matrix.minus(self.weights,temp)
예제 #7
0
 def test_determinant(self):
     A=[[1,-9,13,7],[-2,5,-1,3],[3,-1,5,-5],[2,8,-7,-10]]
     expected_result=-312
     actual_result=Matrix.determinant(A)
     self.assertEqual(expected_result, actual_result)   
예제 #8
0
 def test_append(self):
     A=[[1,2,3],[4,5,6]]
     B=[[1,2,3],[4,5,6]]
     expected_result=[[1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6]]
     actual_result=Matrix.append(A, B)
     self.assertEqual(expected_result, actual_result)    
예제 #9
0
 def test_eye(self):
     N=3
     expected_result=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
     actual_result=Matrix.eye(N)
     self.assertEqual(expected_result, actual_result)
예제 #10
0
 def test_inverse(self):
     A=[[1,1,2],[-1,2,0],[1,1,3]]
     expected_result=[[2.0, -0.3333333333333333, -1.3333333333333335], [1.0, 0.3333333333333333, -0.6666666666666666], [-1.0, 0.0, 1.0]]
     actual_result=Matrix.inverse(A)
     self.assertEqual(expected_result, actual_result) 
예제 #11
0
 def test_multiply_integer(self):
     A=[[1,2,3],[4,5,6]]
     N=2
     expected_result=[[2,4,6],[8,10,12]]
     actual_result=Matrix.multiply_integer(A,N)
     self.assertEqual(expected_result, actual_result)   
예제 #12
0
 def test_transpose(self):
     A=[[1,2,3],[4,5,6]]
     expected_result=[[1,4],[2,5],[3,6]]
     actual_result=Matrix.transpose(A)
     self.assertEqual(expected_result, actual_result)
예제 #13
0
 def test_minus(self):
     A=[[1,2,3],[4,5,6]]
     B=[[6,5,4],[3,2,1]]
     expected_result=[[-5,-3,-1],[1,3,5]]
     actual_result=Matrix.minus(A, B)
     self.assertEqual(expected_result, actual_result)    
예제 #14
0
 def test_plus(self):
     A=[[1,2,3],[4,5,6]]
     B=[[6,5,4],[3,2,1]]
     expected_result=[[7,7,7],[7,7,7]]
     actual_result=Matrix.plus(A, B)
     self.assertEqual(expected_result, actual_result)   
예제 #15
0
 def test_multiply(self):
     A=[[1,2,3],[4,5,6]]
     B=[[6,5],[4,3],[2,1]]
     expected_result=[[20, 14], [56, 41]]
     actual_result=Matrix.multiply(A, B)
     self.assertEqual(expected_result, actual_result)