在python中无法从索引中找到数据



我试图使用matplotlib显示特定索引的图像,但它显示我错误,我没有得到为什么?我试图获得索引0的mnist数据,并将其大小调整为28 × 28像素,然后通过plot.show()函数显示该索引值。

%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
some_digit = X[0]
some_digit_image = some_digit.reshape(28, 28)
plt.imshow(
some_digit_image, 
cmap = matplotlib.cm.binary,
interpolation="nearest")
plt.axis("off")
plt.show()
KeyError                                  Traceback (most recent call last)
~/Machinelearning/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3079             try:
-> 3080                 return self._engine.get_loc(casted_key)
3081             except KeyError as err:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 0
The above exception was the direct cause of the following exception:
KeyError                                  Traceback (most recent call last)
<ipython-input-35-246778f0802e> in <module>
3 import matplotlib.pyplot as plt
4 
----> 5 some_digit = X[0]
6 some_digit_image = some_digit.reshape(28, 28)
7 
~/Machinelearning/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
3022             if self.columns.nlevels > 1:
3023                 return self._getitem_multilevel(key)
-> 3024             indexer = self.columns.get_loc(key)
3025             if is_integer(indexer):
3026                 indexer = [indexer]
~/Machinelearning/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3080                 return self._engine.get_loc(casted_key)
3081             except KeyError as err:
-> 3082                 raise KeyError(key) from err
3083 
3084         if tolerance is not None:
KeyError: 0 

变化

import numpy as np
x = [36000]

np.array(x.iloc[36000])

最新更新