其他分享
首页 > 其他分享> > 使用Pandas,Numpy解析的MNIST数据

使用Pandas,Numpy解析的MNIST数据

作者:互联网

首先是参考网站:

然后就是具体咋写的

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]

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

标签:read,labels,int,file,images,ubyte,Numpy,Pandas,MNIST
来源: https://www.cnblogs.com/Lemon-GPU/p/15259853.html