【发布时间】:2018-07-07 15:20:57
【问题描述】:
我正在尝试学习如何使用 numpy 在一个简单的示例中确定特征向量和值,但结果看起来不正确。这是我的代码:
import numpy as np
import numpy.linalg as la
# create the matrix
matrix = np.array([[-2, 1, 0], [1, -2, 1], [0, 1, -2]])
print("Matrix:\n", matrix)
# calculate the eigenvalues and vectors
vals, vecs = np.linalg.eigh(matrix)
# print the eigenvalues and vectores
print("vals:\n", vals)
print("vecs:\n", vecs)
# get the eigenvectors
v1 = vecs[:,0]
v2 = vecs[:,1]
v3 = vecs[:,2]
print("v1:", v1)
print("v2:", v2)
print("v3:", v3)
# compute dot
dot1 = np.dot(matrix, v1)
dot2 = np.dot(matrix, v2)
dot3 = np.dot(matrix, v3)
# is the dot collinear to the eigenvectors?
print("dot1 / v1", dot1 / v1)
print("dot2 / v2", dot2 / v2)
print("dot3 / v3", dot3 / v3)
这是输出:
Matrix:
[[-2 1 0]
[ 1 -2 1]
[ 0 1 -2]]
vals:
[-3.41421356 -2. -0.58578644]
vecs:
[[ 5.00000000e-01 -7.07106781e-01 -5.00000000e-01]
[ -7.07106781e-01 4.88509860e-17 -7.07106781e-01]
[ 5.00000000e-01 7.07106781e-01 -5.00000000e-01]]
v1: [ 0.5 -0.70710678 0.5 ]
v2: [ -7.07106781e-01 4.88509860e-17 7.07106781e-01]
v3: [-0.5 -0.70710678 -0.5 ]
dot1 / v1 [-3.41421356 -3.41421356 -3.41421356]
dot2 / v2 [-2. -4.54534541 -2. ]
dot3 / v3 [-0.58578644 -0.58578644 -0.58578644]
当我使用在线计算器 (http://www.arndt-bruenner.de/mathe/scripts/engl_eigenwert2.htm) 计算特征向量时,我得到: 真实特征值:{ -3.414213562373095 ; -2 ; -0.585786437626905 }
特征向量:
对于特征值 -3.414213562373095: [ 1 ; -1.4142135623730954; 1]
对于特征值 -2: [ -1 ; 0; 1]
对于特征值 -0.585786437626905: [ 1 ; 1.4142135623730954; 1]
特征值匹配,但特征向量不匹配。
问题: 1. numpy 是否对特征向量进行缩放?
- 如何证明(矩阵点特征向量)与特征向量共线,以证明我有正确的特征向量。我在想将点积除以特征向量会显示一个恒定的偏移量,但特征向量 2 不会发生这种情况。话虽如此,当我将 numpy 与在线计算器进行比较时,特征向量 2 在我看来并不正确。这是显示共线性的正确方法吗?
【问题讨论】:
-
您将
dot2除以v2元素。dot2和v2中的第二个元素大约为 0(我分别得到大约 -2.6e-16 和 2.08e-17)。如果计算准确,则这些元素将恰好为 0,并且不会定义您的除法。因此,您将较小的数值噪声除以较小的数值噪声,从而产生放大的噪声。
标签: python numpy linear-algebra