我正在使用MNIST数字数据进行一些练习,但是当我尝试对其进行可视化时会失败。该练习来自一本书BTW。所以我导入数据集
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
mnist.data.shape
然后我只绘制部分数据
fig, ax = plt.subplots(6, 8, subplot_kw=dict(xticks=[], yticks=[]))
for i, axi in enumerate(ax.flat):
axi.imshow(mnist.data[1250 * i].reshape(28, 28), cmap='gray_r')
然后我对数据的1/30进行分析
# use only 1/30 of the data: full dataset takes a long time!
data = mnist.data[::30]
target = mnist.target[::30]
model = Isomap(n_components=2)
proj = model.fit_transform(data)
plt.scatter(proj[:, 0], proj[:, 1], c=target.astype(int),
cmap=plt.cm.get_cmap('jet', 10)) # need to convert target into int
plt.colorbar(ticks=range(10))
plt.clim(-0.5, 9.5);
我只对数据集中的1感兴趣,我想看看这些,这是我得到的错误。这是我的跑步
from sklearn.manifold import Isomap
# Choose 1/4 of the "1" digits to project
data = mnist.data[mnist.target == 1][::4]
fig, ax = plt.subplots(figsize=(10, 10))
model = Isomap(n_neighbors=5, n_components=2, eigen_solver='dense')
plot_components(data, model, images=data.reshape((-1, 28, 28)),
ax=ax, thumb_frac=0.05, cmap='gray_r')
这将导致
ValueError: Found array with 0 sample(s) (shape=(0, 784)) while a minimum of 1 is required.
我不明白为什么数组为空?
答案 0 :(得分:3)
mnist数据的目标值是字符串,而不是整数。
只需更改此行:
data = mnist.data[mnist.target == 1][::4]
收件人:
data = mnist.data[mnist.target == '1'][::4]