-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupport-vector-machine.py
176 lines (140 loc) · 5.54 KB
/
support-vector-machine.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
# Encoding: utf-8
"""
written by: Lawrence McDaniel
https://lawrencemcdaniel.com
date: jun-2023
usage: minimalist implementation of Support Vector Machine models.
- Linear Kernel
- RBF Kernel
"""
import os
import warnings
# ------------------------------------------------------------------------------
# IMPORTANT: DON'T FORGET TO INSTALL THESE LIBRARIES WITH pip
# ------------------------------------------------------------------------------
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.preprocessing import MinMaxScaler
# module initializations
sns.set()
HERE = os.path.abspath(os.path.dirname(__file__))
warnings.filterwarnings("ignore")
def metrics_score(actual, predicted):
"""
Create a common function for measuring the
accuracy of both the train as well as test data.
"""
print("Metrics Score.")
print(classification_report(actual, predicted))
cm = confusion_matrix(actual, predicted)
plt.figure(figsize=(8, 5))
sns.heatmap(
cm,
annot=True,
fmt=".2f",
xticklabels=["Not Cancelled", "Cancelled"],
yticklabels=["Not Cancelled", "Cancelled"],
)
plt.ylabel("Actual")
plt.xlabel("Predicted")
plt.show()
def prepare_data():
"""
Raw database transformations:
- clean the data
- remove columns that don't contain any information
- recast data types as necessary
- convert categorical data into series of dummy columns
- split dependent / independent variables
- split training / test data sets
"""
print("Preparing data sets")
original_db = pd.read_csv(os.path.join(HERE, "data", "reservations-db.csv"))
# need to be careful to only work with a **COPY** of the original
# source data, lest we accidentally permanently modify any of this
# raw data.
data = original_db.copy()
# remove the ID column from the data set, since it contains
# no predictive information.
data = data.drop(["Booking_ID"], axis=1)
# recast dependent variable as boolean
data["booking_status"] = data["booking_status"].apply(
lambda x: 1 if x == "Canceled" else 0
)
# hive off the dependent variable, "booking_status"
x = data.drop(["booking_status"], axis=1)
y = data["booking_status"]
# encode all categorical features
x = pd.get_dummies(x, drop_first=True)
# Split data in train and test sets
return train_test_split(x, y, test_size=0.30, stratify=y, random_state=1)
def linear_Kernel():
"""
- create training and test data sets
- create a Logistic Regression model
- train the model
- generate confusion matrix and f-score for the training set
- generate confusion matrix and f-score for the test set
"""
print("Linear Kernel")
x_train, x_test, y_train, y_test = prepare_data()
print("- scaling")
scaling = MinMaxScaler(feature_range=(-1, 1)).fit(x_train)
x_train_scaled = scaling.transform(x_train)
x_test_scaled = scaling.transform(x_test)
print("- training")
svm = SVC(kernel="linear", probability=True)
model = svm.fit(X=x_train_scaled, y=y_train)
print("- modeling on training data")
y_pred_train_svm = model.predict(x_train_scaled)
metrics_score(y_train, y_pred_train_svm)
print("- modeling on test data")
y_pred_test_svm = model.predict(x_test_scaled)
metrics_score(y_test, y_pred_test_svm)
# Set the optimal threshold (refer to the Jupyter Notebook to see how we arrived at 40)
optimal_threshold_svm = 0.40
print("- remodeling on training data")
y_pred_train_svm = model.predict_proba(x_train_scaled)
metrics_score(y_train, y_pred_train_svm[:, 1] > optimal_threshold_svm)
print("- remodeling on test data")
y_pred_test = model.predict_proba(x_test_scaled)
metrics_score(y_test, y_pred_test[:, 1] > optimal_threshold_svm)
def rbf_Kernel():
"""
- create training and test data sets
- create a Logistic Regression model
- train the model
- generate confusion matrix and f-score for the training set
- generate confusion matrix and f-score for the test set
"""
print("RBF Kernel")
x_train, x_test, y_train, y_test = prepare_data()
print("- scaling")
scaling = MinMaxScaler(feature_range=(-1, 1)).fit(x_train)
x_train_scaled = scaling.transform(x_train)
x_test_scaled = scaling.transform(x_test)
# Linear Kernel or linear decision boundary
print("- training")
svm_rbf = SVC(kernel="rbf", probability=True)
model = svm_rbf.fit(x_train_scaled, y_train)
print("- modeling on training data")
y_pred_train_svm = model.predict(x_train_scaled)
metrics_score(y_train, y_pred_train_svm)
print("- modeling on test data")
y_pred_test_svm = model.predict(x_test_scaled)
metrics_score(y_test, y_pred_test_svm)
# Set the optimal threshold (refer to the Jupyter Notebook to see how we arrived at 41)
optimal_threshold_svm = 0.41
print("- remodeling on training data")
y_pred_train_svm = model.predict_proba(x_train_scaled)
metrics_score(y_train, y_pred_train_svm[:, 1] > optimal_threshold_svm)
print("- remodeling on test data")
y_pred_test = model.predict_proba(x_test_scaled)
metrics_score(y_test, y_pred_test[:, 1] > optimal_threshold_svm)
if __name__ == "__main__":
linear_Kernel()
rbf_Kernel()