使用了TensorFlow中的mnist数据集
from sklearn import svm
import numpy as np
from sklearn.metrics import classification_report
from tensorflow.keras.datasets.mnist import load_data
(x_train, y_train), (x_test, y_test) = load_data()
x_train = x_train.reshape(x_train.shape[0], -1)
x_test = x_test.reshape(x_test.shape[0], -1)
# 获取一个支持向量机模型
predictor = svm.SVC(gamma='scale', C=1.0, decision_function_shape='ovr', kernel='rbf')
# 把数据丢进去
predictor.fit(x_train, y_train)
y_pred = predictor.predict(x_test)
print(classification_report(y_test, y_pred))
准确率打印
precision recall f1-score support
0 0.98 0.99 0.99 980
1 0.99 0.99 0.99 1135
2 0.98 0.97 0.98 1032
3 0.97 0.99 0.98 1010
4 0.98 0.98 0.98 982
5 0.99 0.98 0.98 892
6 0.99 0.99 0.99 958
7 0.98 0.97 0.97 1028
8 0.97 0.98 0.97 974
9 0.97 0.96 0.97 1009
accuracy 0.98 10000
macro avg 0.98 0.98 0.98 10000
weighted avg 0.98 0.98 0.98 10000