Guest User

Untitled

a guest
May 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. from sklearn import datasets
  2. from sklearn.decomposition import PCA
  3. from sklearn.preprocessing import scale
  4. from pandas import DataFrame
  5. iris = datasets.load_iris()
  6. iris = data1 = DataFrame(data= np.c_[iris['data'], iris['target']],
  7. columns= iris['feature_names'] + ['class'])
  8.  
  9. def plot_pca(data,classes, classes_label=None):
  10. fig, (axe_2d,axe_info) = plt.subplots(1,2)
  11. data_scaled = scale(data)
  12. classes_unique = np.unique(classes)
  13. if classes_label is None:
  14. classes_label = ["Class %i" % i for i in classes_unique]
  15. n_axis = np.shape(data)[1]
  16. pca = PCA()
  17. x_reduced = pca.fit_transform(data_scaled)
  18. var = pca.explained_variance_ratio_
  19. cumul_prop = np.cumsum(var)
  20.  
  21. # Plot 2D
  22. for class_value, class_label in zip(classes_unique,classes_label):
  23. selector = classes==class_value
  24. axe_2d.scatter(x_reduced[selector,0],x_reduced[selector,1], label=class_label)
  25. axe_2d.set_title("2D plot")
  26. axe_2d.set_xlabel("Component 1")
  27. axe_2d.set_xlabel("Component 2")
  28. axe_2d.legend()
  29. # Plot bar
  30. axe_info.bar(range(n_axis),cumul_prop)
  31. axe_info.set_title("Components cumulative proportion")
  32. axe_info.set_xlabel("Components")
  33. axe_info.set_xlabel("Cumulative proportion")
  34.  
  35. for axe in (axe_2d, axe_info):
  36. axe.grid()
  37.  
  38. plot_pca(iris.values[:,:-1],iris.values[:,-1])
  39. plt.show()
Add Comment
Please, Sign In to add comment