-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwine_pca.py
More file actions
191 lines (128 loc) · 6.12 KB
/
Copy pathwine_pca.py
File metadata and controls
191 lines (128 loc) · 6.12 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
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
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/python3
import pandas as pd
import numpy as np
import time
import matplotlib.pyplot as plt
import csv
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from sklearn.mixture import GaussianMixture
from sklearn import metrics
from sklearn import preprocessing
from sklearn.cluster import KMeans
from sklearn.datasets import load_digits
from sklearn.tree import DecisionTreeClassifier
from sklearn.decomposition import PCA
from sklearn.decomposition import FastICA
from sklearn.decomposition import FactorAnalysis
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
import scipy
from sklearn import random_projection
from cluster_func import em
from cluster_func import kmeans
data = pd.read_csv('winequality-data.csv')
X = data.iloc[:,:-2]
y = data.iloc[:,-2]
y = y > 6
#Splitting data into training and testing and keeping testing data aside
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.2)
#Converting into numpy arrays
X_train = X_train.as_matrix()
X_test = X_test.as_matrix()
y_train = y_train.as_matrix()
y_test = y_test.as_matrix()
print("Starting PCA")
print("Dimensionality reduction")
decisiontree = DecisionTreeClassifier(criterion = 'gini', max_depth = 15, min_samples_split = 5)
pca = PCA()
pipe = Pipeline(steps=[('pca', pca), ('decisionTree', decisiontree)])
# Plot the PCA spectrum
pca.fit(X)
fig, ax = plt.subplots()
ax.bar(list(range(1,12)), pca.explained_variance_ratio_, linewidth=2, color = 'blue')
plt.axis('tight')
plt.xlabel('n_components')
ax.set_ylabel('explained variance ratio')
#Checking the accuracy for taking all combination of components
n_components = range(1, 12)
# Parameters of pipelines can be set using ‘__’ separated parameter names:
gridSearch = GridSearchCV(pipe, dict(pca__n_components=n_components), cv = 3)
gridSearch.fit(X, y)
results = gridSearch.cv_results_
ax1 = ax.twinx()
#Plotting the accuracies and best component
ax1.plot(results['mean_test_score'], linewidth = 2, color = 'red')
ax1.set_ylabel('Mean Cross Validation Accuracy')
ax1.axvline(gridSearch.best_estimator_.named_steps['pca'].n_components, linestyle=':', label='n_components chosen', linewidth = 2)
plt.legend(prop=dict(size=11))
plt.title('Accuracy/Variance explained for PCA (best n_components= %d)'%gridSearch.best_estimator_.named_steps['pca'].n_components )
plt.savefig("wine_pca_1")
#plt.show()
#Reducing the dimensions with optimal number of components
pca_new = PCA(n_components = gridSearch.best_estimator_.named_steps['pca'].n_components)
pca_new.fit(X_train)
X_train_transformed = pca_new.transform(X_train)
X_test_transformed = pca_new.transform(X_test)
###############################################################################################################################
#Reconstruction Error
print("Calculating Reconstruction Error")
reconstruction_error = []
for comp in n_components:
pca = PCA(n_components = comp)
X_transformed = pca.fit_transform(X_train)
X_projected = pca.inverse_transform(X_transformed)
reconstruction_error.append(((X_train - X_projected) ** 2).mean())
if(comp == gridSearch.best_estimator_.named_steps['pca'].n_components):
chosen_error = ((X_train - X_projected) ** 2).mean()
fig2,ax2 = plt.subplots()
ax2.plot(n_components, reconstruction_error, linewidth= 2)
ax2.axvline(gridSearch.best_estimator_.named_steps['pca'].n_components, linestyle=':', label='n_components chosen', linewidth = 2)
plt.axis('tight')
plt.xlabel('Number of components')
plt.ylabel('Reconstruction Error')
plt.title('Reconstruction error for n_components chosen %f '%chosen_error)
plt.savefig("wine_pca_2")
#plt.show()
################################################################################################################################
#Clustering after dimensionality reduction
print("Clustering PCA")
#Reducing the dimensions with optimal number of components
pca_new = PCA(n_components = gridSearch.best_estimator_.named_steps['pca'].n_components)
pca_new.fit(X)
X_transformed_f = pca_new.transform(X)
#clustering experiments
#clustering experiments
print("Expected Maximization")
component_list, array_aic, array_bic, array_homo_1, array_comp_1, array_sil_1, array_avg_log = em(X_train_transformed, X_test_transformed, y_train, y_test, component_list = [3,4,5,6,7,8,9,10,11], num_class = 7, toshow =0, file_no = "wine_pca")
print("KMeans")
component_list, array_homo_2, array_comp_2, array_sil_2, array_var = kmeans(X_train_transformed, X_test_transformed, y_train, y_test, component_list = [3,4,5,6,7,8,9,10,11], num_class = 7, toshow =0, file_no = "wine_pca")
#Writing data to file
component_list = np.array(component_list).reshape(-1,1)
array_aic = np.array(array_aic).reshape(-1,1)
array_bic = np.array(array_bic).reshape(-1,1)
array_homo_1 = np.array(array_homo_1).reshape(-1,1)
array_comp_1 = np.array(array_comp_1).reshape(-1,1)
array_sil_1 = np.array(array_sil_1).reshape(-1,1)
array_avg_log = np.array(array_avg_log).reshape(-1,1)
array_homo_2 = np.array(array_homo_2).reshape(-1,1)
array_comp_2 = np.array(array_comp_2).reshape(-1,1)
array_sil_2 = np.array(array_sil_2).reshape(-1,1)
array_var = np.array(array_var).reshape(-1,1)
reconstruction_error = np.array(reconstruction_error).reshape(-1,1)
data_em_pca_wine = np.concatenate((component_list, array_aic, array_bic, array_homo_1, array_comp_1, array_sil_1, array_avg_log), axis =1)
data_km_pca_wine = np.concatenate((component_list, array_homo_2, array_sil_2, array_var), axis =1)
reconstruction_error_pca_wine = np.concatenate((np.arange(1,12).reshape(-1,1), reconstruction_error), axis = 1)
file = './data/data_em_pca_wine.csv'
with open(file, 'w', newline = '') as output:
writer = csv.writer(output, delimiter=',')
writer.writerows(data_em_pca_wine)
file = './data/data_km_pca_wine.csv'
with open(file, 'w', newline = '') as output:
writer = csv.writer(output, delimiter=',')
writer.writerows(data_km_pca_wine)
file = './data/reconstruction_error_pca_wine.csv'
with open(file, 'w', newline = '') as output:
writer = csv.writer(output, delimiter=',')
writer.writerows(reconstruction_error_pca_wine)