kaggle-Digit Recognizer
- 安装kaggle工具获取数据源(linux 环境)
- 采用sklearn的KNeighborsClassifier训练数据
- 通过K折交叉验证来选取K值是正确率更高
1.安装kaggle,获取数据源
pip install kaggle
将数据下载到目录/data/data-test/digit_recognize/下
cd /data/data-test/digit_recognize/
kaggle competitions download -c digit-recognizer
2.安装anaconda3作为python3环境,自带sklearn,pandas,numpy等常用工具包
3.代码实现
import pandas as pd
from sklearn.model_selection import cross_val_score
from sklearn.neighbors import KNeighborsClassifier
import pickle
# 文件路径
project_path = '/data/data-test/digit_recognize/'
clf_file = project_path + 'knn.pickle'
def get_data_chunk(file_name):
# 文件太大分块读取文件 9000万条
reader = pd.read_csv(file_name, iterator=True)
loop = True
chunk_size = 100000
chunks = []
while loop:
try:
chunk = reader.get_chunk(chunk_size)
chunks.append(chunk)
print(len(chunks))
except StopIteration:
loop = False
print("Iteration is stopped.")
res = pd.concat(chunks, ignore_index=True)
return res
def save_clf(clf_s):
clf_f = open(clf_file, 'wb')
pickle.dump(clf_s, clf_f)
clf_f.close()
def get_clf():
clf_f = open(clf_file, 'rb')
res = pickle.load(clf_f)
return res
# 对测试数据集预测结果
def predict():
knn_clf = get_clf()
test_data = get_data_chunk(project_path + "test.csv")
res_data = knn_clf.predict(test_data)
df = pd.DataFrame()
df["imageId"] = test_data["imageId"]
df["Label"] = res_data
df.to_csv(project_path + 'res.csv', index=False)
def train():
train_data = get_data_chunk(project_path + "train.csv")
print(train_data.info())
print(train_data)
train_lable = train_data['label']
x = train_data.drop(columns=['label'])
max = 0
max_k = 5
# k取值从5,15用K折交叉验证算出正确率分数
for k in range(5, 15):
clf = KNeighborsClassifier(n_neighbors=k)
# cv为2折
scores = cross_val_score(clf, x, train_lable, cv=2, scoring='accuracy')
mean = scores.mean()
print(k, mean)
if mean > max:
max_k = k
print("maxK=", max_k)
# 用max_k作为knn参数训练模型
clf = KNeighborsClassifier(n_neighbors=max_k)
clf.fit(x, train_lable)
# 存储模型到pickle文件
save_clf(clf)
if __name__ == '__main__':
train()
predict()
kaggle-Digit Recognizer的更多相关文章
- Kaggle—Digit Recognizer竞赛
Digit Recognizer 手写体数字识别 MNIST数据集 本赛 train 42000样例 test 28000样例,原始MNIST是 train 60000 test 10000 我分别 ...
- kaggle实战记录 =>Digit Recognizer
date:2016-09-13 今天开始注册了kaggle,从digit recognizer开始学习, 由于是第一个案例对于整个流程目前我还不够了解,首先了解大神是怎么运行怎么构思,然后模仿.这样的 ...
- Kiggle:Digit Recognizer
题目链接:Kiggle:Digit Recognizer Each image is 28 pixels in height and 28 pixels in width, for a total o ...
- DeepLearning to digit recognizer in kaggle
DeepLearning to digit recongnizer in kaggle 近期在看deeplearning,于是就找了kaggle上字符识别进行练习.这里我主要用两种工具箱进行求解.并比 ...
- Kaggle入门(一)——Digit Recognizer
目录 0 前言 1 简介 2 数据准备 2.1 导入数据 2.2 检查空值 2.3 正则化 Normalization 2.4 更改数据维度 Reshape 2.5 标签编码 2.6 分割交叉验证集 ...
- Kaggle 项目之 Digit Recognizer
train.csv 和 test.csv 包含 1~9 的手写数字的灰度图片.每幅图片都是 28 个像素的高度和宽度,共 28*28=784 个像素点,每个像素值都在 0~255 之间. train. ...
- kaggle赛题Digit Recognizer:利用TensorFlow搭建神经网络(附上K邻近算法模型预测)
一.前言 kaggle上有传统的手写数字识别mnist的赛题,通过分类算法,将图片数据进行识别.mnist数据集里面,包含了42000张手写数字0到9的图片,每张图片为28*28=784的像素,所以整 ...
- 适合初学者的使用CNN的数字图像识别项目:Digit Recognizer with CNN for beginner
准备工作 数据集介绍 数据文件 train.csv 和 test.csv 包含从零到九的手绘数字的灰度图像. 每张图像高 28 像素,宽 28 像素,总共 784 像素.每个像素都有一个与之关联的像素 ...
- SMO序列最小最优化算法
SMO例子: 1 from numpy import * 2 import matplotlib 3 import matplotlib.pyplot as plt 4 5 def loadDataS ...
- How do I learn machine learning?
https://www.quora.com/How-do-I-learn-machine-learning-1?redirected_qid=6578644 How Can I Learn X? ...
随机推荐
- Android Studio 减小项目文件夹的大小和.gitignore文件配置
Build --> Clean Project 可以清理出很大一部分的空间 手动删除以下文件或者目录 Dir : ProjectFolder/buildDir : ProjectFolder/a ...
- 解决在jupyter notebook中遇到的ImportError: matplotlib is required for plotting问题
昨天学习pandas和matplotlib的过程中, 在jupyter notebook遇到ImportError: matplotlib is required for plotting错误, 以下 ...
- webapi 重复提交问题
https://izen.live/Blog/info/13.html /// <summary> /// action方法过滤器 /// </summary> public ...
- 微信H5开发
1,line-height 不垂直居中 解决方法:用padding写高度 2,手指滑动事件 touchmove 3,页面滚动事件:touchmove 4,要写不同手机适配的基数font-siz ...
- JMD Handy Baby 2 to Decode & Adding New BMW 525 ID46 Key
Here OBD2TOOL share the guide on how to use JMD Handy Baby II to decode and add new keys for BMW 525 ...
- 记一次Django报错Reverse for 'indextwo' with no arguments not found. 1 pattern(s) tried: ['$index/$']
启动python manage.py runserver 打开127.0.0.1:8000,报错信息如下: Reverse for 'indextwo' with no arguments not f ...
- ubuntu16.04 下安装 visual studio code 以及利用 g++ 运行 c++程序
参考链接:1. http://www.linuxidc.com/Linux/2016-07/132798.htm(安装vs code) 2.https://blog.csdn.net/qq_28598 ...
- Vue post提交
vue中的axios 是不直接支持post方法的,所以我们得绕一下路,我目前在登录的时候运用到了,服务器端用php,可以收到数据 let param = new URLSearchParams(); ...
- docker相关操作
docker 安装参照官网一步一步来,特别简单,主要是下载比较慢: docker 需要 管理员权限: docker 相关命令: 容器生命周期管理 — docker [run|start|stop|re ...
- 生成多个git ssh密钥
如果你已经有了一套名为 id_rsa 的公秘钥,将要生成另外一个公钥,比如 aysee ,你也可以使用任何你喜欢的名字. 步骤如下: 1.生成一个新的自定义名称的公钥: ssh-keygen -t r ...