1、降维作用
- 数据在低维下更容易处理、更容易使用;
- 相关特征,特别是重要特征更能在数据中明确的显示出来;如果只有两维或者三维的话,更便于可视化展示;
- 去除数据噪声
- 降低算法开销
import numpy as np
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
ir=load_iris()
data=ir.data
target=ir.target
result=PCA(n_components=2).fit_transform(data)
red_x,red_y=[],[]
blue_x,blue_y=[],[]
green_x,green_y=[],[]
for i in range(len(result)):
if target[i]==0:
red_x.append(result[i][0])
red_y.append(result[i][1])
elif target[i]==1:
blue_x.append(result[i][0])
blue_y.append(result[i][1])
elif target[i]==2:
green_x.append(result[i][0])
green_y.append(result[i][1])
# plt.figure(figsize=(5,12))
# plt.subplot(1,3,1)
plt.scatter(red_x,red_y,c="red")
# plt.subplot(1,3,2)
plt.scatter(blue_x,blue_y,c="blue")
# plt.subplot(1,3,3)
plt.scatter(green_x,green_y,c="green")
plt.show()