首页 > 其他 > 详细

使用Pandas,Numpy解析的MNIST数据

时间:2021-09-13 18:53:18      阅读:23      评论:0      收藏:0      [点我收藏+]

首先是参考网站:

然后就是具体咋写的

from typing import Tuple
import pandas as pd
import numpy as np
import sys
import struct


def read_label_in_idx1_ubyte(path: str) -> Tuple[int, int, pd.DataFrame]:
    file = open(path, mode="rb")
    magic_number, count = struct.unpack(">ii", file.read(8))
    labels = np.fromfile(file=file, dtype=np.uint8)
    labels = pd.DataFrame(labels)
    return magic_number, count, labels


def read_image_in_idx3_ubyte(path: str) -> Tuple[int, int, int, int, pd.DataFrame]:
    file = open(path, mode="rb")
    magic_number, count, rows, columns = struct.unpack(">iiii", file.read(16))
    images: np.array = np.fromfile(file=file, dtype=np.uint8)
    images = images.reshape(count, (rows*columns))
    images = pd.DataFrame(images)
    return magic_number,count,rows,columns,images


labels = read_label_in_idx1_ubyte(
    "MNIST/train-labels-idx1-ubyte/train-labels.idx1-ubyte")[-1]
image = read_image_in_idx3_ubyte(
    "MNIST/train-images-idx3-ubyte/train-images.idx3-ubyte")[-1]

说点坑,首先这个必须用二进制流打开,然后,是大端模式。

使用Pandas,Numpy解析的MNIST数据

原文:https://www.cnblogs.com/Lemon-GPU/p/15259853.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!