Machine learning 为什么在MNIST分类器代码中使用X[0]会给我带来错误?
我正在学习使用MNIST数据集进行分类。我犯了一个错误,我不知道,我做了很多谷歌搜索,我什么都做不了,也许你是个专家,可以帮助我。这是密码-- 输出: dict_键(['data'、'target'、'frame'、'categories'、'feature_name'、'target_name'、'DESCR'、'details'、'url'])) 产出:(70000784) 产出:(70000)Machine learning 为什么在MNIST分类器代码中使用X[0]会给我带来错误?,machine-learning,mnist,Machine Learning,Mnist,我正在学习使用MNIST数据集进行分类。我犯了一个错误,我不知道,我做了很多谷歌搜索,我什么都做不了,也许你是个专家,可以帮助我。这是密码-- 输出: dict_键(['data'、'target'、'frame'、'categories'、'feature_name'、'target_name'、'DESCR'、'details'、'url'])) 产出:(70000784) 产出:(70000) >X[0] 输出:KeyError回溯(最近一次调用) c:\users\khush\appda
>X[0]
输出:KeyError回溯(最近一次调用)
c:\users\khush\appdata\local\programs\python39\lib\site packages\pandas\core\index\base.py in get\u loc(self、key、method、tolerance)
2897尝试:
->2898自动返回引擎。获取锁定(铸造键)
2899除KeyError作为错误外:
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
关键错误:0
上述异常是以下异常的直接原因:
KeyError回溯(最近一次呼叫最后一次)
在里面
---->1 X[0]
c:\users\khush\appdata\local\programs\python39\lib\site packages\pandas\core\frame.py in\uuuuu\getitem\uuuuu(self,key)
2904如果self.columns.nlevels>1:
2905返回自我。\u获取项目\u多级(键)
->2906索引器=self.columns.get_loc(键)
2907如果是_整数(索引器):
2908索引器=[索引器]
c:\users\khush\appdata\local\programs\python39\lib\site packages\pandas\core\index\base.py in get\u loc(self、key、method、tolerance)
2898自动返回引擎。获取锁定(铸造键)
2899除KeyError作为错误外:
->2900从err升起钥匙错误(钥匙)
2901
2902如果公差不是无:
关键错误:0
请回答,这可能是一个愚蠢的错误,因为我在ML中是一个乞丐。如果您也给我一些提示,那将非常有用。获取openml的API在不同版本之间更改。最初,它返回一个
pandas.DataFrame
,这正是您所拥有的。由于0.24.0
(2020年12月),as\u frame
的fetch\u openml
参数被设置为False,这将为您提供一个numpy.ndarray
。您应该将数据帧转换为numpy.ndarray
,请参阅pandas
,或者升级sklearn
不同版本之间的
fetch\u openml
API发生了变化。最初,它返回一个pandas.DataFrame
,这正是您所拥有的。由于0.24.0
(2020年12月),as\u frame
的fetch\u openml
参数被设置为False,这将为您提供一个numpy.ndarray
。您应该将数据帧转换为numpy.ndarray
,请参阅pandas
,或者升级sklearn
我也面临着同样的问题
- scikit学习:0.24.0
- matplotlib:3.3.3
- Python:3.9.1
import matplotlib as mpl
import matplotlib.pyplot as plt
# instead of some_digit = X[0]
some_digit = X.to_numpy()[0]
some_digit_image = some_digit.reshape(28,28)
plt.imshow(some_digit_image,cmap="binary")
plt.axis("off")
plt.show()
我也面临着同样的问题
- scikit学习:0.24.0
- matplotlib:3.3.3
- Python:3.9.1
import matplotlib as mpl
import matplotlib.pyplot as plt
# instead of some_digit = X[0]
some_digit = X.to_numpy()[0]
some_digit_image = some_digit.reshape(28,28)
plt.imshow(some_digit_image,cmap="binary")
plt.axis("off")
plt.show()
如果遵循以下代码,则无需降级scikit学习库:
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version= 1, as_frame= False)
mnist.keys()
如果遵循以下代码,则无需降级scikit学习库:
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version= 1, as_frame= False)
mnist.keys()
我们在访问字典时通常会得到
KeyError
,这似乎X
是一本字典,试着打印X
看看它包含什么我们在访问字典时通常会得到KeyError
,这似乎X
是一本字典,试着打印X
看看它包含什么,我使用的是较旧的numpy版本,因为使用较新的版本会导致运行时错误。顺便说一下,非常感谢。是的,我使用的是旧版本的numpy,因为使用新版本会给我带来运行时错误。顺便说一句,非常感谢。太好了,你是在用《用scikit学习tensorflow和Keras进行机器学习》这本书吗?是的,我刚开始读这本书。如果它解决了问题,你也能接受答案吗?太好了,你是在用《用scikit学习tensorflow和Keras进行机器学习》一书吗?是的,我刚开始读这本书。如果问题解决了,你也能接受答案吗。
import matplotlib as mpl
import matplotlib.pyplot as plt
# instead of some_digit = X[0]
some_digit = X.to_numpy()[0]
some_digit_image = some_digit.reshape(28,28)
plt.imshow(some_digit_image,cmap="binary")
plt.axis("off")
plt.show()
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version= 1, as_frame= False)
mnist.keys()