最近在看这本书看到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. 实分析p78 两个解释

    1. 是为了存在一个充分大的J,使得,当j大于J.会满足.x是满足能一致收敛到f(x)自变量取得集合,, 是为了允许有限个 前面的不成立,是对所有的k都成立,让k取很大,可以很小 2.是函数列收敛到f ...

  2. PHP之CLI模式

    转载: http://www.cnblogs.com/zcy_soft/archive/2011/12/10/2283437.html 所有的PHP发行版,不论是编译自源代码的版本还是预创建的版本,都 ...

  3. [转帖]你所不知道的C和C++运行库

    [C-C++]你所不知道的C和C++运行库 https://blog.csdn.net/humanking7/article/details/85887884 原作者也是转的blog 最近一个物理机上 ...

  4. js实现input的赋值

    input框赋值如下所示,是一个文本框的html代码,实际开发中,要涉及到将数据库中的数据取出然后放入input框中. <input id="name1" name=&quo ...

  5. linux安装httpd,做文件服务器

    在一个团队或者公司层面上,做一个本地的文件服务器,将网上的资源下载到本地,是有必要的.这将节省其他人的很多下载时间. >>提君博客原创  http://www.cnblogs.com/ti ...

  6. Day 5-3 多态与多态性

    多态与多态性 鸭子类型 多态与多态性 多态:一类事物有多种形态.比如,动物有多种形态,人,狗,猪,豹子.水也有多种形态,冰,雪,水蒸气. #多态:同一类事物的多种形态 import abc class ...

  7. .Net MVC4 log4net的配置

    一.首先在使用log4net记录日志的时候,我们要引用log4net.dll文件 二.在web.config中添加一下配置代码 <configSections> <!-- For m ...

  8. Python 中关于 round 函数的小坑

    参考: http://www.runoob.com/w3cnote/python-round-func-note.html

  9. InnoDB: Error: Table "mysql"."innodb_table_stats" not found.

    问题:打开mysql错误日志时发现大量的如下错误 Error: Table "mysql"."innodb_table_stats" not found. In ...

  10. Linux在shell中进入python敲方向键出现「^[[C^[[D」的解决办法

    安装yum -y install readline-devel,然后在重新编译python