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 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)
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)