《Hands-On Machine Learning with Scikit-Learn&TensorFlow》mnist数据集错误及解决方案
最近在看这本书看到Chapter 3.Classification,是关于mnist数据集的分类,里面有个代码是
from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('MNIST original')
mnist
我十分郁闷,因为这个根本加载不出来-_-||,报了个OSError,改了data_home之后也有error,然后我按照网上的方法改data_home也没用,弄了很久最后决定自己弄这个数据集出来(气死了)
百度搜索mnist第一个出来的就是http://yann.lecun.com/exdb/mnist/
很多人点进去就头大,看到四个可下载的文件不知道怎么用(包括我),不过为了解决这个问题我就耐心读了下页面(心情简单)

这两张图要放一起看,特别是划红线的部分,我们可以确定一下几个事实:
- 每个dimension是 4-byte Integers,对应到struct模块里面的fmt格式就是'I'
- high endian也就是大端法读进来,至于什么是大端法我想大家可以去wiki看看ヽ( ̄▽ ̄)ノ
- 右图的dimension 0就是左边的magic number,接下里的dimension 1就是number of images,如此类推应该就会看了吧emmmmm
补充个链接:python struct模块:https://docs.python.org/2/library/struct.html
下面是代码:
import struct
import gzip
import numpy as np
import matplotlib.pyplot as plt
import matplotlib def getImage(file):
with gzip.open(file) as f:
buffer = f.read()
magicNumber, images, rows, columns = struct.unpack_from('>IIII',buffer)
index = 0
index += struct.calcsize('>IIII') #struct.calcsize(fmt)返回这个结构的长度
pattern = '>' + str(images*rows*columns) + 'B' #这里计算了文件的长度,'B'表示为1位无符号字符(unsigned char)
data = struct.unpack_from(pattern,buffer,index) #从index指定的位置开始读
return np.array(data).reshape(images, rows, columns) #因为一个图片是28*28pixel,这里需要reshape
def getLabel(file):
with gzip.open(file) as f:
buffer = f.read()
magicNumber, labels = struct.unpack_from('>II',buffer)
index = 0
index += struct.calcsize('>II')
pattern = '>' + str(labels) + 'B' #这里计算了文件的长度,'B'表示为1位无符号字符(unsigned char)
data = struct.unpack_from(pattern,buffer,index) #从index指定的位置开始读
return np.array(data) #这里label就是一个array不需要reshape
if __name__ =='__main__':
x_train_data = getImage("train-images-idx3-ubyte.gz")
y_train_data = getLabel("train-labels-idx1-ubyte.gz")
x_test_data = getImage("t10k-images-idx3-ubyte.gz")
y_test_data = getLabel("t10k-labels-idx1-ubyte.gz") '''以下为测试模块'''
print(x_train_data.shape)
print(y_train_data.shape)
print(x_test_data.shape)
print(y_test_data.shape)
x = x_train_data[150]
plt.imshow(x,cmap=matplotlib.cm.binary,interpolation="nearest")
plt.axis()
plt.show()
ps.难以置信我弄好这个后,我不死心试着去运行了书里的代码,竟然自己好了,心情如下:

如需转载请注明出处
喜欢请支持下~
《Hands-On Machine Learning with Scikit-Learn&TensorFlow》mnist数据集错误及解决方案的更多相关文章
- 集成算法(chapter 7 - Hands on machine learning with scikit learn and tensorflow)
Voting classifier 多种分类器分别训练,然后分别对输入(新数据)预测/分类,各个分类器的结果视为投票,投出最终结果: 训练: 投票: 为什么三个臭皮匠顶一个诸葛亮.通过大数定律直观地解 ...
- 第25月第5天 Hands-on Machine Learning with Scikit-Learn and TensorFlow
1.apachecn视频(机器学习实战) https://github.com/apachecn/AiLearning https://space.bilibili.com/97678687/#/ch ...
- Tensorflow MNIST 数据集测试代码入门
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50614444 测试代码已上传至GitH ...
- Tensorflow MNIST 数据集測试代码入门
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50614444 測试代码已上传至GitH ...
- Hands on Machine Learning with Sklearn and TensorFlow学习笔记——机器学习概览
一.什么是机器学习? 计算机程序利用经验E(训练数据)学习任务T(要做什么,即目标),性能是P(性能指标),如果针对任务T的性能P随着经验E不断增长,成为机器学习.[这是汤姆米切尔在1997年定义] ...
- Hands on Machine Learning with sklearn and TensorFlow —— 一个完整的机器学习项目(加州房地产)
数据集地址:https://github.com/ageron/handson-ml/tree/master/datasets 先行知识准备:NumPy,Pandas,Matplotlib的模块使用 ...
- 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? ...
- Google's Machine Learning Crash Course #01# Introducing ML & Framing & Fundamental terminology
INDEX Introducing ML Framing Fundamental machine learning terminology Introducing ML What you learn ...
- machine learning----->谷歌Cloud Machine Learning平台
1.谷歌Cloud Machine Learning平台简介: 机器学习的三要素是数据源.计算资源和模型.谷歌在这三个方面都有强大的支撑:谷歌不仅有种类丰富且数量庞大的数据资源,而且有强大的计算机群提 ...
随机推荐
- XT535
今天金山误删了一个文件,把手机系统整坏了,故刷了个机,刷机教程: http://bbs.dospy.com/thread-15027415-1-623-1.html 中间安装了个驱动精灵,否则手机开启 ...
- 转:VIM选择文本块/复制/粘贴
VIM选择文本块/复制/粘贴 - lcj_cjfykx的专栏 - CSDN博客https://blog.csdn.net/lcj_cjfykx/article/details/9091569
- Html5使用canvas作图线宽很粗
自己使用canvas画图是碰到的问题,在这里记录一下.我把lineWidth设置为1,但是很粗,而且发虚.代码如下: <script type="text/javascript&quo ...
- Js中instanceof 的用法
在 JavaScript 中,判断一个变量的类型尝尝会用 typeof 运算符,在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 “object”. ...
- C# Note29: Close()和Dispose()的区别
待更! 深入解析Close()和Dispose()的区别
- 剑指Offer(9)
题目: 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 解法: 要考虑到底数为0,指数为负数的情况,这道题主要考的是对边界值的思考. p ...
- Spring boot + mybatis + orcale实战(干货)
废话少说,直接上步骤: 第一步:安装好IDEA(此处省略) 第二步:在IDEA新建springboot工程 第三步:在springboot工程的pom.xml添加oracle和mybait依赖 < ...
- Spring boot+ logback环境下,日志存放路径未定义的问题
日志路径未定义 环境:Spring boot + logback 配置文件: <configuration> <springProfile name="dev"& ...
- SSM框架整合系列——第一步
环境: JDK8 idea2018.2 maven3.5 spring和springMVC是天然集成,所以只需要解决mybatis和spring的整合问题,重点整合mybatis和spring的两个东 ...
- SQL之CASE WHEN用法详解[1]
简单CASE WHEN函数: CASE SCORE WHEN 'A' THEN '优' ELSE '不及格' END CASE SCORE WHEN 'B' THEN '良' ELSE '不及格' E ...