# 【线代&NumPy】第八章 – 特征值和特征向量 | Eigenvalue and Eigenvector | 简述并提供代码

? 例1：

``````import numpy as np

A = np.array([[2, 3], [3, -6]])
w1, V1 = np.linalg.eig(A) # 计算A的特征值和特征向量

print("A的特征值： = ", w1)
print("A的特征向量： = ", V1)

B = np.array([[5,2,0], [2,5,0], [-3,4,6]])
w2, V2 = np.linalg.eig(B) # 计算B的特征值和特征向量

print("n");
print("B的特征值 = ", w2)
print("B的特征向量 = ", V2)``````

? 运行结果：

A的特征值： =  [ 3. -7.]
A的特征向量： =  [[ 0.9486833  -0.31622777]
[ 0.31622777  0.9486833 ]]

B的特征值 =  [6. 7. 3.]
B的特征向量 =  [[ 0.          0.57735027  0.36650833]
[ 0.          0.57735027 -0.36650833]
[ 1.          0.57735027  0.85518611]]

? 例2：

url：https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data

``````import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import pandas as pd
from sklearn.preprocessing import StandardScaler

# iris 数据的 URL
url = "xxx"
# Pandas DataFrame
df = pd.read_csv(url, names=['sepal length','sepal width','petal length','petal width','target'])

nrow, ncol = df.shape
print("Iris data set :", nrow, "records with", ncol, "attributesn")
print("First 5 records in iris datan", df.head(5))
features = ['sepal length', 'sepal width', 'petal length', 'petal width']
x = df.loc[:, features].values
y = df.loc[:,['target']].values
x = StandardScaler().fit_transform(x) # 变换为 平均0, 分散1 的数据

pca = PCA(n_components=2) # 利用 PCA
principalComponents = pca.fit_transform(x)
# 利用2个主成分轴转换为二维数据
print("nFirst principal axis:", pca.components_[0])
print("Second principal axis:", pca.components_[1])

principalDf = pd.DataFrame(data = principalComponents,
columns = ['principal component 1', 'principal component 2'])
finalDf = pd.concat([principalDf, df[['target']]], axis = 1)

fig = plt.figure(figsize = (8,8))
ax.set_xlabel('principal component 1', fontsize = 12)
ax.set_ylabel('principal component 2', fontsize = 12)
ax.set_title('PCA with 2 components', fontsize = 15)

targets = ['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'] # iris的数据类型名称
colors = ['r', 'g', 'b'] # 按类别指定的颜色
for target, color in zip(targets,colors):
indicesToKeep = finalDf['target'] == target
ax.scatter(finalDf.loc[indicesToKeep, 'principal component 1']
, finalDf.loc[indicesToKeep, 'principal component 2'], c = color, s = 40)
ax.legend(targets)
ax.grid()
fig.show()
``````

? 运行结果：

--- A --- 1.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 1.00 rank(A) = 4 --- B --- 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

rank(B) = 0 --- C --- 2.00 5.00 -3.00 -4.00 8.00 4.00 7.00 -4.00 -3.00 9.00 6.00 9.00 -5.00 2.00 4.00 0.00 -9.00 6.00 5.00 -6.00

rank(C) = 3 --- C^T --- 2.00 4.00 6.00 0.00 5.00 7.00 9.00 -9.00 -3.00 -4.00 -5.00 6.00 -4.00 -3.00 2.00 5.00 8.00 9.00 4.00 -6.00

rank(C^T) = 3

Introduction to Linear Algebra, International 4 th Edition by Gilbert Strang, Wellesley Cambridge Press.

THE END

)">