Advertisement
sserban21

pb1Bia

Mar 21st, 2023
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. """
  2. Use the iris dataset in order to reduce its dimensionality down to three dimensions by using the PCA algorithm.
  3. Print the principal components and the explained variance ratio and plot the 3D dataset.
  4. Also visualize the data reduced to three dimensions using the t-SNE method.
  5. """
  6.  
  7. import numpy as np
  8. import matplotlib.pyplot as plt
  9. from sklearn import datasets
  10. from sklearn.decomposition import PCA
  11. from sklearn.manifold import TSNE
  12.  
  13. # Load the iris dataset
  14. iris = datasets.load_iris()
  15. X = iris.data
  16. y = iris.target
  17.  
  18. # Reduce the dimensionality of the data to 3 dimensions
  19. pca = PCA(n_components=3)
  20. X_reduced = pca.fit_transform(X)
  21.  
  22. # Print the principal components and the explained variance ratio
  23. print("Principal components:",pca.components_)
  24. print("Explained variance ratio:",pca.explained_variance_ratio_)
  25.  
  26. # Plot the 3D dataset
  27. fig = plt.figure(figsize=(6, 3.8), constrained_layout=True)
  28. ax = fig.add_subplot(111, projection='3d')
  29.  
  30. #plot the points above and below the plane
  31. #X_reduced = pca.inverse_transform(X_reduced)
  32. X3D_above = X[X[:, 2] > X_reduced[:, 2]]
  33. X3D_below = X[X[:, 2] <= X_reduced[:, 2]]
  34. ax.plot(X3D_above[:, 0], X3D_above[:, 1], X3D_above[:, 2], "bo")
  35. ax.plot(X3D_below[:, 0], X3D_below[:, 1], X3D_below[:, 2], "bo", alpha=0.5)
  36.  
  37. #plot the plane
  38. axes = [-8, 8, -6, 6, -3, 7]
  39.  
  40. x1s = np.linspace(axes[0], axes[1], 10)
  41. x2s = np.linspace(axes[2], axes[3], 10)
  42. x1, x2 = np.meshgrid(x1s, x2s)
  43. C = pca.components_
  44. R = C.T.dot(C)
  45. z = (R[0, 2] * x1 + R[1, 2] * x2) / (1 - R[2, 2])
  46. ax.plot_surface(x1, x2, z, alpha=0.2, color="k")
  47.  
  48. #plot the projections on the plane
  49. ax.plot(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], "k+")
  50. ax.plot(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], "k.")
  51.  
  52. #plot the lines connecting the 3D points with their projections on the plane
  53. n, d = X.shape
  54. for i in range(n):
  55.     if X[i, 2] > X_reduced[i, 2]:
  56.         ax.plot([X[i][0], X_reduced[i][0]], [X[i][1], X_reduced[i][1]], [X[i][2], X_reduced[i][2]], "k-")
  57.     else:
  58.         ax.plot([X[i][0], X_reduced[i][0]], [X[i][1], X_reduced[i][1]], [X[i][2], X_reduced[i][2]], "k-", color="#505050")
  59.    
  60. ax.set_xlabel("$x_1$", fontsize=18)
  61. ax.set_ylabel("$x_2$", fontsize=18)
  62. ax.set_zlabel("$x_3$", fontsize=18)
  63. ax.set_xlim(axes[0:2])
  64. ax.set_ylim(axes[2:4])
  65. ax.set_zlim(axes[4:6])
  66.  
  67. plt.show()
  68.  
  69. # build a TSNE model
  70. tsne = TSNE(n_components=3, random_state=42)
  71. flowers_tsne = tsne.fit_transform(X)
  72.  
  73. # transform the iris data onto the first two principal components
  74. #flowers_pca = pca.transform(X)
  75. colors = ["#476A2A", "#7851B8", "#BD3430", "#4A2D4E", "#875525",
  76.           "#A83683", "#4E655E", "#853541", "#3A3120", "#535D8E"]
  77. axes2 = [-100, -10, 20, -100, -10, 20]
  78. figure = plt.figure(figsize=(10, 5), constrained_layout=True)
  79. bx = figure.add_subplot(111, projection='3d')
  80. bx.set_xlim(flowers_tsne[:, 0].min(), flowers_tsne[:, 0].max())
  81. bx.set_ylim(flowers_tsne[:, 1].min(), flowers_tsne[:, 1].max())
  82. bx.set_zlim(flowers_tsne[:, 2].min(), flowers_tsne[:, 2].max())
  83. for i in range(len(X)):
  84.     # actually plot the digits as text instead of using scatter
  85.     bx.text(flowers_tsne[i, 0], flowers_tsne[i, 1], flowers_tsne[i, 2], str(y[i]),
  86.              color = colors[y[i]],
  87.              fontdict={'weight': 'bold', 'size': 9})
  88. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement