最近在看这本书看到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. 我们为什么要使用List和Set(List,Set详解)

    1.集合概述 类图 集合和数组的区别? 集合基本方法 集合特有的遍历方式? public static void main(String[] args) { //创建集合对象 Collection c ...

  2. 团队作业5——测试与发布(alpha阶段)

    Deadline: 2018-5-9 10:00PM,以提交至班级博客时间为准. 根据以下要求,完成对本团队项目的测试与发布. 测试 请根据团队项目中软件的需求文档.功能说明.系统设计和测试计划,写出 ...

  3. Linux的LiveCd与CD、DVD版

    https://blog.csdn.net/sun_168/article/details/6744401

  4. ipython安装( jupyter)

    生产环境:win10 64位 pip的版本不是最新的,输入命令 python -m pip install --upgrade pip 更新我们的pip,pip不是最新的也会导致安装不了ipython ...

  5. MySQL 性能调优之SQL

    原文:http://bbs.landingbj.com/t-0-245451-1.html 对于SQL的优化,我们主要提供调整执行计划.优化SQL的方法有:缩短访问的路径.尽早过滤数据.尽可能减少排序 ...

  6. # 【Python3练习题 007】 有一对兔子,从出生后第3个月起每个月都生一对兔子, # 小兔子长到第三个月后每个月又生一对兔子, # 假如兔子都不死,问每个月的兔子总数为多少?

    # 有一对兔子,从出生后第3个月起每个月都生一对兔子,# 小兔子长到第三个月后每个月又生一对兔子, # 假如兔子都不死,问每个月的兔子总数为多少?这题反正我自己是算不出来.网上说是经典的“斐波纳契数列 ...

  7. React Native之通知栏消息提示(android)

    React Native之通知栏消息提示(android) 一,需求分析与概述 1.1,推送作为手机应用的基本功能,是手机应用的重要部分,如果自己实现一套推送系统费时费力,所以大部分的应用都会选择使用 ...

  8. java设计模式:概述与GoF的23种设计模式

    软件设计模式的产生背景 设计模式这个术语最初并不是出现在软件设计中,而是被用于建筑领域的设计中. 1977 年,美国著名建筑大师.加利福尼亚大学伯克利分校环境结构中心主任克里斯托夫·亚历山大(Chri ...

  9. easyUI 数据表格datagrid的使用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. QueryRunner 错误

    QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource()); 写成了 QueryRunner qr = new QueryRunner(); 导 ...