Reputation: 75
I am doing some exercises with MNIST digits data but it fails when I try to visualize it. The exercise is from a book BTW. So I import the the dataset
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
mnist.data.shape
then I just plot part of the data
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')
then I perform my analysis on 1/30th of the data
# 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);
I am only interested in the 1 from the dataset and I want to see those and this were I get the error. Here is what I run
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')
this results in a
ValueError: Found array with 0 sample(s) (shape=(0, 784)) while a minimum of 1 is required.
I don't understand why the array is empty?
Upvotes: 2
Views: 264
Reputation: 837
Target values for mnist data are strings and not integers.
Just change this line:
data = mnist.data[mnist.target == 1][::4]
to:
data = mnist.data[mnist.target == '1'][::4]
Upvotes: 3