Skip to content

Commit 2015125

Browse files
authored
Create svm.py
1 parent b2c2de1 commit 2015125

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

SVM/svm.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#Importing Libraries
2+
import pandas as pd
3+
import numpy as np
4+
from sklearn.model_selection import train_test_split
5+
from matplotlib import pyplot as plt
6+
from sklearn import svm
7+
from sklearn.metrics import accuracy_score
8+
from sklearn.model_selection import KFold
9+
from sklearn.metrics import classification_report, confusion_matrix
10+
11+
#data
12+
data = pd.read_csv("17054.csv")
13+
14+
#X and y for classification
15+
X = data.drop('label',axis =1)
16+
y = data.label
17+
18+
#Scaling the features
19+
from sklearn.preprocessing import StandardScaler
20+
scaler = StandardScaler()
21+
X = scaler.fit_transform(X)
22+
23+
#Splitting the dataset
24+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=51)
25+
26+
27+
#model without Grid search
28+
svm_model = svm.SVC(kernel='rbf',gamma='scale', C=1) #C value varied
29+
svm_model.fit(X_train,y_train)
30+
predictions = svm_model.predict(X_test)
31+
#score = accuracy_score(predictions, y_test)
32+
print("The report without using Grid search CV : ")
33+
print(classification_report(y_test, predictions))
34+
35+
36+
#Grid Search for best parameters
37+
from sklearn.model_selection import GridSearchCV
38+
param_grid = {'C': [0.01,0.1, 1, 10, 100],
39+
'gamma': [1, 0.1, 0.01, 0.001, 'auto','scale'],
40+
'kernel': ['rbf']}
41+
42+
grid = GridSearchCV(svm.SVC(), param_grid, refit = True, verbose = 3)
43+
44+
# fitting the model for grid search
45+
grid.fit(X_train, y_train)
46+
grid_predictions = grid.predict(X_test)
47+
print("After using Grid Search CV: ")
48+
print(classification_report(y_test, grid_predictions))

0 commit comments

Comments
 (0)