最近在看这本书看到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. spring实例入门

    首先是bean文件: package onlyfun.caterpillar; public class HelloBean {    private String helloWord = " ...

  2. Docker -d : Running modprobe bridge nf_nat failed with message: exit status 1

    nf_nat 是做什么用的 - DockOne.iohttp://dockone.io/question/1384 docker-py的配置与使用 - openxxs - 博客园http://www. ...

  3. ShowDoc上手

    ShowDoc是什么 每当接手一个他人开发好的模块或者项目,看着那些没有写注释的代码,我们都无比抓狂.文档呢?!文档呢?!Show me the doc !! 程序员都很希望别人能写技术文档,而自己却 ...

  4. MySQL基础配置之mysql的默认字符编码的设置(my.ini设置字符编码) - 转载

    MySQL基础配置之mysql的默认字符编码的设置(my.ini设置字符编码) MySQL的默认编码是Latin1,不支持中文,那么如何修改MySQL的默认编码呢,下面以设置UTF-8为例来说明. 需 ...

  5. MySQL unknown variable 'default-character-set=utf8'的解决

    Windows07 安装了MySQL-server-5.5,直接在命令行输入net start mysql ,启动mysql成功, 然后修改/MySQL Server 5.5/my.ini,增加了de ...

  6. [转帖]SAP一句话入门:Plant Maintenance

    SAP一句话入门:Plant Maintenance http://blog.vsharing.com/MilesForce/A618273.html PM就是Plant Maintenance(本文 ...

  7. apache benchmark 的简单安装与测试

    1. 下载apache benchmark Copy From https://blog.csdn.net/fyqaccpt96/article/details/43272001 yum instal ...

  8. 47.Majority Element I & II

    Majority Element I 描述 给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一. You may assume that the array is non ...

  9. Baby-Step-Giant-Step 很酷的算法

    Baby-Step-Giant-Step BSGS算法用于解决形如:      A  ^  x  ≡  B  (  mod  C  ) 的问题.  学这个算法前需要具备以下知识:快速幂取模.扩展欧几里 ...

  10. CRM系统数据授权

    1.新建角色,华东二区 2.业务对象中找到客户管理 3.在数据范围中新建数据规则,并进行设置 4.点击授权后,生效. 另:数据权限设置