Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- import sklearn
- # Read data
- srdata = pd.read_csv("./srdata.txt")
- print("srdata = ")
- print(srdata)
- print()
- print("srdata summary = ")
- print(srdata.describe())
- print()
- # Plot 3D - Initial Points
- fig = plt.figure()
- ax = fig.add_subplot(projection='3d')
- ax.scatter(srdata.V1, srdata.V2, srdata.V3)
- ax.set_xlabel('V1')
- ax.set_ylabel('V2')
- ax.set_zlabel('V3')
- plt.title("Initial points (3-D)")
- plt.show()
- # ISOMAP - Convert 3-D data to 2-D data
- from sklearn.manifold import Isomap
- neigh = 4
- comp = 2 # of new dimensions
- isomap = Isomap(n_neighbors = neigh, n_components = comp)
- isomap = isomap.fit(srdata)
- transformed = pd.DataFrame(isomap.transform(srdata))
- # srdata = 3D, while transformed = 2D
- print()
- print("2-D converted data by ISOMAP: ")
- print(transformed)
- print()
- print()
- # Plot 3-D - Initial Points, but colors given by ISOMAP
- colors = [i - min(transformed.loc[:, 0].tolist()) + 1 for i in transformed.loc[:, 0].tolist()]
- fig = plt.figure()
- ax = fig.add_subplot(projection='3d')
- ax.scatter(srdata.V1, srdata.V2, srdata.V3, c=colors)
- ax.set_xlabel('V1')
- ax.set_ylabel('V2')
- ax.set_zlabel('V3')
- plt.title("ISOMAP-colored initial points (3-D)")
- plt.show()
- # New 2-D plot by ISOMAP
- plt.figure()
- plt.scatter(transformed.loc[:, 0], transformed.loc[:, 1], c=colors)
- plt.title("ISOMAP-colored initial points (3-D)")
- plt.xlabel("New var 0")
- plt.ylabel("New var 1")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement