【问题标题】:Handwritten Digit Recognition on MNIST dataset using sklearn使用 sklearn 对 MNIST 数据集进行手写数字识别
【发布时间】:2021-10-05 22:18:31
【问题描述】:

我想使用 sklearn 在 MNIST 数据集上构建一个手写数字识别,并且我想针对特征 (x) 和标签 (y) 对我的训练集进行洗牌。但它显示了一个 KeyError。让我知道正确的方法是什么。

    from sklearn.datasets import fetch_openml
    mnist = fetch_openml('mnist_784')
    x,y=mnist['data'],mnist['target']
    x.shape
    y.shape
    import matplotlib
    import matplotlib.pyplot as plt
    import numpy as np
    digit = np.array(x.iloc[45])
    digit_img = digit.reshape(28,28)
    plt.imshow(digit_img,cmap=matplotlib.cm.binary , interpolation="nearest")
    plt.axis("off")
    y.iloc[45]
    x_train, x_test = x[:60000],x[60000:]
    y_train, y_test=y[:60000],y[60000:]
    import numpy as np
    shuffled = np.random.permutation(60000)
    x_train=x_train[shuffled] -->
    y_train = y_train[shuffled] --> these two lines are throwing error

【问题讨论】:

  • 你能过去完整的堆栈跟踪以及你正在使用的 sklearn 版本吗?

标签: numpy machine-learning scikit-learn mnist numpy-random


【解决方案1】:

请检查type(x_train) 是 numpy.ndarray 还是 DataFrame。 由于 Scikit-Learn 0.24,fetch_openml() 默认返回 Pandas DataFrame。 如果是数据框,则不能使用x_train[shuffled],它适用于数组。 而是使用x_train.iloc[shuffled]

【讨论】:

    猜你喜欢
    • 2017-09-01
    • 2018-07-04
    • 2013-08-09
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 2015-04-29
    • 2019-06-25
    • 1970-01-01
    相关资源
    最近更新 更多