簡體   English   中英

SKlearn 數據集,Mnist 隨機樣本繪圖不起作用? 找不到錯誤

[英]SKlearn dataset , Mnist random sample plotting is not working? cannot find error

采樣隨機 mnist 數據集,但顯示錯誤,告訴我我哪里做錯了?

import sklearn
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import fetch_openml

mnist = fetch_openml('mnist_784')
y = mnist.target
X = mnist.data

fig, ax = plt.subplots(2, 5)
ax = ax.flatten()
for i in range(10):
    im_idx = np.argwhere(y == i)[0]
    print(im_idx)
    plottable_image = np.reshape(X[im_idx], (28, 28))
    ax[i].imshow(plottable_image, cmap='gray_r')
    

您得到結果的錯誤是因為np.argwhere(y == i)返回一個空數組,那是因為您試圖在填充string值的y和作為int i之間進行比較。

以下更改將修復它:

im_idx = np.argwhere(y == str(i))[0]

暫無
暫無

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

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