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

Posted 柠檬叶子C

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线代&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, "attributes\\n")
print("First 5 records in iris data\\n", 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)

print("\\nFirst 5 Transformed records\\n", finalDf.head(5))
fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(1,1,1)
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.

百度百科[EB/OL]. []. https://baike.baidu.com/

本篇完。

以上是关于线代&NumPy第八章 - 特征值和特征向量 | Eigenvalue and Eigenvector | 简述并提供代码的主要内容,如果未能解决你的问题,请参考以下文章

第八章语言

第八章

线代线代复习自用

《JAVA编程思想》学习笔记——第八章 多态

java编程思想第八章多态

第八章 无标度网络模型