/
04_svm_rf.py
32 lines (29 loc) · 1.09 KB
/
04_svm_rf.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
# SVM results
from sklearn.svm import SVC
from sklearn import metrics
for kernel in ['rbf', 'linear']:
clf = SVC(kernel=kernel).fit(Xtrain, ytrain)
ypred = clf.predict(Xtest)
print("SVC: kernel = {0}".format(kernel))
print(metrics.f1_score(ytest, ypred))
plt.figure()
plt.imshow(metrics.confusion_matrix(ypred, ytest),
interpolation='nearest', cmap=plt.cm.binary)
plt.colorbar()
plt.xlabel("true label")
plt.ylabel("predicted label")
plt.title("SVC: kernel = {0}".format(kernel))
# random forest results
from sklearn.ensemble import RandomForestClassifier
for max_depth in [3, 5, 10]:
clf = RandomForestClassifier(max_depth=max_depth).fit(Xtrain, ytrain)
ypred = clf.predict(Xtest)
print("RF: max_depth = {0}".format(max_depth))
print(metrics.f1_score(ytest, ypred))
plt.figure()
plt.imshow(metrics.confusion_matrix(ypred, ytest),
interpolation='nearest', cmap=plt.cm.binary)
plt.colorbar()
plt.xlabel("true label")
plt.ylabel("predicted label")
plt.title("RF: max_depth = {0}".format(max_depth))