-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinear_separable_visual.py
214 lines (159 loc) · 6.83 KB
/
linear_separable_visual.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
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 16 08:53:32 2020
@author: user
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix
from sklearn.svm import SVC
from sklearn import datasets
from mpl_toolkits.mplot3d import Axes3D
import plotly.graph_objs as go
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, accuracy_score
from sklearn.datasets import make_blobs
# Linear separable visualization
def svm_decision_function(model, ax=None, plot_support=True):
if ax is None:
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# create grid
x = np.linspace(xlim[0].min(), xlim[1].max(), 20)
y = np.linspace(ylim[0].min(), ylim[1].max(), 20)
Y, X = np.meshgrid(y, x)
xy = np.vstack([X.ravel(), Y.ravel()]).T
P = model.decision_function(xy).reshape(X.shape)
# plot decision boundary and margins
ax.contour(X, Y, P, colors='k', levels=[-1, 0, 1], alpha=0.5, linestyles=['--', '-', '--'])
# plot support vectors
if plot_support:
ax.scatter(model.support_vectors_[:, 0],
model.support_vectors_[:, 1],
s=300, linewidth=1, edgecolors = 'k', facecolors='none');
ax.set_xlim(xlim)
ax.set_ylim(ylim)
def params_gamma():
X, y = make_blobs(n_samples=50, n_features= 2, centers=2, random_state=0, cluster_std=0.7)
fig, ax = plt.subplots(1, 3, figsize=(16, 6))
fig.subplots_adjust(left=0.0625, right=0.95, wspace=0.1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),\
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for axis, G in zip(ax, [1, 100, 0.1]):
print(G)
model = SVC(kernel='rbf', gamma=G).fit(X_train, y_train)
axis.scatter(X_set[:, 0], X_set[:, 1], c=y_set, cmap='Set1')
axis.contourf(X1, X2, model.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.55, cmap = ListedColormap(('red', 'green')))
svm_decision_function(model, axis, plot_support=True)
# Plot training
axis.set_title('gamma = %s' %(G), size=14)
def params_c():
# make_blobs
# n_samples = total samples
# n_features = X
# centers = y
# shuffle = shuffle the samples
# cluster_std = standard deviation of samples
X, y = make_blobs(n_samples=50, n_features= 2, centers=2, random_state=0, cluster_std=0.7)
fig, ax = plt.subplots(1, 3, figsize=(16, 6))
fig.subplots_adjust(left=0.0625, right=0.95, wspace=0.1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
for axis, C in zip(ax, [1.0, 10.0, 0.1]):
print(C)
model = SVC(kernel='linear', C=C).fit(X_train, y_train)
# Plot training
axis.scatter(X_train[:, 0], X_train[:, 1], c=y_train, s=50, cmap='Set1')
svm_decision_function(model, axis)
axis.scatter(model.support_vectors_[:, 0],
model.support_vectors_[:, 1],
s=300, lw=1, facecolors='none');
axis.set_title('C = {0:.1f}'.format(C), size=14)
def line_visual():
data = datasets.load_iris()
X = data.data[:, :2] # we only take the first 2 features.
Y = data.target
#Binary classification problem
X = X[np.logical_or(Y==0,Y==1)]
Y = Y[np.logical_or(Y==0,Y==1)]
# Dataframe of Iris
irisDF = pd.DataFrame(data = data.data, columns = data.feature_names)
irisDF['Target'] = pd.DataFrame(data.target)
print("\nIris dataframe: \n",irisDF.head(8)) # print dataset only first 8 rows
# Data visualization
# scatter_matrix(frame, figure_size, ..)
scatter_matrix(irisDF.iloc[:,0:4], figsize=(15,10))
# 4x4 matrices
# Build model
svm = SVC(kernel='poly')
svm.fit(X, Y)
# Assume here wants to compare 2 features: sepal length n sepal width
# x as sepal length
# y as sepal width
#x = irisDF.iloc[:, 0]
#y = irisDF.iloc[:, 1]
iris_names = data.target_names
colors = ['red', 'blue', 'green']
label = (data.target).astype(np.int)
plt.figure(figsize=(15,10))
#plt.scatter(x, y, c = 'red', marker= 's', alpha=0.5) # in one color image
# Get different color of classes
# range(len(irish_names)): return the number of items in irish_names: 3
# while the target(label) is (0,1,2) so the color is (red, blue, green)
for i in range(len(iris_names)):
separate_df = irisDF[irisDF['Target'] == i]
separate_df = separate_df.iloc[:,[0,1]].values
print(separate_df, "\n")
plt.scatter(separate_df[:, 0], separate_df[:, 1], label = iris_names[i])
plt.scatter(separate_df[:, 0], separate_df[:, 1], s=50, cmap='Set1')
svm_decision_function(svm)
plt.title("sepal length vs sepal width")
plt.xlabel("sepal length")
plt.ylabel("sepal width")
plt.legend()
plt.grid()
plt.show()
def plane_visual():
iris = datasets.load_iris()
X = iris.data[:, :4] # we only take the first three features.
Y = iris.target
#Binary classification problem
#X = X[np.logical_or(Y==0,Y==1)]
#Y = Y[np.logical_or(Y==0,Y==1)]
model = SVC(kernel='linear')
clf = model.fit(X, Y)
# The equation of the separating plane is ((w)svc.coef_[0], x) + b = 0.
# Solve for w
z = lambda x,y: (-clf.intercept_[0]-clf.coef_[0][0]*x -clf.coef_[0][1]*y) / clf.coef_[0][2]
#Create grid
xm, xM = X[:,0].min(), X[:, 0].max()
ym, yM = X[:,1].min(), X[:, 1].max()
x = np.linspace(xm, xM, 10)
y = np.linspace(ym, yM, 10)
x, y =np.meshgrid(x, y)
#3D Visualize
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot3D(X[Y==0,0], X[Y==0,1], X[Y==0,2],'sr')
ax.plot3D(X[Y==1,0], X[Y==1,1], X[Y==1,2],'ob')
ax.plot_surface(x, y, z(x,y))
ax.view_init(30, 60)
plt.grid()
plt.show()
if __name__ == "__main__":
params_gamma()
params_c()
line_visual()
plane_visual()