手写体数字预测

导入数据

1 # 从sklearn.datasets里导入手写体数字加载器。
2 from sklearn.datasets import load_digits
3 digits = load_digits()

数据分割及标准化

数据分割

1 from sklearn.model_selection import train_test_split
2 X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.25, random_state=33)

数据标准化

1 # 从sklearn.preprocessing里导入数据标准化模块。
2 from sklearn.preprocessing import StandardScaler
3 from sklearn.svm import LinearSVC
4 ss = StandardScaler()
5 X_train = ss.fit_transform(X_train)
6 X_test = ss.transform(X_test)

模型训练

1 # 初始化线性假设的支持向量机分类器LinearSVC。
2 lsvc = LinearSVC()
3 lsvc.fit(X_train, y_train)
4 y_predict = lsvc.predict(X_test)
5 print(y_predict)

 预测性能评分

1 # 查看评分
2 lsvc.score(X_test, y_test)
3 from sklearn.metrics import classification_report
4 print(classification_report(y_test, y_predict, target_names=digits.target_names.astype(str)))

评分如图1-1

      图1-1 支持向量机模型评分

 

posted @ 2021-09-19 12:38  李点  阅读(81)  评论(0)    收藏  举报