用 NumPy 实现矩阵投影:一个有趣且实用的算法
import numpy as np
生成一个随机的 5x5 矩阵
matrix = np.random.rand(5, 5)
计算矩阵的迹
eigenvalues = np.linalg.eigvals(matrix) trace = np.sum(eigenvalues)
将矩阵中所有小于迹的元素变为 0
matrix[matrix < trace] = 0
计算矩阵的特征值和特征向量
eigenvalues, eigenvectors = np.linalg.eig(matrix)
对特征向量进行尺度缩放
scaled_eigenvectors = eigenvectors / np.max(eigenvectors, axis=0)
将缩放后的特征向量投影到矩阵上
projected_matrix = np.dot(scaled_eigenvectors, matrix)
将矩阵中所有大于 0 的元素变为 1
projected_matrix[projected_matrix > 0] = 1
输出最终的矩阵
print(projected_matrix)
原文地址: https://www.cveoy.top/t/topic/lfI4 著作权归作者所有。请勿转载和采集!