【问题标题】:displaying 100 mnist dataset显示 100 个 mnist 数据集
【发布时间】:2021-08-14 09:03:42
【问题描述】:

这是我用来打印 100 mnist 数据的原始未简化图片的代码,但它不断给我一个错误。即使尝试了很多,我也找不到解决方案。征求意见

 from sklearn.datasets import fetch_openml
   mnist = fetch_openml('mnist_784')
   X = mnist["data"]
   y = mnist["target"]
   X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000],y[60000:]
   pca = PCA()
   pca.fit(X_train)
   cumsum = np.cumsum(pca.explained_variance_ratio_)
   d = np.argmax(cumsum >= 0.90) + 1

   #Setup a figure 8 inches by 8 inches
   fig = plt.figure(figsize=(8,8))
   fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
    for i in range(100):
        ax = fig.add_subplot(10, 10, i+1, xticks=[], yticks=[])
        ax.imshow(X_train[i].reshape(28,28), cmap=plt.cm.bone, interpolation='nearest')
        plt.show()

【问题讨论】:

  • 给我一个错误”对可能的受访者没有帮助。什么错误?请查看如何创建minimal reproducible example(并修复您的代码缩进)。

标签: python matplotlib scikit-learn mnist


【解决方案1】:

这就是你的情节显示语句仍在循环中的地方。只需将其移出循环,它就会显示正常。试试下面的内容;

from sklearn.datasets import fetch_openml
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt

mnist = fetch_openml('mnist_784')
X = mnist["data"]
y = mnist["target"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, 
 random_state=44)
pca = PCA()
pca.fit(X_train)

fig = plt.figure(figsize=(8,8))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(100):
    ax = fig.add_subplot(10, 10, i+1, xticks=[], yticks=[])
    ax.imshow(X_train[i].reshape(28,28), cmap=plt.cm.bone, interpolation='nearest')

plt.show()

【讨论】:

    【解决方案2】:
    import matplotlib.pyplot as plt
    
    fig,ax = plt.subplots(5, 10)
    
    for i in range(10):
        for j in range(10):
          ax[i,j].imshow(X_train[(10*i)+j].reshape(8, 8), cmap='binary')
    

    【讨论】:

      猜你喜欢
      • 2016-11-13
      • 2019-08-27
      • 1970-01-01
      • 2018-10-09
      • 2019-05-14
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多