当前位置: 首页 > news >正文

机器学习:逻辑回归处理手写数字的识别

1、获取数据, 图像分割该数据有50行100列,每个数字占据20*20个像素点,可以进行切分,划分出训练集和测试集。

import numpy as np
import pandas as pd
import cv2
img=cv2.imread("digits.png")#读取文件
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#变成灰度图
#切分数据
x=np.array([np.hsplit(i,100) for i in np.vsplit(gray,50)])
train=x[:,:50]
test=x[:,50:100]

2、每个数据的像素点为20*20,将其全部变成一列1*400格式,转换成数值特征

train_new=train.reshape(-1,400).astype(np.float32)
test_new=test.reshape(-1,400).astype(np.float32)

3、总共有2500行特征对应着2500个标签,从0到9每个数字有250个

k=np.arange(10)
train_labels=np.repeat(k,250)[:,np.newaxis].ravel()
test_labels=np.repeat(k,250)[:,np.newaxis].ravel()

4、导入逻辑回归库,采用交叉验证的方法找到最佳C值

#导入逻辑回归和交叉验证库
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
scores=[]
# 设置C的值进行交叉验证,找到最佳C
c_param_range=[0.01,0.1,1,10,100]
for i in c_param_range:lr = LogisticRegression(C=i, penalty='l2', solver='lbfgs', max_iter=10000)score = cross_val_score(lr, train_new, train_labels, cv=10, scoring='recall_macro')score_mean = sum(score) / len(score)scores.append(score_mean)
# 选择使得平均分数最高的C值
best_c = c_param_range[np.argmax(scores)]
lr = LogisticRegression(C=best_c, penalty='l2', max_iter=10000)
#使用最佳C值初始化逻辑回归模型并训练
lr.fit(train_new, train_labels)

5、使用训练好的模型对测试集进行预测

from sklearn import metrics
train_predicted=lr.predict(test_new)
print(metrics.classification_report(test_labels,train_predicted))

6、打印的分类报告

7、读取手写数字图像,并进行预测

p1=cv2.imread("p1.png")
gray_p1=cv2.cvtColor(p1,cv2.COLOR_BGR2GRAY)
tess=np.array(gray_p1)
tess_new=tess.reshape(-1,400).astype(np.float32)
# 使用训练好的模型进行预测
predicted_shouxie=lr.predict(tess_new)
print(predicted_shouxie)

8、书写预测结果

完整代码

import numpy as np
import pandas as pd
import cv2
img=cv2.imread("digits.png")#读取文件
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#变成灰度图
#切分数据
x=np.array([np.hsplit(i,100) for i in np.vsplit(gray,50)])
train=x[:,:50]
test=x[:,50:100]
train_new=train.reshape(-1,400).astype(np.float32)
test_new=test.reshape(-1,400).astype(np.float32)
k=np.arange(10)
train_labels=np.repeat(k,250)[:,np.newaxis].ravel()
test_labels=np.repeat(k,250)[:,np.newaxis].ravel()#导入逻辑回归和交叉验证库
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
scores=[]
# 设置C的值进行交叉验证,找到最佳C
c_param_range=[0.01,0.1,1,10,100]
for i in c_param_range:lr = LogisticRegression(C=i, penalty='l2', solver='lbfgs', max_iter=10000)score = cross_val_score(lr, train_new, train_labels, cv=10, scoring='recall_macro')score_mean = sum(score) / len(score)scores.append(score_mean)
# 选择使得平均分数最高的C值
best_c = c_param_range[np.argmax(scores)]
lr = LogisticRegression(C=best_c, penalty='l2', max_iter=10000)
lr.fit(train_new, train_labels)
#使用最佳C值初始化逻辑回归模型并训练
from sklearn import metrics
train_predicted=lr.predict(test_new)
print(metrics.classification_report(test_labels,train_predicted))
# 读取新的手写数字图像,并进行预测
p1=cv2.imread("p1.png")
gray_p1=cv2.cvtColor(p1,cv2.COLOR_BGR2GRAY)
tess=np.array(gray_p1)
tess_new=tess.reshape(-1,400).astype(np.float32)
# 使用训练好的模型进行预测
predicted_shouxie=lr.predict(tess_new)
print(predicted_shouxie)

http://www.lryc.cn/news/429285.html

相关文章:

  • 文件上传真hard
  • 精益管理|介绍一本专门研究防错法(Poka-Yoke)的书
  • 面试题目:(4)给表达式添加运算符
  • [C#]将opencvsharp的Mat对象转成onnxruntime的inputtensor的3种方法
  • CTF入门教程(非常详细)从零基础入门到竞赛,看这一篇就够了!
  • 数据链路层 I(组帧、差错控制)【★★★★★】
  • 悟空降世 撼动全球
  • Swoole 和 Java 哪个更有优势呢
  • Salesforce 发布开源大模型 xGen-MM
  • 冒 泡 排 序
  • 采用先进的人工智能视觉分析技术,能够精确识别和分析,提供科学、精准的数据支持的智慧物流开源了。
  • IAA游戏APP如何让合理地让用户观看更多广告,提高广告渗透率
  • 环网交换机的特殊作用是什么?
  • mac电脑安装Zsh并启用
  • 【后续更新】python搜集上海二手房数据
  • 创建GPTs,打造你的专属AI聊天机器人
  • 深度学习 vector 之模拟实现 vector (C++)
  • 关于LLC知识10
  • 最长的严格递增或递减子数组
  • 【JavaEE】SpringBoot 统一功能处理:拦截器、统一数据返回与异常处理的综合应用与源码解析
  • I2C学习:上拉电阻选取
  • AC自动机-1
  • 注解@Service@Component@Slf4j@Data
  • 【Nodejs】六、express框架
  • 进阶 pro max
  • Agentic Security:一款针对LLM模型的模糊测试与安全检测工具
  • Spring Cloud Config 与 Spring Cloud Bus 来实现动态配置文件
  • Qt:Qt背景
  • 【数据结构】选择排序
  • 国产GD32单片机开发入门(二)GD32单片机详解