python数字识别kaggle论文_kaggle入门 手写数字识别

2a41c219828c293aa3724ad08ff9be830a3.jpg

The data files train.csv and test.csv contain gray-scale images of hand-drawn digits, from zero through nine.

Each image is 28 pixels in height and 28 pixels in width, for a total of 784 pixels in total. Each pixel has a single pixel-value associated with it, indicating the lightness or darkness of that pixel, with higher numbers meaning darker. This pixel-value is an integer between 0 and 255, inclusive.

The training data set, (train.csv), has 785 columns. The first column, called "label", is the digit that was drawn by the user. The rest of the columns contain the pixel-values of the associated image.

Each pixel column in the training set has a name like pixelx, where x is an integer between 0 and 783, inclusive. To locate this pixel on the image, suppose that we have decomposed x as x = i * 28 + j, where i and j are integers between 0 and 27, inclusive. Then pixelx is located on row i and column j of a 28 x 28 matrix, (indexing by zero).

For example, pixel31 indicates the pixel that is in the fourth column from the left, and the second row from the top, as in the ascii-diagram below.

Visually, if we omit the "pixel" prefix, the pixels make up the image like this:

000 001 002 003 ... 026 027

028 029 030 031 ... 054 055

056 057 058 059 ... 082 083

| | | | ... | |

728 729 730 731 ... 754 755

756 757 758 759 ... 782 783

The test data set, (test.csv), is the same as the training set, except that it does not contain the "label" column.

Your submission file should be in the following format: For each of the 28000 images in the test set, output a single line containing the ImageId and the digit you predict. For example, if you predict that the first image is of a 3, the second image is of a 7, and the third image is of a 8, then your submission file would look like:

ImageId,Label

1,3

2,7

3,8

(27997 more lines)

The evaluation metric for this contest is the categorization accuracy, or the proportion of test images that are correctly classified. For example, a categorization accuracy of 0.97 indicates that you have correctly classified all but 3% of the images.

训练集是有42001*785的大小组成的,第一行名称除去 ,第一列是训练标签,第二至最后一列是训练数据。测试数据有28000个。

可以使用随机森林来实现。

速度的话还行,一两分钟就可以训练出来。

正确率是0.96657

#-*- coding:utf-8 -*-

'''

@auther: Starry

@file: version3.py

@time: 2018/2/12 10:09

'''

from sklearn.ensemble import RandomForestClassifier

import numpy as np

import pandas as pd

import csv

def loadData():

train = pd.read_csv('data/train.csv')

test = pd.read_csv('data/test.csv')

trainData = train.drop(['label'],axis=1).values.astype(dtype=np.int64)

trainLabel = np.array(train['label'])

testData = test.values

print('load Data finish!!!')

return trainData, trainLabel, testData

def saveResult(testLabel, fileName):

header = ['ImageID', 'Label']

with open(fileName, 'w', newline='') as csvFile:

writer = csv.writer(csvFile, delimiter=',')

writer.writerow(header)

for i, p in enumerate(testLabel):

writer.writerow([str(i + 1), str(p)])

def RFClassify(trainData, trainLabel, testData):

nbCF = RandomForestClassifier(n_estimators=256, warm_start=True)

nbCF.fit(trainData, np.ravel(trainLabel))

testLabel = nbCF.predict(testData)

saveResult(testLabel, 'output/output5.csv')

print('finish!!!')

RFClassify(*loadData())

参数优化

参数n_estimators我填的是256,但不一定非要填这个,那么填多少好呢?

通过交叉验证可以估计个大概来。

def cross_va(train_X,train_y):

n_estimators = [100,120,140,160,180,200]

test_scores = []

for n_est in n_estimators:

print('n_estimators is %s now.'% n_est)

clf = RandomForestClassifier(n_estimators=n_est,warm_start=True)

test_score = np.sqrt(-cross_val_score(clf,train_X,train_y,cv=5,scoring='neg_mean_squared_error'))

print(test_score)

test_scores.append(np.mean(test_score))

print(test_scores)

plt.plot(n_estimators,test_scores)

plt.title('n_estimator vs CV Error')

plt.show()

由于每一次算的很慢 我就先在[100,150,200,250]算一下,看哪个更好。

得到下面这张图:

5cc71d68d76c98338a69783d23ce0c0c803.jpg

大概在100-200之间,所以又算了[100,120,140,160,180,200]

得到下图:

fc9b2981d80844627bc38c42c7e58887cbf.jpg

大概在180左右。这也只是个估计,并不是一定的。

在用180提交时得到0.96628的准确率,比256还低。所以又用185提交了,是0.96700,提高了一点点。

用190提交时又减少了,是0.96628,所以参数n_estimators差不多用185更好些。准确率在0.96700。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值