-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsvm-ozone-linear.py
106 lines (73 loc) · 2.74 KB
/
svm-ozone-linear.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
from sklearn import svm
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn import metrics
import pandas as pd
from sklearn.metrics import recall_score, f1_score, precision_score
#from sklearn.preprocessing import LabelEncoder
df = pd.read_csv('ozone-data.csv')
#print(df.head())
#data select
X = df.iloc[:,1:73]
#print(X)
#target select [class]
y = df.iloc[:,73:74]
#print(y)
#dataframe to numpy array
yyy = np.array(y).ravel()
XXX = np.array(X).reshape(2534,-1)
#print(target)
#print(connects)
#target_variables = LabelEncoder()
#y['Date_data'] = target_variables.fit_transform(y['Date'])
#y_n = y.drop(['Date'],axis=1)
#print("Features: ", X)
#print("Target: ", y_n)
X_train, X_test, y_train, y_test = train_test_split(XXX,yyy,test_size=0.2880820836621942,random_state=49) # 80% training and 20% test
#creating of SVM model
model_ozone = svm.SVC(kernel='linear', C=3) # Linear Kernel
#Train the model using the training sets
model_ozone.fit(X_train, y_train)
#Predict the response for test dataset
y_test_pred = model_ozone.predict(X_test)
#print(X_test)
#print(y_test)
y_train_pred = model_ozone.predict(X_train)
from sklearn.metrics import confusion_matrix
c_matrix = confusion_matrix(y_train, y_train_pred)
print(c_matrix)
#[[1669 7]
# [ 115 13]]
#C = 1.0
#h = .02
from sklearn.metrics import auc
fpr, tpr, _ = metrics.roc_curve(y_test, y_test_pred)
auc_test_roc_curve= auc(fpr,tpr)
print("Auc Roc Curve Score: ",auc_test_roc_curve)
#Auc Roc Curve Score: 0.5617836676217765
print("Precision Score: ", precision_score(y_test, y_test_pred, average="macro").mean()*100)
print("Recall Score: ", recall_score(y_test, y_test_pred, average="macro").mean()*100)
print("F1 Score: ",f1_score(y_test, y_test_pred, average="macro").mean()*100)
#Precision Score: 88.06896551724138
#Recall Score: 56.178366762177646
#F1 Score: 59.79183681221629
#rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X_train, y_train)
#poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X_train, y_train)
#lin_svc = svm.LinearSVC(C=C).fit(X_train, y_train)
#print(y_pred)
print("Accuracy:",metrics.accuracy_score(y_test,y_test_pred))
#Accuracy: 0.9561643835616438
import matplotlib.pyplot as plt
#plt.scatter(X_train[:,0], X_train[:,1])
#plt.title('Linearly separable data')
#plt.xlabel('X1')
#plt.ylabel('X2')
#plt.show()
support_vectors = model_ozone.support_vectors_
# Visualize support vectors
plt.scatter(X_train[:,0], X_train[:,1])
plt.scatter(support_vectors[:,0], support_vectors[:,1], color='red')
plt.title('Ozone Day and Normal Day Prediction Software')
plt.xlabel('Data')
plt.ylabel('Class [Ozone Day = 1, Normal Day = 0]')
plt.show()