代写代考 Q2_solution

Q2_solution

Copyright By PowCoder代写 加微信 powcoder

# Wk11 Task 2. SVM classifier and confusion matrix

import sys
import string

if __name__ == ‘__main__’:
from sklearn import svm
from sklearn.metrics import confusion_matrix

X_train=[[0, 0, 0, 0, 2], [3, 0, 1, 0, 1], [0, 0, 0, 0, 1], [2, 0, 3, 0, 2], [5, 2, 0, 0, 1], [0, 0, 1, 0, 1], [0, 1, 1, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [1, 1, 0, 1, 2]]
y_train=[-1, 1, -1, 1, 1, -1, -1, -1, -1, -1]

X_test=[[1, 1, 1, 1, 2], [3, 0, 2, 0, 3], [0, 0, 0, 0, 0], [6, 0, 5, 0, 1], [4, 2, 0, 2, 1], [0, 0, 1, 1, 1], [0, 1, 0, 0, 1], [1, 0, 0, 0, 1], [0, 1, 0, 0, 1], [1, 1, 0, 1, 2]]
y_test=[-1, 1, 1, 1, 1, -1, -1, -1, -1, -1]

clf = svm.SVC(gamma=’scale’)
clf.fit(X_train, y_train)
result1 = clf.predict(X_test)
print(“SVC Predication Results: ” + str(result1))
supp_v = clf.support_vectors_
print(“Support Vectors: ” + str(supp_v))

lin_clf = svm.LinearSVC()
fit=lin_clf.fit(X_train, y_train)
result2 = fit.predict(X_test)
print(“Linear Predication Results: s” + str(result2))
dec = lin_clf.decision_function(X_test)
print(“Linear decision function values: ” + str(dec))

print(“The Confusion Matrix for Result1::”)
print(confusion_matrix(y_test, result1))

print(“The Confusion Matrix for Result2::”)
print(confusion_matrix(y_test, result2))

SVC Predication Results: [-1 1 -1 1 1 -1 -1 -1 -1 -1]
Support Vectors: [[0. 0. 0. 0. 2.]
[0. 0. 1. 0. 1.]
[0. 1. 1. 0. 1.]
[1. 1. 0. 1. 2.]
[3. 0. 1. 0. 1.]
[2. 0. 3. 0. 2.]
[5. 2. 0. 0. 1.]]
Linear Predication Results: s[-1 1 -1 1 1 -1 -1 -1 -1 -1]
Linear decision function values: [-0.68289058 0.83908391 -0.63286029 4.31189413 1.01606936 -0.7565212
-1.20341656 -0.33193098 -1.20341656 -0.96746872]
The Confusion Matrix for Result1::
The Confusion Matrix for Result2::

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com