-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkpca_iris_SVC.py
More file actions
70 lines (61 loc) · 2.66 KB
/
kpca_iris_SVC.py
File metadata and controls
70 lines (61 loc) · 2.66 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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import decomposition
#df.to_csv('iris.csv')
from sklearn.preprocessing import StandardScaler
from sklearn import metrics
from sklearn.svm import SVC # 'rbf'
import time as t
from sklearn.model_selection import train_test_split
# load dataset into Pandas DataFrame
def SVC_With_Different_Kernels(data_before_KPCA,data_after_KPCA,ker):
print('* NOTE: Dimensions are reduced using KPCA with kernel:'+str(ker))
df = pd.read_csv(data_before_KPCA)
features = ['sepal length', 'sepal width', 'petal length', 'petal width']
# Separating out the features
X = df.loc[:, features].values
# Separating out the target
Y = df.loc[:,['target']].values
X_train, X_test, y_train, y_test = train_test_split(
X, Y, test_size = 0.3, random_state = 100)
y_train=y_train.ravel()
y_test=y_test.ravel()
#classifier.fit(X_train, y_train.squeeze())
s=t.time()
svm_model_linear = SVC(kernel ='linear', C = 1).fit(X_train, y_train)
svm_predictions = svm_model_linear.predict(X_test)
e=t.time()
# model accuracy for X_test
accuracy = svm_model_linear.score(X_test, y_test)
print('*'*11,' SVC Kernel : LINEAR ','*'*11)
print('Time taken for classification is :',e-s )
print('Accuracy of IRIS Dataset Before KPCA using SVC is :',accuracy*100)
#============================================================================
df = pd.read_csv(data_after_KPCA)
#df.to_csv('iris.csv')
from sklearn.preprocessing import StandardScaler
features = ['PC-1', 'PC-2']
# Separating out the features
X = df.loc[:, features].values
# Separating out the target
Y = df.loc[:,['target']].values
X_train, X_test, y_train, y_test = train_test_split(
X, Y, test_size = 0.3, random_state = 100)
y_train=y_train.ravel()
y_test=y_test.ravel()
s=t.time()
svm_model_linear = SVC(kernel ='linear', C = 1).fit(X_train, y_train)
svm_predictions = svm_model_linear.predict(X_test)
e=t.time()
# model accuracy for X_test
accuracy = svm_model_linear.score(X_test, y_test)
print('Time taken for classification is :',e-s )
print('Accuracy of IRIS Dataset After KPCA using SVC is :',accuracy*100,'\n')
data_before_KPCA="D:\Python_programs\ML\Iris Data\KPCA\iris.csv"
files=['D:\Python_programs\ML\Iris Data\KPCA\iris_after_KPCA_using_linear.csv',
'D:\Python_programs\ML\Iris Data\KPCA\iris_after_KPCA_using_rbf.csv',
'D:\Python_programs\ML\Iris Data\KPCA\iris_after_KPCA_using_poly.csv']
ker=['linear','rbf','poly']
for i in range(3):
SVC_With_Different_Kernels(data_before_KPCA,files[i],ker[i])