-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2d.py
75 lines (61 loc) · 1.66 KB
/
2d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
X=np.loadtxt("matrix.txt", delimiter=",")
y=[]
co=[]
for row in X:
a = row.tolist()
b=a.index(max(a))
if b==0:
c='255,31,31,0.8'
elif b==1:
c = '28,217,44,0.8'
elif b==2:
c = '37,171,226,0.8'
elif b==3:
c = '255,255,31,0.8'
elif b==4:
c = '123,104,238,0.8'
else:
c = '2,255,255,0.8'
co.append(c)
y.append(b)
File = open("colour.txt", "w")
for Index in co:
File.write(str(Index) + "\n")
File.close()
pca = PCA(n_components=2)
pca.fit(X)
print(pca.explained_variance_ratio_)
print(pca.explained_variance_)
X_new = pca.transform(X)
scaler = MinMaxScaler( )
scaler.fit(X_new)
scaler.data_max_
mat=scaler.transform(X_new)
plt.figure(1)
plt.scatter(mat[:, 0], mat[:, 1],marker='o',c=y)
plt.show()
np.savetxt("pca.txt", mat, fmt="%.5f", delimiter=",")
plt.figure(2)
lda = LinearDiscriminantAnalysis(n_components=2)
lda.fit(X,y)
X_lda = lda.transform(X)
scaler.fit(X_lda)
scaler.data_max_
X_lda=scaler.transform(X_lda)
plt.scatter(X_lda[:, 0], X_lda[:, 1],marker='o',c=y)
plt.show()
np.savetxt("lda.txt", X_lda, fmt="%.5f", delimiter=",")
plt.figure(3)
tsne = TSNE(n_components=2, n_iter=300).fit_transform(X)
scaler.fit(tsne)
scaler.data_max_
tsne1=scaler.transform(tsne)
plt.scatter(tsne1[:, 0], tsne1[:, 1],marker='o',c=y)
plt.show()
np.savetxt("sne.txt", tsne1, fmt="%.5f", delimiter=",")