<jupyter_start><jupyter_text>バイナリデータの読み込み<jupyter_code>import struct
import numpy as np
import matplotlib.pyplot as plt
filename = 'mnist/t10k-images-idx3-ubyte'
fp = open(filename, 'rb')
magic = struct.unpack('>i', fp.read(4))
print('magic number is "%d"' % (magic))
n_images, height, width = struct.unpack('>iii', fp.read(4 * 3))
print('#image: %d' % (n_images))
print('size: %d x %d' % (width, height))
n_pixels = width * height
pixels = struct.unpack('>' + 'B' * n_pixels, fp.read(1 * n_pixels))
pixels = np.asarray(pixels, dtype='uint8')
pixels = pixels.reshape((height, width))<jupyter_output><empty_output><jupyter_text>データの表示<jupyter_code>plt.imshow(pixels)
plt.show()
plt.imshow(pixels, cmap='gray')
plt.show()
images = []
for i in range(100):
    pixels = struct.unpack('>' + 'B' * n_pixels, fp.read(1 * n_pixels))
    pixels = np.asarray(pixels, dtype='uint8')
    pixels = pixels.reshape((height, width))
    images.append(pixels)
# Matplotlibを使ったタイリング
fig = plt.figure(figsize=(10, 10))

for i in range(10):
    for j in range(10):
        index = i * 10 + j
        image = images[index]

        # 10x10のタイルのindex番目のaxisという意味
        ax = fig.add_subplot(10, 10, index + 1)
        ax.imshow(image, cmap='gray')
        ax.axis('off')

plt.show()
# もう少し気の利いたタイリング

# 全ての画像にマージンをつける
margin = 1
pad_images = [np.pad(image, (margin, margin), mode='constant', constant_values=255) for image in images]

# 画像の列をNumPyの配列に変えてreshape
pad_images = np.asarray(pad_images).reshape((10, 10, height + margin * 2, width + margin * 2))

# 縦横の列を結合する
image_grid = np.transpose(pad_images, axes=(0, 2, 1, 3))
image_grid = np.reshape(image_grid, (10 * (height + margin * 2), 10 * (width + margin * 2)))

# 表示
plt.figure(figsize=(10, 10))
plt.imshow(image_grid, cmap='gray')
plt.axis('off')
plt.tight_layout()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16964769647696476, 'fraction_numerical': 0.03035230352303523, 'mean_word_length': 4.823343848580442, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '495830', 'license': 'MIT', 'n_tokens_mistral': 831, 'n_tokens_neox': 782, 'n_words': 152, 'path': '_programs/python/logistic/read_binary.ipynb', 'repo_name': 'tatsy/cpp-python-beginners', 'size': '1845'}