首页 > 代码库 > python读取mnist label数据库

python读取mnist label数据库

<br>[offset] [type]          [value]          [description]
0000     32 bit integer  0x00000803(2051) magic number
0004     32 bit integer  60000            number of items

0008     unsigned byte   ??               label
0009     unsigned byte   ??               label
........
xxxx     unsigned byte   ??               label

 Mnist label数据结构如上.

完整代码:

import struct

filename = ‘/home/xxxx/Downloads/train-labels.idx1-ubyte‘
binFile = open(filename, ‘rb‘)
buf = binFile.read()

index = 0
magic, train_label_num = struct.unpack_from(‘>II‘, buf, index)
print(magic)
print(train_label_num)

index += struct.calcsize(‘>II‘)

train_label_lis=[]

for i in range(train_label_num):
    label_item = int(struct.unpack_from(‘>B‘, buf, index)[0])
    train_label_lis.append(label_item)
    index += struct.calcsize(‘B‘)


print(train_label_lis[0])

 

python读取mnist label数据库