visual.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # time: 2023/6/28 16:03
  4. # file: visual.py
  5. # author: David
  6. # company: shenyang JY
  7. import matplotlib.pyplot as plt
  8. from sklearn.decomposition import PCA # 加载PCA算法包
  9. from sklearn.datasets import load_iris
  10. import numpy as np
  11. def cluster_scatter(x, y):
  12. #PCA降维
  13. pca = PCA(n_components=2) # 加载PCA算法,设置降维后PC数目为2
  14. reduced_x = pca.fit_transform(x) # 对样本进行降维
  15. # reduced_x = np.dot(reduced_x, pca.components_) + pca.mean_ # 还原数据
  16. # 画图
  17. red_x, red_y = [], []
  18. blue_x, blue_y = [], []
  19. green_x, green_y = [], []
  20. yellow_x, yellow_y = [], []
  21. # print(reduced_x)
  22. for i in range(len(reduced_x)):
  23. if y[i] == 0:
  24. red_x.append(reduced_x[i][0])
  25. red_y.append(reduced_x[i][1])
  26. elif y[i] == 1:
  27. blue_x.append(reduced_x[i][0])
  28. blue_y.append(reduced_x[i][1])
  29. elif y[i] == 2:
  30. green_x.append(reduced_x[i][0])
  31. green_y.append(reduced_x[i][1])
  32. elif y[i] == 3:
  33. yellow_x.append(reduced_x[i][0])
  34. yellow_y.append(reduced_x[i][1])
  35. plt.scatter(red_x, red_y, c='r', marker='.')
  36. plt.scatter(blue_x, blue_y, c='b', marker='.')
  37. plt.scatter(green_x, green_y, c='g', marker='.')
  38. plt.scatter(yellow_x, yellow_y, c='y', marker='.')
  39. plt.show()