【问题标题】:Linear Algebra in Python: Calculating Eigenvectors for 3x3 MatrixPython 中的线性代数:计算 3x3 矩阵的特征向量
【发布时间】:2021-01-02 03:58:38
【问题描述】:

我正在使用 Python 导出与 3x3 矩阵中的特征值相关的特征向量。我的代码返回正确的特征值但错误的特征向量。

A = np.array([[-2, -4,  2],
              [-2,  1,  2],
              [4,   2,  5]])
print (A)
print ('-------------------------------------------------------')

eigenvalues, eigenvectors = np.linalg.eig(A) # must use this line of code exactly 
print(f'eigenvalues of matrix A are:{eigenvalues}')
print ('-------------------------------------------------------')
print(f'eigenvectors of matrix A are:{eigenvectors}')

例如,与值 6 关联的特征向量应该是 [1, 6, 16],而不是代码输出的内容。

【问题讨论】:

  • 每个特征值有无限多个特征向量。代码工作正常。
  • 从 numpy 文档中,返回特征值矩阵,使得“归一化(单位“长度”)特征向量,使得列 v[:,i] 是对应于特征值 w[一世]。”查看特征向量矩阵的最后 。它是 [1, 6, 16],仅归一化。

标签: python linear-algebra eigenvalue eigenvector


【解决方案1】:

正确,可以通过第二个特征值和特征向量的特征向量/特征值条件来检查。

其中u 是特征向量,lambda 是其特征值。 所以我们将特征向量 v[:,1] 乘以 A 并检查它是否与将相同的特征向量乘以其特征值 w[1] 相同。

import numpy as np

>>> w, v = np.linalg.eig(A)
# w contains the eigenvalues. 
# v contains the corresponding eigenvectors, one eigenvector per column. 
# The eigenvectors are normalized so their Euclidean norms are 1
>>> u = v[:,1]
>>> print(u)
[ 0.53452248, -0.80178373, -0.26726124]

>>> lam = w[1]
>>> lam
3.0

>>> print(np.dot(A,u))
[ 1.60356745 -2.40535118 -0.80178373]
>>> print(lam*u)
[ 1.60356745 -2.40535118 -0.80178373]

【讨论】:

  • 具体来说,np.linalg.eig 返回的特征向量被归一化为单位长度。
  • @PineNuts0 如果这回答了您的问题,请接受。
【解决方案2】:

这里的关键是上面所说的 NumPy 库进行的规范化过程,以以数字方式显示“特征向量”数组,因为它编程显示像

# when λ = 6
x_dash = np.array([[ 1*x],
                   [ 6*x],
                   [16*x]])
# and then you replace x with your lucky number that would save you some effort in math manipulation

,所以在你的情况下,你期望 [1, 6, 16] 作为 6 个特征值的特征向量,没关系,不要惊慌。您只需要认识到整个向量经历了一个点乘过程,其中包含来自向量化的一些常数,在您的情况下,它恰好是

0.05842062

  1. 1 * 0.05842062 = 0.05842062
  2. 6 * 0.05842062 = 0.35052372
  3. 16 * 0.05842062 = 0.93472992

这就是您使用 np.linalg 库获得的结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    相关资源
    最近更新 更多