采樣隨機 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.astype('float64')
fig, ax = plt.subplots(2, 5)
ax = ax.flatten()
for i in range(10):
im_idx = np.argwhere(y == str(i))[0] #panda versions bug, worked in pycharm. {kindly tell me an alternate way to fix it}
print(im_idx)
plottable_image = np.reshape(X[im_idx], (28, 28))
ax[i].imshow(plottable_image, cmap='gray_r')
錯誤顯示
ValueError: Length of passed values is 1, index implies 70000.
uj5u.com熱心網友回復:
錯誤你得到的結果,因為np.argwhere(y == i)回傳一個空陣列,這是因為你正試圖使之間的比較y,其充滿string價值觀和i這是一個int。
以下更改將修復它:
im_idx = np.argwhere(y == str(i))[0]
編輯:
這是更新的演員表以匹配numpy操作:
y = mnist.target.to_numpy()
X = mnist.data.astype('float64').to_numpy()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/357821.html
