-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvm_for_multivariate_data.py
executable file
·235 lines (137 loc) · 6.62 KB
/
svm_for_multivariate_data.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""
Version: 1.5
Summary: This code implemented SVM(Support Vector Machine) for classification of multi-dimensional Dataset (Tassel shape calssification)
Author: suxing liu
Author-email: [email protected]
USAGE
python3 svm_for_multivariate_data.py -p ~/example/cluster_ml/ -f trait_part.xlsx
"""
# importing necessary libraries
import argparse
import pandas as pd
from sklearn import datasets
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn import model_selection
from sklearn.decomposition import PCA
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
import numpy as np
def multidimensional_classification(full_path):
"""SVM(Support Vector Machine) for classification of multi-dimensional Dataset
Inputs:
full_path: full path of the training excel file
Returns:
print out accuracy and confusion matrix
"""
# Read dataset into pandas dataframe
df = pd.read_excel(full_path, names = ['filename','tassel area', 'tassel area ratio', 'average width', 'average height', 'number of branches', 'average branch length', 'tassel_type'])
# assign features
data_features = ['tassel area', 'tassel area ratio', 'average width', 'average height', 'number of branches', 'average branch length']
#data_features = ['max_width', 'max_height', 'number_of_branches']
# Extract features
X = df.loc[:, data_features].values
# Extract target class ID
y = df.loc[:, ['tassel_type']].values
y = y.ravel()
# Now using scikit-learn model_selection module, split the iris data into train/test data sets
# keeping 40% reserved for testing purpose and 60% data will be used to train and form model.
# dividing X, y into train and test data
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.8, random_state = 0)
# keeping 40% reserved for testing purpose and 60% data will be used to train and form model.
#X_train, X_test, y_train, y_test = model_selection.train_test_split (X, y, test_size=0.8, random_state=0)
# training a linear SVM classifier
svm_model_linear = SVC(kernel = 'linear', C = 1).fit(X_train, y_train)
svm_predictions = svm_model_linear.predict(X_test)
# model accuracy for X_test
accuracy = svm_model_linear.score(X_test, y_test)
# creating a confusion matrix
cm = confusion_matrix(y_test, svm_predictions)
print(accuracy)
# Further, let’s now validate robustness of above model using K-Fold Cross validation technique
# We give cross_val_score a model, the entire iris data set and its real values, and the number of folds:
scores_res = model_selection.cross_val_score(svm_model_linear, X, y, cv=5)
# Print the accuracy of each fold (i.e. 5 as above we asked cv 5)
print(scores_res)
# And the mean accuracy of all 5 folds.
print(scores_res.mean())
# perform PCA analysis
PCA_analysis(X, y)
'''
linear = SVC(kernel='linear', C=1, decision_function_shape='ovo').fit(X_train, y_train)
rbf = SVC(kernel='rbf', gamma=1, C=1, decision_function_shape='ovo').fit(X_train, y_train)
poly = SVC(kernel='poly', degree=3, C=1, decision_function_shape='ovo').fit(X_train, y_train)
sig = SVC(kernel='sigmoid', C=1, decision_function_shape='ovo').fit(X_train, y_train)
linear_pred = linear.predict(X_test)
poly_pred = poly.predict(X_test)
rbf_pred = rbf.predict(X_test)
sig_pred = sig.predict(X_test)
# retrieve the accuracy and print it for all 4 kernel functions
accuracy_lin = linear.score(X_test, y_test)
accuracy_poly = poly.score(X_test, y_test)
accuracy_rbf = rbf.score(X_test, y_test)
accuracy_sig = sig.score(X_test, y_test)
print("Accuracy Linear Kernel:", accuracy_lin)
print("Accuracy Polynomial Kernel:", accuracy_poly)
print("Accuracy Radial Basis Kernel:", accuracy_rbf)
print("Accuracy Sigmoid Kernel:", accuracy_sig)
'''
'''
in_data_for_prediction = [[4.9876, 3.348, 1.8488, 0.2], [5.3654, 2.0853, 3.4675, 1.1222], [5.890, 3.33, 5.134, 1.6]]
p_res = svm_model_linear.predict(in_data_for_prediction)
print('Given first iris is of type:', p_res[0])
print('Given second iris is of type:', p_res[1])
print('Given third iris is of type:', p_res[2])
'''
def get_cmap(n, name = 'hsv'):
"""get n kinds of colors from a color palette
Inputs:
n: number of colors
name: the color palette choosed
Returns:
plt.cm.get_cmap(name, n): Returns a function that maps each index in 0, 1, ..., n-1 to a distinct
RGB color; the keyword argument name must be a standard mpl colormap name.
"""
return plt.cm.get_cmap(name,n)
def PCA_analysis(X, y):
"""Dimensionality Reduction using PCA (Principal Component Analysis)
Inputs:
X: feature data
y: target class ID
Returns:
scatter_plot of all classes
"""
#Use n_components = 2, transform into a 2-Dimensional dataset.
pca = PCA(n_components=2, whiten=True).fit(X)
X_pca = pca.transform(X)
print('explained variance ratio:', pca.explained_variance_ratio_)
print('Preserved Variance:', sum(pca.explained_variance_ratio_))
# assign class IDs
target_names = ['C1', 'C2','C3','C4','C5','C6','C7','C8','C9']
# Print scatter plot to view classification of the simplified dataset
colors = get_cmap(len(target_names))
plt.figure()
target_list = np.array(y).flatten()
for i, t_name in enumerate(target_names):
color_rgb = colors(i)[:len(colors(i))-1]
plt.scatter(X_pca[target_list == t_name, 0], X_pca[target_list ==t_name, 1], color = color_rgb, label=t_name)
plt.legend()
#plt.show()
# save plot result
result_file = (current_path + 'scatter_plot.png')
plt.savefig(result_file)
if __name__ == '__main__':
# construct the argument and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--path", required = True, help = "path to file")
ap.add_argument("-f", "--filename", required = True, help = "file name")
args = vars(ap.parse_args())
# parce path to file
current_path = args["path"]
filename = args["filename"]
# full path to data file
full_path = current_path + filename
# classification
multidimensional_classification(full_path)