机器学习1——手写数字识别

简介

kaggle上发起的一个“Digit Recognizer”手写数字识别竞赛。链接:kaggle手写数字识别竞赛

1.数据介绍

  1. 数据文件train.csv和test.csv包含手绘数字的灰度图像,从0到9。
  2. 每张图像高28像素,宽28像素,总共784像素。每个像素都有一个与之相关联的像素值,表示该像素的明度或暗度,数字越大表示越暗。这个像素值是0到255之间的整数(包括255)。
  3. 训练数据集(train.csv)有785列。第一列称为“label”,是用户绘制的数字。其余的列包含关联图像的像素值。训练集中的每个像素列都有一个类似pixel x的名称,其中x是0到783之间的整数(包括0和783)。为了在图像上定位这个像素,假设我们将x分解为x = i * 28 + j,其中i和j都是0到27之间的整数,包括0和27。然后,pixel x位于一个28 x 28矩阵的第i行和第j列,(索引为0)。
    构成的图片大概这种格式:

在这里插入图片描述

  1. 测试数据集(test.csv)除了不包含label列外,与训练集相同。
  2. 对于测试集中的28000张图像,输出包含ImageId和预测数字的单行。最后作为最后提交的submission。这个方法最终获得了0.97003的评分。在这里插入图片描述

2.代码

库的引入

import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import metrics
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import precision_score
from sklearn.model_selection import train_test_split

导入数据

train = pd.read_csv("train1.csv")
test = pd.read_csv("test1.csv")

数据集可自行下载:数据集下载

数据初探

train.shape
train.head()

test.shape
test.head()

numbers = train['label']
numbers.head(10)

train=train.drop('label', axis=1)
train.head()
fre = numbers.value_counts()
fre.sort_index(inplace=True)

for x, y in enumerate(fre.values):
    plt.text(x-0.4, y, "%s" %y)
    
plt.title("Frequency Histogram of Numbers in Training Data")
plt.xlabel("Number")
plt.ylabel("Frequency")
fre.plot.bar()
plt.show()

在这里插入图片描述

绘制部分训练集图片

def draw():
    for i in range(36):
        plt.subplot(6,6,i+1)
        plt.imshow(train.ix[i].values.reshape(28, 28))
    plt.show()
draw()

在这里插入图片描述

训练

train = train/255
test = test/255

X_train, X_num, y_train, y_num = train_test_split(train, numbers, test_size = 0.2)

mlp = MLPClassifier()

mlp.fit(X_train,y_train)

y_test_predict=mlp.predict(X_num)
print(y_num)

y_test=y_num.values
print(y_test)

print(y_test_predict)

准确率

print(mlp, metrics.classification_report(y_num, y_test_predict))

p = precision_score(y_test,y_test_predict,average=None) 
print("accuracy:",p)

print('accurcy :',metrics.accuracy_score(y_num, y_test_predict))

项目提交

test_prediction=mlp.predict(test)
results = pd.Series(test_prediction,name="Label")
image_ids=pd.Series(range(1,28001),name = "ImageId")
My_submission = pd.concat([image_ids,results],axis = 1)
My_submission.to_csv("submission.csv",index=False)
My_submission.head()

在这里插入图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

调参侠鱼尾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值