-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm_plotter.py
139 lines (119 loc) · 5.26 KB
/
svm_plotter.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
import numpy as np
import pandas as pd
from sklearn import svm
from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt
# Create arbitrary dataset for example
#X = [pd.DataFrame(),pd.DataFrame(),pd.DataFrame(),pd.DataFrame(),pd.DataFrame(),pd.DataFrame(),pd.DataFrame(),pd.DataFrame(),pd.DataFrame(),]
df = pd.DataFrame()
df = pd.read_excel('excel/train_output.xlsx')
df = df[[ 'i', 'line', 'img_area', 'Class', 'Class_id', 'x_min', 'x_max', 'y_min', 'y_max', \
'total_area_hsv_blue', 'total_area_hsv_red', \
'max_contour_area_hsv_red', 'max_contour_area_hsv_blue', \
'centroid_X_hsv_red', 'centroid_Y_hsv_red', \
'centroid_X_hsv_blue', 'centroid_Y_hsv_blue', \
'total_area_hsv_blue_t20', 'total_area_hsv_red_t20', \
'max_contour_area_hsv_red_t20', 'max_contour_area_hsv_blue_t20', \
'centroid_X_hsv_red_t20', 'centroid_Y_hsv_red_t20', \
'centroid_X_hsv_blue_t20', 'centroid_Y_hsv_blue_t20', \
'total_area_hsv_thresh_blue', 'total_area_hsv_thresh_red', \
'max_contour_area_hsv_thresh_red', 'max_contour_area_hsv_thresh_blue', \
'centroid_X_hsv_thresh_red', 'centroid_Y_hsv_thresh_red', \
'centroid_X_hsv_thresh_blue', 'centroid_Y_hsv_thresh_blue', \
'total_area_hsv_thresh_blue_t20', 'total_area_hsv_thresh_red_t20', \
'max_contour_area_hsv_thresh_red_t20', 'max_contour_area_hsv_thresh_blue_t20', \
'centroid_X_hsv_thresh_red_t20', 'centroid_Y_hsv_thresh_red_t20', \
'centroid_X_hsv_thresh_blue_t20', 'centroid_Y_hsv_thresh_blue_t20']]
# Fit Support Vector Machine Classifier
y = df['Class_id']
fit_type = 'rbf'
fig = plt.figure()
plt.subplot(2, 4, 1)
X_1 = df[['total_area_hsv_blue', 'total_area_hsv_red']]
clf = svm.SVC(kernel=fit_type, decision_function_shape='ovr')
clf.fit(X_1.values, y.values)
plot_decision_regions(X=X_1.values,
y=y.values,
clf=clf,
legend=2)
plt.xlabel(X_1.columns[0], size=8)
plt.ylabel(X_1.columns[1], size=8)
plt.title('SVM Decision Region Boundary_1', size=10)
plt.subplot(2, 4, 2)
X_2 = df[['max_contour_area_hsv_red', 'max_contour_area_hsv_blue']]
clf = svm.SVC(kernel=fit_type, decision_function_shape='ovr')
clf.fit(X_2.values, y.values)
plot_decision_regions(X=X_2.values,
y=y.values,
clf=clf,
legend=2)
plt.xlabel(X_2.columns[0], size=8)
plt.ylabel(X_2.columns[1], size=8)
plt.title('SVM Decision Region Boundary_2', size=10)
plt.subplot(2, 4, 3)
X_3 = df[['total_area_hsv_blue_t20', 'total_area_hsv_red_t20']]
clf = svm.SVC(kernel=fit_type, decision_function_shape='ovr')
clf.fit(X_3.values, y.values)
plot_decision_regions(X=X_3.values,
y=y.values,
clf=clf,
legend=2)
plt.xlabel(X_3.columns[0], size=8)
plt.ylabel(X_3.columns[1], size=8)
plt.title('SVM Decision Region Boundary_3', size=10)
plt.subplot(2, 4, 4)
X_4 = df[['max_contour_area_hsv_red_t20', 'max_contour_area_hsv_blue_t20']]
clf = svm.SVC(kernel=fit_type, decision_function_shape='ovr')
clf.fit(X_4.values, y.values)
plot_decision_regions(X=X_4.values,
y=y.values,
clf=clf,
legend=2)
plt.xlabel(X_4.columns[0], size=8)
plt.ylabel(X_4.columns[1], size=8)
plt.title('SVM Decision Region Boundary_4', size=10)
plt.subplot(2, 4, 5)
X_5 = df[['total_area_hsv_thresh_blue', 'total_area_hsv_thresh_red']]
clf = svm.SVC(kernel=fit_type, decision_function_shape='ovr')
clf.fit(X_5.values, y.values)
plot_decision_regions(X=X_5.values,
y=y.values,
clf=clf,
legend=2)
plt.xlabel(X_5.columns[0], size=8)
plt.ylabel(X_5.columns[1], size=8)
plt.title('SVM Decision Region Boundary_5', size=10)
plt.subplot(2, 4, 6)
X_6 = df[['max_contour_area_hsv_thresh_red', 'max_contour_area_hsv_thresh_blue']]
clf = svm.SVC(kernel=fit_type, decision_function_shape='ovr')
clf.fit(X_6.values, y.values)
plot_decision_regions(X=X_6.values,
y=y.values,
clf=clf,
legend=2)
plt.xlabel(X_6.columns[0], size=8)
plt.ylabel(X_6.columns[1], size=8)
plt.title('SVM Decision Region Boundary_6', size=10)
plt.subplot(2, 4, 7)
X_7 = df[['total_area_hsv_thresh_blue_t20', 'total_area_hsv_thresh_red_t20']]
clf = svm.SVC(kernel=fit_type, decision_function_shape='ovr')
clf.fit(X_7.values, y.values)
plot_decision_regions(X=X_7.values,
y=y.values,
clf=clf,
legend=2)
plt.xlabel(X_7.columns[0], size=8)
plt.ylabel(X_7.columns[1], size=8)
plt.title('SVM Decision Region Boundary_7', size=10)
plt.subplot(2, 4, 8)
X_8 = df[['max_contour_area_hsv_thresh_red_t20', 'max_contour_area_hsv_thresh_blue_t20']]
clf = svm.SVC(kernel=fit_type, decision_function_shape='ovr')
clf.fit(X_5.values, y.values)
plot_decision_regions(X=X_8.values,
y=y.values,
clf=clf,
legend=2)
plt.xlabel(X_8.columns[0], size=8)
plt.ylabel(X_8.columns[1], size=8)
plt.title('SVM Decision Region Boundary_8', size=10)
plt.show()