机器学习逻辑回归分类,图片 0-9 分类
"""
@author:Jerry Liu
@file: DigitsLogisticRegression.py
@version:
@time: 2018/12/11
@email:liudongbing8@163.com
@function: https://www.jianshu.com/p/0e6fb477a159
"""
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn import metrics
# 划分训练与测试数据集
from sklearn.model_selection import train_test_split
# Logistic Regression Classified
def logistic_regression_classifier(x_train, y_train):
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(x_train, y_train)
return model
# Showing the image and the Labels(Digits Dataset)
def plot_figure():
plt.figure()
# zip 数据打包为元组形式
for index, (image, label) in enumerate(zip(digits.data[0:5],digits.target[0:5])):
plt.subplot(1, 5, index + 1)
plt.imshow(np.reshape(image, (8, 8)))
plt.title("Training:%i\n" % label)
plt.show()
if __name__ == "__main__":
digits = load_digits()
# Print to show there are 1797 images(8*8)
print("Image Data Shape = ", digits.data.shape)
# Print to show There are 1797 labels (intergers form 0-9)
print("Label Data Shape = ", digits.target.shape)
#绘制部分图片
plot_figure()
# 划分训练与测试数据集
x_train, x_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.25, random_state=0)
# 搭建模型
model = logistic_regression_classifier(x_train, y_train)
# 预测数据
predictions = model.predict(x_test)
# Measuring Model Performance
# 测试正确率,方案 ①
accuracy = metrics.accuracy_score(y_test, predictions)
print('accuracy: %.2f%%' % (100 * accuracy))
# 测试正确率,方案 ②,同方案1 一致
score = model.score(x_test, y_test)
print("score:%.2f%%" % (100 * score))
参考文章:
https://towardsdatascience.com/logistic-regression-using-python-sklearn-numpy-mnist-handwriting-recognition-matplotlib-a6b31e2b166a