import numpy as np
data = np.array([[1,0,0,0,0,0,0,0],
[0.846,1,0,0,0,0,0,0],
[0.805,0.881,1,0,0,0,0,0],
[0.859,0.826,0.801,1,0,0,0,0],
[0.473,0.376,0.380,0.436,1,0,0,0],
[0.398,0.326,0.319,0.329,0.762,1,0,0],
[0.301,0.277,0.237,0.327,0.730,0.583,1,0],
[0.382,0.415,0.345,0.365,0.629,0.577,0.539,1]])
for i in range(data.shape[0]):
for j in range(data.shape[0]):
data[i][j]=data[j][i]
print(data)
eigenvalue,featurevector=np.linalg.eig(data)
print("原始矩阵的特征值")
print("eigenvalue=")
print(eigenvalue)
print("featurevector=")
print(featurevector)
import xlsxwriter
workbook = xlsxwriter.Workbook('主成分.xlsx') # 建立文件
worksheet = workbook.add_worksheet() # 建立sheet, 可以work.add_worksheet('employee')来指定sheet名,但中文名会报UnicodeDecodeErro的错误
for i in range(featurevector.shape[0]):
for j in range(featurevector.shape[0]):
worksheet.write(i,j, featurevector[i][j]) # row, column, value
workbook.close()
求矩阵的特征值和特征向量来做主成分分析
最新推荐文章于 2024-06-17 14:46:59 发布