簡體   English   中英

MNIST 的平均圖像

[英]Average image of MNIST

使用 MNIST 數據集,我試圖找到每個不同數字 (0-9) 的平均圖像。 以下代碼為我提供了數據集中的每個不同圖像,但我不確定如何獲得每個類的平均值 (0-9)

data = io.loadmat('mnist-original.mat')

x, y = data['data'].T, data['label'].T

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5)


a=np.unique(y, return_index=True)
b = a[1]

plt.figure(figsize=(15,4.5))
for i in b:
    img=x[i][:].reshape(28,28)
    plt.imshow(img)
    plt.show()  

假設零的“平均”圖像是標簽 = 0 的所有訓練數據的平均值。例如:

avgImg = np.average(x_train[y_train==0],0)

我認為這就是你想要的:

import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=(10,3))
for i in range(10):
    avgImg = np.average(x_train[y_train==i],0)
    plt.subplot(2, 5, i+1)
    plt.imshow(avgImg.reshape((16,16))) 
    plt.axis('off')

numpy_indexed包(免責聲明:我是它的作者)以矢量化的方式提供這種類型的功能:

import numpy_indexed as npi
digits, means = npi.group_by(y).mean(x)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM