Advertisement
makispaiktis

ML - Lab 6 - ISOMAP

Oct 20th, 2022 (edited)
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import sklearn
  5.  
  6. # Read data
  7. srdata = pd.read_csv("./srdata.txt")
  8. print("srdata = ")
  9. print(srdata)
  10. print()
  11. print("srdata summary = ")
  12. print(srdata.describe())
  13. print()
  14.  
  15. # Plot 3D - Initial Points
  16. fig = plt.figure()
  17. ax = fig.add_subplot(projection='3d')
  18. ax.scatter(srdata.V1, srdata.V2, srdata.V3)
  19. ax.set_xlabel('V1')
  20. ax.set_ylabel('V2')
  21. ax.set_zlabel('V3')
  22. plt.title("Initial points (3-D)")
  23. plt.show()
  24.  
  25.  
  26.  
  27.  
  28. # ISOMAP - Convert 3-D data to 2-D data
  29. from sklearn.manifold import Isomap
  30. neigh = 4
  31. comp = 2            # of new dimensions
  32. isomap = Isomap(n_neighbors = neigh, n_components = comp)
  33. isomap = isomap.fit(srdata)
  34. transformed = pd.DataFrame(isomap.transform(srdata))
  35. # srdata = 3D, while transformed = 2D
  36. print()
  37. print("2-D converted data by ISOMAP: ")
  38. print(transformed)
  39. print()
  40. print()
  41.  
  42.  
  43. # Plot 3-D - Initial Points, but colors given by ISOMAP
  44. colors = [i - min(transformed.loc[:, 0].tolist()) + 1 for i in transformed.loc[:, 0].tolist()]
  45. fig = plt.figure()
  46. ax = fig.add_subplot(projection='3d')
  47. ax.scatter(srdata.V1, srdata.V2, srdata.V3, c=colors)
  48. ax.set_xlabel('V1')
  49. ax.set_ylabel('V2')
  50. ax.set_zlabel('V3')
  51. plt.title("ISOMAP-colored initial points (3-D)")
  52. plt.show()
  53.  
  54.  
  55. # New 2-D plot by ISOMAP
  56. plt.figure()
  57. plt.scatter(transformed.loc[:, 0], transformed.loc[:, 1], c=colors)
  58. plt.title("ISOMAP-colored initial points (3-D)")
  59. plt.xlabel("New var 0")
  60. plt.ylabel("New var 1")
  61. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement