-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEX_Support_Vector_Machine-classification.py
More file actions
181 lines (70 loc) · 2.96 KB
/
Copy pathEX_Support_Vector_Machine-classification.py
File metadata and controls
181 lines (70 loc) · 2.96 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
#!/usr/bin/env python
# coding: utf-8
# # Support Vector Machine classification
# #### Support Vector Machine (SVM) classification is a powerful supervised learning algorithm used primarily for binary classification tasks. Here’s a concise overview of its key concepts, workings, and applications.
# ##### SVM is designed to find the best decision boundary (hyperplane) that separates data points of different classes in a high-dimensional space.
# ##### Key Concepts :
#
# ##### Hyperplane: A hyperplane is a flat affine subspace that divides the data into two classes. In a two-dimensional space, it is simply a line; in three dimensions, it is a plane.
# ##### Support Vectors: These are the data points that are closest to the hyperplane and influence its position and orientation. The SVM algorithm focuses on these points for constructing the decision boundar .
# ##### Margin: The margin is the distance between the hyperplane and the nearest support vectors from either class. SVM aims to maximize this margin, which helps improve classification accurac.
# In[ ]:
# ###### How SVM Works
# ###### Training Phase: During training, SVM identifies the optimal hyperplane that maximizes the margin between classes using support vectors.
# ###### Prediction Phase: For new data points, SVM determines which side of the hyperplane they fall on to classify them into one of the categories.
# In[ ]:
# In[1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
# In[4]:
dataset = pd.read_csv('Social_Network_Ads.csv')
dataset
# In[6]:
# plot the distribution
plt.scatter(dataset['Age'],dataset['EstimatedSalary'],c=dataset['Purchased'],cmap='rainbow')
plt.xlabel('Age')
plt.ylabel('Est. Salary')
plt.show()
# In[9]:
# transform the dataset in Rows and Cloumn
X = dataset.iloc[:,[2,3]].values
X
# In[10]:
Y = dataset.iloc[:,-1].values
Y
# In[ ]:
# In[12]:
# import the sklearn libraries
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,Y,test_size=0.2,random_state=0)
# In[ ]:
# In[13]:
# import the SVC packages
from sklearn.svm import SVC
# In[16]:
# prepare the model
model = SVC(kernel='rbf')
#### kernel='rbf': This parameter specifies the type of kernel function to be used in the SVC model.
# The RBF kernel is a popular choice for handling non-linear classification problems.
# It maps input features into a higher-dimensional space where a linear separation is possible.
# In[ ]:
# In[17]:
model.fit(X_train,y_train)
# In[19]:
# model prediction
y_pred = model.predict(X_test)
y_pred
# In[20]:
# Analyse the prediction with actual data
from sklearn.metrics import confusion_matrix,classification_report
# In[21]:
print(confusion_matrix(y_test,y_pred))
# In[ ]:
# In[22]:
# analyse the overall model score
print(classification_report(y_test,y_pred))
# In[ ]:
# In[ ]: