0.引言
0.引言
介绍了采用sklearn的多少个机械学习模型进行手写体识别。
介绍了选取sklearn的多少个机器学习模型进行手写体识别。
1.费用条件
1.支出条件
python: 3.6.3
python: 3.6.3
PIL,cv2, pandas, numpy, os, csv, random
PIL,cv2, pandas, numpy, os, csv, random
必要调用的sklearn库:
须求调用的sklearn库:
1 from sklearn.linear_model import LogisticRegression #线性模型中的Logistic回归模型
2 from sklearn.linear_model import SGDClassifier #线性模型中的随机梯度下降模型
3 from sklearn.svm import LinearSVC #SVM模型中的线性SVC模型
4 from sklearn.neural_network import MLPClassifier #神经网络模型中的多层网络模型
1 from sklearn.linear_model import LogisticRegression #线性模型中的Logistic回归模型
2 from sklearn.linear_model import SGDClassifier #线性模型中的随机梯度下降模型
3 from sklearn.svm import LinearSVC #SVM模型中的线性SVC模型
4 from sklearn.neural_network import MLPClassifier #神经网络模型中的多层网络模型
2.总体规划设计思路
2.一体化规划思路
图1 全部的框架设计
图1 全部的框架设计
工程的目标,是想利用机械学习模型去练习识别生成的人身自由验证码图像(单个数字1-9),通过以下八个步骤达成:
工程的目标,是想利用机械学习模型去磨炼识别生成的自由验证码图像(单个数字1-9),通过以下多个步骤完结:
1.生成多张单个验证码图像
1.生成多张单个验证码图像
2.领到特征向量写入CSV
2.提取特征向量写入CSV
3.sklearn模型演习和测试
3.sklearn模型演习和测试
图2 全部的规划流程
图2 全体的安插性流程
3.编制程序进程
3.编程进度
3.1 生成多张单个验证码图像
3.1 生成多张单个验证码图像
图3 生成的多张单个验证码图像
图3 生成的多张单个验证码图像
项目的率先步,是内需扭转单个手写体图像。
项指标率先步,是亟需扭转单个手写体图像。
随机生成数字1-9,然后利用PIL的画笔工具进行画图,然后依据随便数的真实标记1-9,保存到对应文件夹内,然后用标记+序号命名。重心在介绍机器学习模型的采纳,验证码生成这里不再过多介绍。
随机生成数字1-9,然后使用PIL的画笔工具举行画图,然后依照随便数的忠实标记1-9,保存到相应文件夹内,然后用标记+序号命名。重心在介绍机器学习模型的采用,验证码生成那里不再过多介绍。
1 draw = ImageDraw.Draw(im) # 画笔工具
1 draw = ImageDraw.Draw(im) # 画笔工具
**3.2 **提取特征向量写入CSV
**3.2 **提取特征向量写入CSV
这一步是领取图像中的特征。生成的单个图像是30*30即900个像素点的。
这一步是领取图像中的特征。生成的单个图像是30*30即900个像素点的。
为了降低维度,没有选取900个像素点每点的灰度作为输入,而是精选了30行每行的黑点数,和30列每列的黑点数作为输入,那样降到了60维。
为了下落维度,没有选用900个像素点每点的灰度作为输入,而是精选了30行每行的黑点数,和30列每列的黑点数作为输入,这样降到了60维。
(a)提取900维特征
(a)提取900维特征
(b)提取60维特征
(b)提取60维特征
图4 提取图像特点
图4 提取图像特点
天性的领到也比较不难,逐行逐列总括然后计数求和:
特点的领到也比较简单,逐行逐列总括然后计数求和:
1 def get_feature(img):
2 # 提取特征
3 # 30*30的图像,
4
5 width, height = img.size
6
7 global pixel_cnt_list
8 pixel_cnt_list=[]
9
10 height = 30
11 for y in range(height):
12 pixel_cnt_x = 0
13 for x in range(width):
14 # print(img.getpixel((x,y)))
15 if img.getpixel((x, y)) == 0: # 黑点
16 pixel_cnt_x += 1
17
18 pixel_cnt_list.append(pixel_cnt_x)
19
20 for x in range(width):
21 pixel_cnt_y = 0
22 for y in range(height):
23 if img.getpixel((x, y)) == 0: # 黑点
24 pixel_cnt_y += 1
25
26 pixel_cnt_list.append(pixel_cnt_y)
27
28 return pixel_cnt_list
1 def get_feature(img):
2 # 提取特征
3 # 30*30的图像,
4
5 width, height = img.size
6
7 global pixel_cnt_list
8 pixel_cnt_list=[]
9
10 height = 30
11 for y in range(height):
12 pixel_cnt_x = 0
13 for x in range(width):
14 # print(img.getpixel((x,y)))
15 if img.getpixel((x, y)) == 0: # 黑点
16 pixel_cnt_x += 1
17
18 pixel_cnt_list.append(pixel_cnt_x)
19
20 for x in range(width):
21 pixel_cnt_y = 0
22 for y in range(height):
23 if img.getpixel((x, y)) == 0: # 黑点
24 pixel_cnt_y += 1
25
26 pixel_cnt_list.append(pixel_cnt_y)
27
28 return pixel_cnt_list
所以大家接下去需求做的劳作是,遍历访问此前放入single文件夹中子文件夹1-9中的全部图像文件,举办特征提取,然后写入csv文件中:
所以大家接下去必要做的干活是,遍历访问从前放入single文件夹中子文件夹1-9中的全体图像文件,举办特征提取,然后写入csv文件中:
1 with open("D:/***/" + "***.csv", "w", newline="") as csvfile:
2 writer = csv.writer(csvfile)
3 # 访问文件夹 1-9
4 for i in range(1, 10):
5 namedir = os.listdir("D:/***/single/single_test/" + str(i))
6 # 访问逐个图像文件
7 for j in range(0, (len(namedir))):
8 img = Image.open("D:/***/single/single_test/" + str(i) + "/" + namedir[j])
9 get_feature(img)
10
11 # 写入csv
12 pixel_cnt_list.append(namedir[j][0])
13 writer.writerow(pixel_cnt_list)
1 with open("D:/***/" + "***.csv", "w", newline="") as csvfile:
2 writer = csv.writer(csvfile)
3 # 访问文件夹 1-9
4 for i in range(1, 10):
5 namedir = os.listdir("D:/***/single/single_test/" + str(i))
6 # 访问逐个图像文件
7 for j in range(0, (len(namedir))):
8 img = Image.open("D:/***/single/single_test/" + str(i) + "/" + namedir[j])
9 get_feature(img)
10
11 # 写入csv
12 pixel_cnt_list.append(namedir[j][0])
13 writer.writerow(pixel_cnt_list)
**3.3 sklearn模型陶冶和测试**
**3.3 sklearn模型练习和测试**
此前的准备工作都做完之后,就能够使用sklearn的机械学习模型举行建立模型处理。
之前的备选干活都做完现在,就足以行使sklearn的机器学习模型进行建立模型处理。
3.3.1 特征数据加工
3.3.1 特征数据加工
率先步必要对CSV文件中的数据实行领取,利用pd.read_csv举办读取。写入CSV时,前60列为60维的特征向量,第五1名列输出标记1-9。
第壹步需求对CSV文件中的数据开始展览提取,利用pd.read_csv进行读取。写入CSV时,前60名列60维的特征向量,第肆1名列输出标记1-9。
1 import pandas as pd
2 from sklearn.model_selection import train_test_split
3
4 # 设定的生成样本个数
5 sample_num = 100
6
7 column_names = []
8
9 # 前60列为60维特征
10 for i in range(0, 60):
11 column_names.append("feature_"+str(i))
12 # 第61列为输出标记
13 column_names.append("true_number")
14
15 data = pd.read_csv("D:/***/data/test_"+str(sample_num)+".csv"
16 , names=column_names)
17
18 # print(data.shape)
19
20 # 得到训练集X—train和y_train,测试集X_test和y_test,此处取75%和25%分割
21 X_train, X_test, y_train, y_test = train_test_split(
22 data[column_names[0:60]],
23 data[column_names[60]],
24 test_size=0.25,
25 random_state=33
26 )
1 import pandas as pd
2 from sklearn.model_selection import train_test_split
3
4 # 设定的生成样本个数
5 sample_num = 100
6
7 column_names = []
8
9 # 前60列为60维特征
10 for i in range(0, 60):
11 column_names.append("feature_"+str(i))
12 # 第61列为输出标记
13 column_names.append("true_number")
14
15 data = pd.read_csv("D:/***/data/test_"+str(sample_num)+".csv"
16 , names=column_names)
17
18 # print(data.shape)
19
20 # 得到训练集X—train和y_train,测试集X_test和y_test,此处取75%和25%分割
21 X_train, X_test, y_train, y_test = train_test_split(
22 data[column_names[0:60]],
23 data[column_names[60]],
24 test_size=0.25,
25 random_state=33
26 )
通过 data.shape()
能够拿走data的维度,此时出口结果(100,61),表示有玖14个样本,65个维度,在那之中56个维度时特征值,第五2个维度为出口标记值1-9。
通过 data.shape()
能够收获data的维度,此时出口结果(100,61),表示有9伍个样本,6二个维度,在那之中伍十四个维度时特征值,第5叁个维度为出口标记值1-9。
利用sklearn库的 train_test_split函数 将数据举行分割,
利用sklearn库的 train_test_split函数 将数据进行剪切,
得报到并且接受集陶冶集数据:X_train,
y_train
得报到并且接受集练习集数据:X_train,
y_train
获得测试集数据:X_test,
y_test
得到测试集数据:X_test,
y_test
3.3.2 模型磨练
3.3.2 模型磨练
透过前边一多级的准备干活做完,那太守式启幕应用sklearn的机器学习模型,利用演练多少对模型进行陶冶,然后使用测试数据实行质量测试。
由在此以前边一多重的准备干活做完,那节度使式启幕运用sklearn的机械学习模型,利用磨练多少对模型举办磨练,然后使用测试数据进行品质测试。
1 from sklearn.preprocessing import StandardScaler
2
3 from sklearn.linear_model import LogisticRegression #线性模型中的Logistic回归模型
4 from sklearn.linear_model import SGDClassifier #线性模型中的随机梯度下降模型
5 from sklearn.svm import LinearSVC #SVM模型中的线性SVC模型
6 from sklearn.neural_network import MLPClassifier #神经网络模型中的多层网络模型
7
8 # LR
9 def con_lr():
10
11 X_train_LR = X_train
12 y_train_LR = y_train
13
14 X_test_LR = X_test
15 y_test_LR = y_test
16
17 ss = StandardScaler()
18 X_train_LR = ss.fit_transform(X_train_LR)
19 X_test_LR = ss.transform(X_test_LR)
20
21 # 初始化LogisticRegression
22 lr = LogisticRegression()
23
24 # 调用LogisticRegression中的fit()来训练模型参数
25 lr.fit(X_train_LR, y_train_LR)
26
27 # 使用训练好的模型lr对X_test进行预测,结果储存在lr_y_predict中
28 global y_predict_LR
29 y_predict_LR = lr.predict(X_test_LR)
30
31 # 性能分析
32 print('Accuarcy of LR Classifier2:', lr.score(X_test_LR, y_test_LR))
33 print (classification_report(y_test_LR, y_predict_LR))
34
35 #SGD
36 def con_sgd():
37
38 X_train_SC = X_train
39 y_train_SC = y_train
40
41 X_test_SC = X_test
42 y_test_SC = y_test
43
44 # 标准化数据
45 ss = StandardScaler()
46 X_train_SC = ss.fit_transform(X_train_SC)
47 X_test_SC = ss.transform(X_test_SC)
48
49 # 初始化SGDClassifier
50 sgdc = SGDClassifier()
51
52 # 调用SGDClassifier中的fit函数用来训练模型参数
53 sgdc.fit(X_train_SC, y_train_SC)
54
55 # 使用训练好的模型sgdc对X_test进行预测,结果储存在sgdc_y_predict中
56 global y_predict_SC
57 y_predict_SC = sgdc.predict(X_test_SC)
58
59 print ('Accarcy of SGD Classifier:', sgdc.score(X_test_SC, y_test_SC))
60 print(classification_report(y_test_SC,y_predict_SC))
61
62 #SVM方法
63 def con_svm():
64
65 X_train_SVM = X_train
66 y_train_SVM = y_train
67
68 X_test_SVM = X_test
69 y_test_SVM = y_test
70
71 ss = StandardScaler()
72 X_train_SVM = ss.fit_transform(X_train_SVM)
73 X_test_SVM = ss.transform(X_test_SVM)
74
75 #调用线性SVC实例化
76 lsvc = LinearSVC()
77 lsvc.fit(X_train_SVM, y_train_SVM)
78
79 global y_predict_SVM
80 y_predict_SVM = lsvc.predict(X_test_SVM)
81
82 print("The accurary:", lsvc.score(X_test_SVM, y_test_SVM))
83 print(classification_report(y_test_SVM, y_predict_SVM)) # , target_names=digits.target_names.astype(str)))
84
85 #神经网络
86 def con_MLPClass():
87
88 X_train_MLP = X_train
89 y_train_MLP = y_train
90
91 X_test_MLP = X_test
92 y_test_MLP = y_test
93
94 ss = StandardScaler()
95 X_train_MLP = ss.fit_transform(X_train_MLP)
96 X_test_MLP = ss.transform(X_test_MLP)
97
98 #调用MLP实例化
99 MLP = MLPClassifier(hidden_layer_sizes=(13,13,13), max_iter=500)
100 MLP.fit(X_train_MLP, y_train_MLP)
101
102 global y_predict_MLP
103 y_predict_MLP = MLP.predict(X_test_MLP)
104
105 print("The accurary:", MLP.score(X_test_MLP, y_test_MLP))
106 print(classification_report(y_test_MLP, y_predict_MLP)) # , target_names=digits.target_names.astype(str)))
1 from sklearn.preprocessing import StandardScaler
2
3 from sklearn.linear_model import LogisticRegression #线性模型中的Logistic回归模型
4 from sklearn.linear_model import SGDClassifier #线性模型中的随机梯度下降模型
5 from sklearn.svm import LinearSVC #SVM模型中的线性SVC模型
6 from sklearn.neural_network import MLPClassifier #神经网络模型中的多层网络模型
7
8 # LR
9 def con_lr():
10
11 X_train_LR = X_train
12 y_train_LR = y_train
13
14 X_test_LR = X_test
15 y_test_LR = y_test
16
17 ss = StandardScaler()
18 X_train_LR = ss.fit_transform(X_train_LR)
19 X_test_LR = ss.transform(X_test_LR)
20
21 # 初始化LogisticRegression
22 lr = LogisticRegression()
23
24 # 调用LogisticRegression中的fit()来训练模型参数
25 lr.fit(X_train_LR, y_train_LR)
26
27 # 使用训练好的模型lr对X_test进行预测,结果储存在lr_y_predict中
28 global y_predict_LR
29 y_predict_LR = lr.predict(X_test_LR)
30
31 # 性能分析
32 print('Accuarcy of LR Classifier2:', lr.score(X_test_LR, y_test_LR))
33 print (classification_report(y_test_LR, y_predict_LR))
34
35 #SGD
36 def con_sgd():
37
38 X_train_SC = X_train
39 y_train_SC = y_train
40
41 X_test_SC = X_test
42 y_test_SC = y_test
43
44 # 标准化数据
45 ss = StandardScaler()
46 X_train_SC = ss.fit_transform(X_train_SC)
47 X_test_SC = ss.transform(X_test_SC)
48
49 # 初始化SGDClassifier
50 sgdc = SGDClassifier()
51
52 # 调用SGDClassifier中的fit函数用来训练模型参数
53 sgdc.fit(X_train_SC, y_train_SC)
54
55 # 使用训练好的模型sgdc对X_test进行预测,结果储存在sgdc_y_predict中
56 global y_predict_SC
57 y_predict_SC = sgdc.predict(X_test_SC)
58
59 print ('Accarcy of SGD Classifier:', sgdc.score(X_test_SC, y_test_SC))
60 print(classification_report(y_test_SC,y_predict_SC))
61
62 #SVM方法
63 def con_svm():
64
65 X_train_SVM = X_train
66 y_train_SVM = y_train
67
68 X_test_SVM = X_test
69 y_test_SVM = y_test
70
71 ss = StandardScaler()
72 X_train_SVM = ss.fit_transform(X_train_SVM)
73 X_test_SVM = ss.transform(X_test_SVM)
74
75 #调用线性SVC实例化
76 lsvc = LinearSVC()
77 lsvc.fit(X_train_SVM, y_train_SVM)
78
79 global y_predict_SVM
80 y_predict_SVM = lsvc.predict(X_test_SVM)
81
82 print("The accurary:", lsvc.score(X_test_SVM, y_test_SVM))
83 print(classification_report(y_test_SVM, y_predict_SVM)) # , target_names=digits.target_names.astype(str)))
84
85 #神经网络
86 def con_MLPClass():
87
88 X_train_MLP = X_train
89 y_train_MLP = y_train
90
91 X_test_MLP = X_test
92 y_test_MLP = y_test
93
94 ss = StandardScaler()
95 X_train_MLP = ss.fit_transform(X_train_MLP)
96 X_test_MLP = ss.transform(X_test_MLP)
97
98 #调用MLP实例化
99 MLP = MLPClassifier(hidden_layer_sizes=(13,13,13), max_iter=500)
100 MLP.fit(X_train_MLP, y_train_MLP)
101
102 global y_predict_MLP
103 y_predict_MLP = MLP.predict(X_test_MLP)
104
105 print("The accurary:", MLP.score(X_test_MLP, y_test_MLP))
106 print(classification_report(y_test_MLP, y_predict_MLP)) # , target_names=digits.target_names.astype(str)))
Python使用对比受欢迎原因之一正是代码的可读性相比较高,那段模型实例化的代码相比简单,二种机器学习模型的施用实例化的代码也足够近似。
Python使用比较受欢迎原因之一正是代码的可读性比较高,那段模型实例化的代码比较简单,两种机器学习模型的施用实例化的代码也丰盛好像。
3.3.3 测试结果
3.3.3 测试结果
在样本数 sample_num=50
的情形下,磨炼百分之七十五数码,用四分之一的数据即1三个样本实行测试。
在样本数 sample_num=50
的气象下,锻炼75%数码,用四分一的多寡即1二个样本举办测试。
二种模型的测试结果如图5所示,可知除了SVM达到84.7%的精度之外,其余都在60-百分之七十左右。
二种模型的测试结果如图5所示,可知除了SVM达到84.7%的精度之外,其他都在60-70%左右。
但是因为唯有肆拾多少个样本点,小样本的情状下测试精度的偶然性误差相比较大。
不过因为唯有四十七个样本点,小样本的景况下测试精度的偶然性误差相比大。
图5
手写体识其他质量分析(在样本数为50的景观下)
图5
手写体识其他品质分析(在样本数为50的景况下)
追加样本数到100,即生成了100张单个手写体图像,75张用来演习,25张用来测试。
追加样本数到100,即生成了100张单个手写体图像,75张用来练习,25张用来测试。
25张的测试结果图6所示,二种机器学习的模子的测试精度都落得了九成左右。
25张的测试结果图6所示,二种机器学习的模型的测试精度都落得了十分九左右。
图6
手写体识别的质量分析(在样本数为100的气象下)
图6
手写体识别的性能分析(在样本数为100的意况下)
图7 分歧样本数量下的多种模型的测试精度
图7 差别样本数量下的各类模型的测试精度
#转发或是使用图片代码请尊重小编劳动成果,注脚出处,感谢
#转载或是使用图片代码请爱护小编劳动成果,注脚出处,多谢