我正在使用 MNIST 数据集和 jupyter 笔记本做一个机器学习项目 [已关闭]

问题描述 投票:0回答:2

我对机器学习非常陌生,我正在编写 Python 代码,用于在 Jupyter 笔记本中对 MNIST 数据集执行手写识别。但是当我尝试运行笔记本时,出现以下错误。谁能帮我弄清楚为什么会这样? x_train 数组是一个 (70000,784) 数组。

~\AppData\Local\Temp/ipykernel_6700/1983129579.py in <module>
      1 shuffle_index = np.random.permutation(6000)
----> 2 x_train, y_train = x_train[shuffle_index], y_train[shuffle_index]

c:\users\hp\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   3459             if is_iterator(key):
   3460                 key = list(key)
-> 3461             indexer = self.loc._get_listlike_indexer(key, axis=1)[1]
   3462 
   3463         # take() does not accept boolean indexers

c:\users\hp\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis)
   1312             keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
   1313 
-> 1314         self._validate_read_indexer(keyarr, indexer, axis)
   1315 
   1316         if needs_i8_conversion(ax.dtype) or isinstance(

c:\users\hp\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis)
   1372                 if use_interval_msg:
   1373                     key = list(key)
-> 1374                 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
   1375 
   1376             not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique())

KeyError: "None of [Int64Index([4112, 3293,  403, 2579,  942,  987, 3778, 3831, 3053, 3412,\n            ...\n             642, 2789, 3410, 3946, 5883, 3439, 2029, 2776, 4626,  497],\n           dtype='int64', length=6000)] are in the [columns]"
python numpy machine-learning scikit-learn
2个回答
4
投票

如果 x_train 和 y_train 是 DataFrame,并且您希望结果是 DataFrame,则可以使用:

x_train = x_train.iloc[shuffle_index]

0
投票

你用错了方法,应该是这样的

x_train = np.random.permutation(x_train)
y_train = np.random.permutation(y_train)

您也可以参考这里的例子 np.随机.排列

© www.soinside.com 2019 - 2024. All rights reserved.