最近在看这本书看到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/

很多人点进去就头大,看到四个可下载的文件不知道怎么用(包括我),不过为了解决这个问题我就耐心读了下页面(心情简单)

     

这两张图要放一起看,特别是划红线的部分,我们可以确定一下几个事实:

  1. 每个dimension 4-byte Integers,对应到struct模块里面的fmt格式就是'I'
  2. high endian也就是大端法读进来,至于什么是大端法我想大家可以去wiki看看ヽ( ̄▽ ̄)ノ
  3. 右图的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数据集错误及解决方案的更多相关文章

  1. 集成算法(chapter 7 - Hands on machine learning with scikit learn and tensorflow)

    Voting classifier 多种分类器分别训练,然后分别对输入(新数据)预测/分类,各个分类器的结果视为投票,投出最终结果: 训练: 投票: 为什么三个臭皮匠顶一个诸葛亮.通过大数定律直观地解 ...

  2. 第25月第5天 Hands-on Machine Learning with Scikit-Learn and TensorFlow

    1.apachecn视频(机器学习实战) https://github.com/apachecn/AiLearning https://space.bilibili.com/97678687/#/ch ...

  3. Tensorflow MNIST 数据集测试代码入门

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50614444 测试代码已上传至GitH ...

  4. Tensorflow MNIST 数据集測试代码入门

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50614444 測试代码已上传至GitH ...

  5. Hands on Machine Learning with Sklearn and TensorFlow学习笔记——机器学习概览

    一.什么是机器学习? 计算机程序利用经验E(训练数据)学习任务T(要做什么,即目标),性能是P(性能指标),如果针对任务T的性能P随着经验E不断增长,成为机器学习.[这是汤姆米切尔在1997年定义] ...

  6. Hands on Machine Learning with sklearn and TensorFlow —— 一个完整的机器学习项目(加州房地产)

    数据集地址:https://github.com/ageron/handson-ml/tree/master/datasets 先行知识准备:NumPy,Pandas,Matplotlib的模块使用 ...

  7. 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? ...

  8. 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 ...

  9. machine learning----->谷歌Cloud Machine Learning平台

    1.谷歌Cloud Machine Learning平台简介: 机器学习的三要素是数据源.计算资源和模型.谷歌在这三个方面都有强大的支撑:谷歌不仅有种类丰富且数量庞大的数据资源,而且有强大的计算机群提 ...

随机推荐

  1. 几个jdbc小技巧

    版本:jdk:1.6mysql-connector-5.0.8 1.如何用jdbc判断某个基本表是否存在 一种比较“笨”的方法:    try{         stmt.executeQuery( ...

  2. Docker Compose vs. Dockerfile

    Docker Compose vs. Dockerfile - which is better? - Stack Overflowhttps://stackoverflow.com/questions ...

  3. C# foreach内部原理

    我们知道使用foreach的一个要求是对象必须继承自IEnumerable接口 这样才可以进行迭代 那内部是怎么实现的呢 这个时候会将对应的foreach语句转换为一个while循环 并且通过Move ...

  4. ascii、unicode、utf-8、gbk 区别

    原文:https://blog.csdn.net/u010262331/article/details/46013905 ASCII:遇上0×10, 终端就换行: 遇上0×07, 终端就向人们嘟嘟叫: ...

  5. SQLServer2016 之后增加了索引列数的限制 从 16个列 增加到了 32个列

    创建带有包含列的索引 https://docs.microsoft.com/zh-cn/sql/relational-databases/indexes/create-indexes-with-inc ...

  6. PL/SQL编程--变量声明及赋值

    declare v_price ,);--单价 v_usenum number;--水费字数 v_usenum2 number;--使用吨数 begin v_price:=2.45;--每吨单价 v_ ...

  7. 用junit对java代码进行测试,需要注意

    1.用@Test注解的方法必须没有返回值,返回值类型无:void 2.用@Test注解的方法必须没有参数.

  8. saltstack二

    配置管理 haproxy的安装部署 haproxy各版本安装包下载路径https://www.haproxy.org/download/1.6/src/,跳转地址为http,改为https即可 创建相 ...

  9. SpringBoot之显示本地图片范例

    controller // 扫描指定目录下的图片进行展示 @RequestMapping("/showPics") public ModelAndView showPics(Mod ...

  10. SQL Server中获取指定时间段内的所有月份

    例如查询 2012-1-5 到 2012-11-3 之间所有的月份 declare @begin datetime,@end datetime set @begin='2012-1-5' set @e ...