使用sklearn对MNIST数据集进行手写数字识别

我想使用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] --> 这两行代码会抛出错误

回答:

请检查type(x_train)是否为numpy.ndarray或DataFrame。自Scikit-Learn 0.24起,fetch_openml()默认返回一个Pandas DataFrame。如果是DataFrame,那么你不能使用x_train[shuffled],这是为数组设计的。请改用x_train.iloc[shuffled]

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注