LEI_TTY7 2021-09-26 13:32 采纳率: 75%
浏览 81
已结题

Python机器学习的报错

操作环境是Anaconda
我的代码是

from sklearn.datasets import fetch_openml 
from matplotlib import pyplot as plt

mnist = fetch_openml('mnist_784')

x,y = mnist [ "data"] , mnist ["target"]
print("MINST数据集大小为:{}".format(x.shape))



for i in range (25):


    digit =x[i * 2500]



    digit_image = digit.reshape (28,28)

    plt.subplot(5,5, i +1)


    plt.axis ( ' off')



    plt.imshow (digit_image , cmap='gray ')


plt.show ()


报错

```python
MINST数据集大小为:(70000, 784)
Traceback (most recent call last):

  File "D:\Anaconda\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
    return self._engine.get_loc(casted_key)

  File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\hashtable_class_helper.pxi", line 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item

  File "pandas\_libs\hashtable_class_helper.pxi", line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 0


The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "C:\Users\Administrator\untitled3.py", line 21, in <module>
    digit =x[i * 2500]

  File "D:\Anaconda\lib\site-packages\pandas\core\frame.py", line 3024, in __getitem__
    indexer = self.columns.get_loc(key)

  File "D:\Anaconda\lib\site-packages\pandas\core\indexes\base.py", line 3082, in get_loc
    raise KeyError(key) from err

KeyError: 0

该怎么解决呀~

  • 写回答

2条回答 默认 最新

  • CSDN专家-kaily 2021-09-26 14:32
    关注
    from sklearn.datasets import fetch_openml 
    from matplotlib import pyplot as plt
    
    mnist = fetch_openml('mnist_784')
    
    x,y = mnist["data"] , mnist["target"]
    print("MINST数据集大小为:{}".format(x.shape))
    for i in range(25):
        digit =x.loc[i*2500].to_numpy()
        digit_image = digit.reshape(28,28)
        plt.subplot(5,5, i +1)
        plt.axis('off')
        plt.imshow(digit_image, cmap='gray')
    plt.show()
    

    问题有两个,一.读入形式是DateFrame,读行可以用loc函数,二.reshape是用于ndarray的, 需要用to_numpy()函数转换

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月4日
  • 已采纳回答 9月26日
  • 创建了问题 9月26日

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献