1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # time: 2023/6/28 16:03
- # file: visual.py
- # author: David
- # company: shenyang JY
- import matplotlib.pyplot as plt
- from sklearn.decomposition import PCA # 加载PCA算法包
- from sklearn.datasets import load_iris
- import numpy as np
- def cluster_scatter(x, y):
- #PCA降维
- pca = PCA(n_components=2) # 加载PCA算法,设置降维后PC数目为2
- reduced_x = pca.fit_transform(x) # 对样本进行降维
- # reduced_x = np.dot(reduced_x, pca.components_) + pca.mean_ # 还原数据
- # 画图
- red_x, red_y = [], []
- blue_x, blue_y = [], []
- green_x, green_y = [], []
- yellow_x, yellow_y = [], []
- # print(reduced_x)
- for i in range(len(reduced_x)):
- if y[i] == 0:
- red_x.append(reduced_x[i][0])
- red_y.append(reduced_x[i][1])
- elif y[i] == 1:
- blue_x.append(reduced_x[i][0])
- blue_y.append(reduced_x[i][1])
- elif y[i] == 2:
- green_x.append(reduced_x[i][0])
- green_y.append(reduced_x[i][1])
- elif y[i] == 3:
- yellow_x.append(reduced_x[i][0])
- yellow_y.append(reduced_x[i][1])
- plt.scatter(red_x, red_y, c='r', marker='.')
- plt.scatter(blue_x, blue_y, c='b', marker='.')
- plt.scatter(green_x, green_y, c='g', marker='.')
- plt.scatter(yellow_x, yellow_y, c='y', marker='.')
- plt.show()
|