180122 特徵值與特徵向量的幾何解釋與python程式碼,附matplotlib繪製多邊形
阿新 • • 發佈:2019-01-08
- 紅色 基座標(豎著看)
1 0
0 1 綠色 變換矩陣(豎著看)
3 1
0 2藍色 特徵向量(豎著看)
1−2√2
02√2 黑色 變換矩陣(左乘)特徵向量(豎著看)
3−2√
02√
特徵向量與特徵值得幾何含義:
特徵向量:原始向量在進行線性旋轉變換(左乘矩陣)後仍留在其所張成的空間的向量。
特徵值:即特徵向量進行線性變換後,留在原空間中變換的比例。
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 11 18:51:08 2015
@author: Duncan
A simplified version of linearring.py, one of the shapely examples
"""
from matplotlib import pyplot as plt
from shapely.geometry.polygon import Polygon
from descartes import PolygonPatch
import numpy as np
def drawArrow1(B,c="data",):
# fc: filling color
# ec: edge color
if c=='data':
fc='r'
ec='r'
s=0.25
elif c=='transf':
fc='g'
ec='g'
s=0.15
elif c=='eigenv':
fc='b'
ec='b'
s=0.1
else:
fc='k'
ec='k'
s=0.08
ax.arrow(0, 0, B[0], B[1],
length_includes_head=True,# 增加的長度包含箭頭部分
head_width=s, head_length=s, fc=fc, ec=ec,label='abc')
# 注意: 預設顯示範圍[0,1][0,1],需要單獨設定圖形範圍,以便顯示箭頭
ax.set_xticks(np.linspace(-3,4,8))
ax.set_yticks(np.linspace(-1,4,6))
ax.set_aspect('equal') #x軸y軸等比例
#%%
fig, ax = plt.subplots()
data_origin = np.array([[1,0],[0,1]])
data_transf = np.array([[3,1],[0,2]]).T
data = np.vstack((data_origin,data_transf))
c = ['data','data','transf','transf']
for i,j in zip(data,c):
drawArrow1(i,j)
#%%
D,V = np.linalg.eig(data_transf.T)
c = ['eigenv','eigenv']
for i,j in zip(V.T,c):
print(i,j)
drawArrow1(i,j)
ax.grid()
#%%
dt = np.dot(data_transf.T,V).T
drawArrow1(dt[0],c='after transfer')
drawArrow1(dt[1],c='after transfer')
print(dt)
#%%
ring_mixed = Polygon([(0, 0), V.T[0], V.T[1]])
ring_patch = PolygonPatch(ring_mixed,fc='yellow', ec='yellow', alpha=0.5)
ax.add_patch(ring_patch)
#%%
plt.savefig('arrow.png', transparent = True, bbox_inches = 'tight', pad_inches = 0.25)