forked from Charles57-CWU/DSCVizTests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDimensional_Reduction_Techniques.py
164 lines (117 loc) · 5.39 KB
/
Dimensional_Reduction_Techniques.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
This script will run dimensional reduction techniques on a dataset
Add column headers, fix missing values, and make sure class labels are in numeric form, before running models
Make sure there is only one class column, and the remaining columns are attributes you'd like to run in the model
"""
# import dimensional reduction techniques
from sklearn.manifold import TSNE
from sklearn.decomposition import TruncatedSVD, PCA
from sklearn.manifold import MDS
# import utilities
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# clear warnings
import warnings
def warn(*args, **kwargs):
pass
warnings.warn = warn
# =====================================================
class DimensionalReduction:
def __init__(self, dataset_name, class_column_name):
self.dataset_name = dataset_name
self.class_column_name = class_column_name
self.labels = None
self.data = None
# csv to data and labels
def get_data_and_labels(self):
# read the dataset file
data = pd.read_csv(self.dataset_name)
# labels
self.labels = data[self.class_column_name]
data.drop([self.class_column_name], axis=1, inplace=True)
self.data = data
# =====================================================
# run dimensional reduction
def run_dim_reduction(self, model, num_of_comp):
# run models
if model == 'TSNE': # t-distributed stochastic neighbor embedding
# TSNE warning
if num_of_comp >= 4:
print('TSNE requires 1 to 3 components')
return
transformed_data = TSNE(n_components=num_of_comp).fit_transform(self.data)
elif model == 'TSVD': # truncated singular value decomposition
transformed_data = TruncatedSVD(n_components=num_of_comp).fit_transform(self.data)
elif model == 'PCA': # principal component analysis
transformed_data = PCA(n_components=num_of_comp).fit_transform(self.data)
elif model == 'MDS': # multidimensional scaling
transformed_data = MDS(n_components=num_of_comp).fit_transform(self.data)
else:
transformed_data = None
# create column names for components
column_names = ['component_' + str(i+1) for i in range(num_of_comp)]
# return components as dataframe
output = pd.DataFrame(transformed_data[:], columns=column_names)
output[self.class_column_name] = self.labels
return output
# =====================================================
# add components to dataset passed to class
def add_components_to_dataset(self, component_df):
component_df.drop([self.class_column_name], axis=1, inplace=True)
comp_names = component_df.columns.values.tolist()
# add components
self.data[comp_names] = component_df[:]
# add class
self.data['class'] = self.labels
return self.data
# =====================================================
# make scatterplot
def scatterplot_of_components(self, component_df, x, y):
"""
custom palette for a lot of classes
access the custom palette by changing my_palette variable to my_palette=colors
add or remove colors from colors array if you have more classes
"""
# colors = ['#0000FF', '#FF0000']
my_palette = 'hls'
# number of classes
targets = len(component_df[self.class_column_name].unique()) # of classes
# general plot info
plt.figure(figsize=(10, 10))
ax = sns.scatterplot(x, y, palette=sns.color_palette(palette=my_palette, n_colors=targets),
data=component_df, hue=self.labels, legend='full')
ax.set_xlabel('Component 1', fontsize=16)
ax.set_ylabel('Component 2', fontsize=16)
plt.show()
# =====================================================
if __name__ == '__main__':
dataset_name = 'LUPI_VEHICLE_ALL.csv'
class_column_name = 'class'
output_csv_name = 'CWU_TEST2.csv'
# initiate class
m = DimensionalReduction(dataset_name, class_column_name)
# get data and labels
m.get_data_and_labels()
# technique and number of reduction components -> manually enter here
dim_components = m.run_dim_reduction('TSNE', 2)
# optional if you want to see components on scatterplot
m.scatterplot_of_components(dim_components, 'component_1', 'component_2')
# optional combine components with original dataset
new_data = m.add_components_to_dataset(dim_components)
print(new_data)
new_data.to_csv(output_csv_name, index=False)
"""
If you want to combine dimensional reduction techniques just initiate another class
Example: MNIST 784 real attributes -> 50 PCA -> 2 TSNE
a = DimensionalReduction('mnist.csv', 'class')
a.get_data_and_labels()
pca_comps = a.run_dim_reduction('PCA', 50)
pca_comps.to_csv('PCA_comps.csv', index=False)
b = DimensionalReduction('PCA_comps.csv', 'class')
b.get_data_and_labels()
tsne_comps = b.run_dim_reduction('TSNE',2)
here you can call:
a.add_components_to_dataset(tsne_comps) --> tsne+original
or b.add_components_to_dataset(tsne_comps) --> tsne+PCA
"""