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? ...
随机推荐
- init.d目录下的文件定义
init.d目录下存放的一些脚本一般是linux系统设定的一些服务的启动脚本. 系统在安装时装了好多服务,这里面就有很多对应的脚本. 执行这些脚本可以用来启动,停止,重启这些服务. 1.这些链接文件前 ...
- 探索未知种族之osg类生物---呼吸分解之更新循环三
补充 当然细心的你会发现,_scene->updateSceneGraph(*_updateVisitor)中还有一个imagePager::UpdateSceneGraph()还没有进行讲解, ...
- Python 多个分隔符 读取逗号和空格分开的数据
str.split() 清除默认 空格和tab 对空格数量不敏感 str.split(' ') 只清除一个空格 对空格数量敏感 l = re.split('[^0-9.]+',s.stri ...
- SLICK基础
1.sbt添加依赖 "com.typesafe.slick" %% "slick" % "3.2.3", "org.slf4j&q ...
- Android手机上浏览器不支持带端口号wss解决方案
首先抄个示例过来,命名为wss-test.html,然后传到服务器: <!DOCTYPE HTML> <html> <head> <meta http-equ ...
- 腾讯云的基本配置(centos 7.1)及mysql的使用
因为想在微信上开发些东西,所以租用了一个月的腾讯云. 推荐选择的镜像是centos7.1.这个系统的选择和本地操作系统基本没有关系. 首先要登录到云主机中,用户名是root,密码是当初自己设置的那一个 ...
- centos中单进程监控
[root@k8s6 proc]# ps aux|grep -v PID|sort -rn -k +|head - root ? Ssl : : /usr/bin/dockerd root ? Ssl ...
- c++类对象的内存分布
要想知道c++类对象的内存布局, 可以有多种方式,比如: 1)输出成员变量的偏移, 通过offsetof宏来得到 2)通过调试器查看, 比如常用的VS 1.没有数据成员的对象 class A{ }; ...
- mysql 在原有的时间上加10个月或者一年
UPDATE SERVER_TIME_LEFT SET END_TIME = DATE_ADD(END_TIME, INTERVAL 10 MONTH) WHERE SHOP_ID BETWEEN 1 ...
- python中global和nonlocal用法的详细说明
一.global 1.global关键字用来在函数或其他局部作用域中使用全局变量.但是如果不修改全局变量也可以不使用global关键字. gcount = 0 def global_test(): ...