-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdimension_reduction.py
More file actions
105 lines (84 loc) · 3.13 KB
/
dimension_reduction.py
File metadata and controls
105 lines (84 loc) · 3.13 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from sklearn.decomposition import PCA
from sklearn.manifold import MDS
from sklearn.decomposition import FastICA
from sklearn.manifold import Isomap
from sklearn.manifold import LocallyLinearEmbedding
from sklearn.manifold import SpectralEmbedding
import matplotlib.pyplot as plt
import csv
import main_file
DIM = 3
def apply_dimension_reduction_method(data, method, plot=True):
# methods = ["PCA", "CMDS", "NCMDS", "ICA", "ISOMAP", "LLE", "LAPLACIAN EIGENMAPS"]
if method == "PCA":
reduced_data = apply_pca(data)
elif method == "CMDS":
reduced_data = apply_cmds(data)
elif method == "NCMDS":
reduced_data = apply_ncmds(data)
elif method == "ICA":
reduced_data = apply_ica(data)
elif method == "ISOMAP":
reduced_data = apply_isomap(data)
elif method == "LLE":
reduced_data = apply_lle(data)
elif method == "LAPLACIAN EIGENMAPS":
reduced_data = apply_laplacian_eigenmaps(data)
else:
raise Exception("No such dimension reduction algorithm")
if DIM == 3:
x, y, z = zip(*reduced_data)
if plot:
fig = plt.figure()
# ax = fig.add_subplot(111)
ax = plt.axes(projection="3d")
plt.title(method)
ax.set_xlabel('First Principal Component')
ax.set_ylabel('Second Principal Component')
ax.set_zlabel('Third Principal Component')
ax.scatter3D(x, y, z, c=main_file.get_real_labels(), alpha=0.8, s=8)
with open("dimension_reduction/" + method + "_3d.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows([x, y, z])
else:
x, y = zip(*reduced_data)
if plot:
fig = plt.figure()
# ax = fig.add_subplot(111)
ax = plt.axes()
plt.title(method)
ax.set_xlabel('First Principal Component')
ax.set_ylabel('Second Principal Component')
ax.scatter(x, y, c=main_file.get_real_labels(), alpha=0.8, s=8)
# plt.show()
return reduced_data
def apply_pca(data):
model = PCA(n_components=DIM)
return model.fit_transform(data)
def apply_cmds(data):
model = MDS(n_components=DIM)
return model.fit_transform(data)
def apply_ncmds(data):
model = MDS(n_components=DIM, metric=False)
return model.fit_transform(data)
def apply_ica(data):
model = FastICA(n_components=DIM, max_iter=1500)
return model.fit_transform(data)
def apply_isomap(data):
model = Isomap(n_components=DIM)
return model.fit_transform(data)
def apply_lle(data):
model = LocallyLinearEmbedding(n_components=DIM, eigen_solver='dense')
return model.fit_transform(data)
def apply_laplacian_eigenmaps(data):
model = SpectralEmbedding(n_components=DIM)
return model.fit_transform(data)
def read_dimension_reduction_results_to_lil(method):
with open("dimension_reduction/" + method + '_3d.csv', newline='') as f:
reader = csv.reader(f)
data = list(reader)
x_str, y_str, z_str = data
x = [float(i) for i in x_str]
y = [float(i) for i in y_str]
z = [float(i) for i in z_str]
return [x, y, z]