[英]Handwritten Digit Recognition on MNIST dataset using sklearn
I want to build a Handwritten Digit Recognition on MNIST dataset using sklearn and I wanted to shuffle my train set for both features(x) and label(y).我想使用 sklearn 在 MNIST 数据集上构建手写数字识别,并且我想为特征(x)和标签(y)洗牌我的训练集。 But it shows a KeyError.
但它显示了一个 KeyError。 Let me know what is the correct way to do it.
让我知道什么是正确的方法。
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
Please check if type(x_train)
is numpy.ndarray or DataFrame.请检查
type(x_train)
是 numpy.ndarray 还是 DataFrame。 Since Scikit-Learn 0.24, fetch_openml()
returns a Pandas DataFrame
by default.从 Scikit-Learn 0.24 开始,
fetch_openml()
默认返回一个 Pandas DataFrame
。 If it is dataframe, in that case you can not use x_train[shuffled]
, which is meant for arrays.如果是数据帧,则不能使用
x_train[shuffled]
,它适用于数组。 Instead use x_train.iloc[shuffled]
而是使用
x_train.iloc[shuffled]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.