-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path[python file]assignment_3.py
More file actions
240 lines (177 loc) · 6.65 KB
/
[python file]assignment_3.py
File metadata and controls
240 lines (177 loc) · 6.65 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
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
236
237
# -*- coding: utf-8 -*-
"""Assignment 3.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1cVbrfGCSYX8gZkZY2lvPQnNy8ilS4blD
Choose an appropriate dataset of your choice such that every record (example) has at least 5 features which are numeric in nature and there is at least one attribute (feature) which is binary in nature.
You can use the binary attribute as the binary target label to be predicted.
Split your dataset into a training set and a test set. You can try different splits: 70:30 (70% training, 30% testing), 80:20 or 90:10 split.
On the training set, train the following classifiers:
1. Half Space
2. Logistic Regression (using inbuilt function)
3. SVM classifier (using a linear kernel)
4. SVM classifier (using a Polynomial kernel and a Gaussian kernel)
5. Logistic Regression using the SGD procedure.
If your data is not linearly separable, then you will be required to use the soft SVM formulation.
You can use the inbuilt implementation of logistic regression and SVM in SciKit Learn.
Compare and analyze the results obtained by using the different classifiers. For the soft SVM formulation, you should compare the performance with the different values of the regularization parameter. Report the number of support vectors obtained for every dataset split.
You should submit a report along with the code.
"""
# Commented out IPython magic to ensure Python compatibility.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn import svm
df = pd.read_csv('bill_authentication.csv')
# https://archive.ics.uci.edu/ml/datasets/banknote+authentication
df.shape
df.head()
# variance_of_wavelet_transformed, used as input.
# skewness_of_wavelet_transformed, used as input.
# curtosis_of_wavelet_transformed, used as input.
# entropy_of_image, used as input.
# class, used as the target. It can only have two values: 0 (false) or 1 (true).
df.Class.value_counts()
labels = 'Positives', 'Neagtive'
num_pos = 610
num_neg = 762
size=[num_pos,num_neg]
figure, plotpie = plt.subplots()
plotpie.pie(size, labels=labels)
plotpie.axis('equal')
plt.show()
# Import seaborn
import seaborn as sns
#import matplotlib
import matplotlib.pyplot as plt
# Use pairplot and set the hue to be our class
sns.pairplot(df, hue='Class')
# Show the plot
plt.show()
# defining features and target variable
y = df['Class']
X = df.drop(columns = ['Class'])
from sklearn.model_selection import train_test_split
#splitting the data into train and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)
print(X_train.shape, X_test.shape)
#HALFSPACE Seperablity test
plt.clf()
plt.figure(figsize=(10,6))
plt.scatter(df['Variance'], df['Skewness'])
plt.title('Variance vs Skewness')
plt.xlabel('Variance')
plt.ylabel('Skewness')
plt.show()
# X_train, y_train
new_x = X_train.Variance
new_y = X_train.Skewness
points_X = X_train[['Variance','Skewness']]
class_y = y_train
clf = svm.SVC(kernel='linear', C = 1.0)
clf.fit(points_X,class_y)
print(clf.predict([[0.58,0.76]]))
print(clf.predict([[10.58,10.76]]))
# same way we can send our test set to predict all values
w = clf.coef_[0]
print(w)
a = -w[0] / w[1]
xx = points_X
yy = a * xx - clf.intercept_[0] / w[1]
h0 = plt.plot(xx, yy, 'k-', label="div")
plt.scatter(new_x,new_y, c = class_y)
plt.legend()
plt.show()
plt.scatter(new_x,new_y, c=class_y, s=50);
plt.plot([0.6], [2.1], 'x', color='red', markeredgewidth=2, markersize=10)
for m, b in [(1, 0.65), (0.5, 1.6), (-0.2, 2.9)]:
plt.plot(clf.coef_[0], m * clf.coef_[0] + b, '-k')
plt.xlim(-6, 6);
#LOGISTIC REGRESSION
from sklearn.linear_model import LogisticRegression
lr=LogisticRegression()
lr.fit(X_train,y_train)
from sklearn.metrics import accuracy_score
y_pred=lr.predict(X_test)
score=accuracy_score(y_test,y_pred)
score
# SVM classifier (using a linear kernel)
from sklearn.svm import SVC
svclassifier = SVC(kernel='linear')
svclassifier.fit(X_train, y_train)
y_pred = svclassifier.predict(X_test)
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test,y_pred))
SVM: Maximum margin separating hyperplane¶
print(classification_report(y_test,y_pred))
# SVM: Maximum margin separating hyperplane
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
plt.scatter(new_x,new_y, c=class_y, s=30, cmap=plt.cm.Paired)
# plot the decision function
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# create grid to evaluate model
xx = np.linspace(xlim[0], xlim[1], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = clf.decision_function(xy).reshape(XX.shape)
# plot decision boundary and margins
ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--'])
# plot support vectors
ax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=100,
linewidth=1, facecolors='none', edgecolors='k')
plt.show()
# Polynomial Kernel
from sklearn.svm import SVC
svclassifier = SVC(kernel='poly', degree=8)
svclassifier.fit(X_train, y_train)
y_pred = svclassifier.predict(X_test)
y_pred
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
new_x, new_y, class_y
points_X, class_y
# Reference: https://scikit-learn.org/stable/auto_examples/svm/plot_svm_kernels.html
clf = svm.SVC(kernel='poly', gamma=2)
clf.fit(points_X, class_y)
plt.clf()
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=80,facecolors='none', zorder=10, edgecolors='k')
# y_train, y_test
plt.scatter(new_x, new_y, c=class_y, zorder=10, cmap=plt.cm.Paired, edgecolors='k')
# X_test, y_test
plt.axis('tight')
x_min = -6
x_max = 6
y_min = -15
y_max = 15
XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()])
# Put the result into a color plot
Z = Z.reshape(XX.shape)
plt.figure(4, figsize=(4, 3))
plt.pcolormesh(XX, YY, Z > 0, cmap=plt.cm.Paired)
plt.contour(XX, YY, Z, colors=['k', 'k', 'k'], linestyles=['--', '-', '--'],levels=[-.5, 0, .5])
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())
plt.show()
# Gaussian Kernel
from sklearn.svm import SVC
svclassifier = SVC(kernel='rbf')
svclassifier.fit(X_train, y_train)
y_pred = svclassifier.predict(X_test)
y_pred
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
# new_x, new_y, class_y, points_X, class_y