python 奇异值分解小程序 分类:机器学习

来源:互联网 发布:1吨铀能发多少电 知乎 编辑:程序博客网 时间:2024/05/17 07:57
[python] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. import numpy as np  
  2. import random  
  3.   
  4. def gen_inv(a):  
  5.         a_sq = np.dot(a.T, a)  
  6.         eigen = np.linalg.eig(a_sq)  
  7.         eigen_vals = eigen[0]  
  8.         eigen_vectors = eigen[1]  
  9.   
  10.         orth = a.dot(eigen_vectors)  
  11.         new_orth_len = np.zeros([orth.shape[1], orth.shape[1]])  
  12.         orth_sq = orth.T.dot(orth)  
  13.   
  14.         for j in range(orth.shape[1]):  
  15.                 for i in range(orth.shape[0]):  
  16.                         orth[i][j] /= (orth_sq[j][j] ** 0.5)  
  17.                 new_orth_len[j][j] = orth_sq[j][j] ** 0.5  
  18.   
  19.         return {“Q”: orth, “lamda”: new_orth_len, “P”: eigen_vectors}  
  20.   
  21. M = np.zeros([500500])  
  22. for i in range(M.shape[0]):  
  23.         for j in range(M.shape[1]):  
  24.                 M[i][j] = random.random()  
  25.   
  26. print gen_inv(M)  
import numpy as npimport randomdef gen_inv(a):        a_sq = np.dot(a.T, a)        eigen = np.linalg.eig(a_sq)        eigen_vals = eigen[0]        eigen_vectors = eigen[1]        orth = a.dot(eigen_vectors)        new_orth_len = np.zeros([orth.shape[1], orth.shape[1]])        orth_sq = orth.T.dot(orth)        for j in range(orth.shape[1]):                for i in range(orth.shape[0]):                        orth[i][j] /= (orth_sq[j][j] ** 0.5)                new_orth_len[j][j] = orth_sq[j][j] ** 0.5        return {"Q": orth, "lamda": new_orth_len, "P": eigen_vectors}M = np.zeros([500, 500])for i in range(M.shape[0]):        for j in range(M.shape[1]):                M[i][j] = random.random()print gen_inv(M)



更多了解请浏览:http://blog.csdn.net/sinat_30665603

阅读全文
0 0
原创粉丝点击