-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVM.py
More file actions
40 lines (35 loc) · 1.38 KB
/
SVM.py
File metadata and controls
40 lines (35 loc) · 1.38 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
import read
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.ensemble import forest
from sklearn import tree
print("Reading data ...")
x_all, y_all = read.read(LOAD_DATA=False)
x_train, x_test, y_train, y_test = train_test_split(x_all, y_all, test_size=0.2, random_state=35)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
classifier_svm = SVC(kernel='rbf', random_state=0, gamma='scale')
classifier_rndf = forest.RandomForestClassifier(n_estimators=10)
classifier_dTree = tree.DecisionTreeClassifier()
classifier_svm.fit(x_train, y_train)
classifier_rndf.fit(x_train,y_train)
classifier_dTree.fit(x_train,y_train)
y_pred_svm = classifier_svm.predict(x_test)
y_pred_rndf = classifier_rndf.predict(x_test)
y_pred_dTree = classifier_dTree.predict(x_test)
cm = confusion_matrix(y_test, y_pred_svm)
print("--Confusion Matrix of SVM--")
print(cm)
print("--Accuracy of SVM--")
print(accuracy_score(y_test, y_pred_svm))
cm2 = confusion_matrix(y_test, y_pred_rndf)
print("--Confusion Matrix of Random Forest--")
print(cm2)
print("--Accuracy of Random Forest--")
print(accuracy_score(y_test, y_pred_rndf))
cm3 = confusion_matrix(y_test, y_pred_dTree)
print("--Confusion Matrix of Decision Tree--")
print(cm3)
print("--Accuracy of Decision Tree--")
print(accuracy_score(y_test, y_pred_dTree))