marta-marta
commited on
Commit
•
7e70cfa
1
Parent(s):
e8a1ac5
Added TSNE Plot
Browse files- Data_Plotting/2D_Lattice.csv +3 -0
- Data_Plotting/Plot_TSNE.py +68 -0
Data_Plotting/2D_Lattice.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bb137b85f16095f396a65fd9f24327374ca4d855c95c29eb916ab3bfe08f596c
|
3 |
+
size 166090
|
Data_Plotting/Plot_TSNE.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sklearn.manifold import TSNE
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# Latent Feature Cluster for Training Data using T-SNE
|
6 |
+
def TSNE_reduction(latent_points, perplexity=30, learning_rate=20):
|
7 |
+
latent_dimensionality = len(latent_points[0])
|
8 |
+
model = TSNE(n_components=2, random_state=0, perplexity=perplexity,
|
9 |
+
learning_rate=learning_rate) # Perplexity(5-50) | learning_rate(10-1000)
|
10 |
+
embedding = model
|
11 |
+
# configuring the parameters
|
12 |
+
# the number of components = dimension of the embedded space
|
13 |
+
# default perplexity = 30 " Perplexity balances the attention t-SNE gives to local and global aspects of the data.
|
14 |
+
# It is roughly a guess of the number of close neighbors each point has. ..a denser dataset ... requires higher perplexity value"
|
15 |
+
# default learning rate = 200 "If the learning rate is too high, the data may look like a ‘ball’ with any point
|
16 |
+
# approximately equidistant from its nearest neighbours. If the learning rate is too low,
|
17 |
+
# most points may look compressed in a dense cloud with few outliers."
|
18 |
+
tsne_data = model.fit_transform(
|
19 |
+
latent_points) # When there are more data points, trainX should be the first couple hundred points so TSNE doesn't take too long
|
20 |
+
x = tsne_data[:, 0]
|
21 |
+
y = tsne_data[:, 1]
|
22 |
+
title = ("T-SNE of Data")
|
23 |
+
return x, y, title, embedding
|
24 |
+
|
25 |
+
|
26 |
+
########################################################################################################################
|
27 |
+
import pandas as pd
|
28 |
+
import json
|
29 |
+
|
30 |
+
df = pd.read_csv('2D_Lattice.csv')
|
31 |
+
row = 0
|
32 |
+
box = df.iloc[row,1]
|
33 |
+
array = np.array(json.loads(box))
|
34 |
+
|
35 |
+
"""
|
36 |
+
# For plotting CSV data
|
37 |
+
# define a function to flatten a box
|
38 |
+
def flatten_box(box_str):
|
39 |
+
box = json.loads(box_str)
|
40 |
+
return np.array(box).flatten()
|
41 |
+
|
42 |
+
|
43 |
+
# apply the flatten_box function to each row of the dataframe and create a list of flattened arrays
|
44 |
+
flattened_arrays = df['Array'].apply(flatten_box).tolist()
|
45 |
+
|
46 |
+
|
47 |
+
x, y, title, embedding = TSNE_reduction(flattened_arrays)
|
48 |
+
|
49 |
+
plt.scatter(x,y)
|
50 |
+
plt.title(title)
|
51 |
+
plt.show()
|
52 |
+
"""
|
53 |
+
|
54 |
+
# def plot_dimensionality_reduction(x, y, label_set, title):
|
55 |
+
# plt.title(title)
|
56 |
+
# if label_set[0].dtype == float:
|
57 |
+
# plt.scatter(x, y, c=label_set)
|
58 |
+
# plt.colorbar()
|
59 |
+
# print("using scatter")
|
60 |
+
# else:
|
61 |
+
# for label in set(label_set):
|
62 |
+
# cond = np.where(np.array(label_set) == str(label))
|
63 |
+
# plt.plot(x[cond], y[cond], marker='o', linestyle='none', label=label)
|
64 |
+
#
|
65 |
+
# plt.legend(numpoints=1)
|
66 |
+
#
|
67 |
+
# plt.show()
|
68 |
+
# plt.close()
|