您现在的位置是:首页 > 技术教程 正文

Bug小能手系列(python)_9: 使用sklearn库报错 module ‘numpy‘ has no attribute ‘int‘

admin 阅读: 2024-03-19
后台-插件-广告管理-内容页头部广告(手机)

AttributeError: module 'numpy' has no attribute 'int'.

  • 0. 错误介绍
  • 1. 环境介绍
  • 2. 问题分析
  • 3. 解决方法
    • 3.1 调用解决
    • 3.2 库包中存在报错
  • 4. 总结

首先,对于自己使用代码dtype=np.int报错的情况,建议直接修改为np.int_即可解决,也不用向下看了!!!!
下文主要是针对sklearn库包中存在大量np.int报错的情况!!!

0. 错误介绍

在使用sklearn库的metrics的cohen_kappa_score()函数以及preprocessing.OneHotEncoder的fit_transform()函数时出错。当然包括sklearn中的很多函数。但是,需要注意的是:这里的报错是sklearn库中调用np.int报错,与自己书写时使用np.int报错是不同的。
因为,对于自己书写的np.int,直接进行修改就行(修改为:np.int_,np.int32或者np.int64)。而sklearn库中存在大量使用np.int的内容,修改过于麻烦,因此专门介绍对于该情况如何解决问题。
代码如下:

from mne.decoding import CSP # Common Spatial Pattern Filtering from mne.channels import read_layout from sklearn import preprocessing from sklearn.preprocessing import OneHotEncoder from sklearn.model_selection import ShuffleSplit # OneHotEncoding Labels enc = OneHotEncoder() X_out = enc.fit_transform(labels.reshape(-1,1)).toarray() # Cross Validation Split 交叉验证拆分 cv = ShuffleSplit(n_splits = 10, test_size = 0.2, random_state = 0) from sklearn.metrics import accuracy_score from sklearn.metrics import cohen_kappa_score from sklearn.metrics import precision_score from sklearn.metrics import recall_score acc = [] ka = [] prec = [] recall = [] ka.append(cohen_kappa_score(test_trues, test_pres))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

报错:
在这里插入图片描述

1. 环境介绍

首先,对代码当前使用环境进行简单介绍。

代码环境:python版本为:3.9、numpy版本为:1.25.0、sklearn版本为:0.21.3。 代码中存在大量关于jupyter的环境,可以在观看的时候将其忽略。

aiofiles 22.1.0 aiosqlite 0.18.0 albumentations 1.2.1 anyio 3.5.0 appdirs 1.4.4 argon2-cffi 21.3.0 argon2-cffi-bindings 21.2.0 asttokens 2.0.5 attrs 22.1.0 Babel 2.11.0 backcall 0.2.0 beautifulsoup4 4.12.2 bleach 4.1.0 brotlipy 0.7.0 certifi 2023.5.7 cffi 1.15.1 charset-normalizer 2.0.4 chinese-calendar 1.8.0 colorama 0.4.6 comm 0.1.2 contourpy 1.1.0 cryptography 39.0.1 cycler 0.11.0 debugpy 1.5.1 decorator 5.1.1 defusedxml 0.7.1 entrypoints 0.4 executing 0.8.3 fastjsonschema 2.16.2 fonttools 4.40.0 idna 3.4 importlib-metadata 6.0.0 importlib-resources 5.12.0 ipykernel 6.19.2 ipython 8.12.0 ipython-genutils 0.2.0 ipywidgets 8.0.4 jedi 0.18.1 Jinja2 3.1.2 joblib 1.2.0 json5 0.9.6 jsonschema 4.17.3 jupyter 1.0.0 jupyter_client 8.1.0 jupyter-console 6.6.3 jupyter_core 5.3.0 jupyter-events 0.6.3 jupyter_server 2.5.0 jupyter_server_fileid 0.9.0 jupyter_server_terminals 0.4.4 jupyter_server_ydoc 0.8.0 jupyter-ydoc 0.2.4 jupyterlab 3.6.3 jupyterlab-pygments 0.1.2 jupyterlab_server 2.22.0 jupyterlab-widgets 3.0.5 kiwisolver 1.4.4 lxml 4.9.2 MarkupSafe 2.1.1 matplotlib 3.7.1 matplotlib-inline 0.1.6 mistune 0.8.4 mkl-fft 1.3.6 mkl-random 1.2.2 mkl-service 2.4.0 mne 1.4.2 nbclassic 0.5.5 nbclient 0.5.13 nbconvert 6.5.4 nbformat 5.7.0 nest-asyncio 1.5.6 notebook 6.5.4 notebook_shim 0.2.2 numpy 1.25.0 packaging 23.0 pandocfilters 1.5.0 parso 0.8.3 pickleshare 0.7.5 Pillow 10.0.0 pip 23.1.2 platformdirs 2.5.2 ply 3.11 pooch 1.4.0 prometheus-client 0.14.1 prompt-toolkit 3.0.36 psutil 5.9.0 pure-eval 0.2.2 pycparser 2.21 Pygments 2.15.1 pyOpenSSL 23.0.0 pyparsing 3.1.0 pypiwin32 223 PyQt5 5.15.7 PyQt5-sip 12.11.0 pyrsistent 0.18.0 PySocks 1.7.1 python-dateutil 2.8.2 python-json-logger 2.0.7 pyttsx3 2.90 pytz 2022.7 PyWavelets 1.4.1 pywin32 305.1 pywinpty 2.0.10 PyYAML 6.0 pyzmq 25.1.0 qtconsole 5.4.2 QtPy 2.2.0 qudida 0.0.4 requests 2.29.0 rfc3339-validator 0.1.4 rfc3986-validator 0.1.1 scikit-learn 0.21.3 scipy 1.10.1 Send2Trash 1.8.0 setuptools 67.8.0 sip 6.6.2 six 1.16.0 sniffio 1.2.0 soupsieve 2.4 stack-data 0.2.0 terminado 0.17.1 threadpoolctl 2.2.0 tinycss2 1.2.1 toml 0.10.2 tomli 2.0.1 torch 1.12.1+cu116 torchaudio 0.12.1+cu116 torchvision 0.13.1+cu116 tornado 6.2 tqdm 4.65.0 traitlets 5.7.1 typing_extensions 4.6.3 urllib3 1.26.16 wcwidth 0.2.5 webencodings 0.5.1 websocket-client 0.58.0 wheel 0.38.4 widgetsnbextension 4.0.5 win-inet-pton 1.1.0 y-py 0.5.9 ypy-websocket 0.8.2 zipp 3.11.0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142

2. 问题分析

首先对问题进行分析,根据报错信息以及报错中的问题指出可以发现,错误来源于numpy库包在当前版本已经没有了np.int这个用法,而这里却使用了这个用法。np.int 在 NumPy 1.20 中已弃用,在 NumPy 1.24 中已删除。
具体地址为:~\AppData\Roaming\Python\Python39\site-packages\sklearn\metrics\classification.py:564 (~表示当前电脑用户地址,一般为:C:\user)
具体代码为:w_mat = np.ones([n_classes, n_classes], dtype=np.int)
错误为:module 'numpy' has no attribute 'int'.,即不能使用np.int,需要对其进行替换。

3. 解决方法

3.1 调用解决

对于自己调用np.int报错,可以通过修改该用法来解决。这里我们点击报错所给的链接,然后得到了下面的图像:
在这里插入图片描述
从上面可以看出,对于调用np.int报错的内容,可以通过将np.int替换为np.int_、np.int32或者np.int64来解决该报错。

3.2 库包中存在报错

对于上文提及的sklearn中存在报错的情况,虽然根据报错给出的具体地址可以找到报错代码,然后根据上文3.1节给出的修改方法可以解决该报错。但是,当你下次同样使用该库包时可能仍会出现上文给出的错误的情况。
因此,这里从sklearn库创建者的角度思考:我不能容忍一个错误存在这么久。所以,尝试对库包进行更新,而更新后的库包理论上应该不会存在该错误。

这里,使用以下命令对sklearn库包进行更新:

pip install scikit-learn --upgrade --user
  • 1

最后,得到更新后的sklearn的版本为:

scikit-learn 1.3.0
  • 1

最后,实验结果表明:sklearn库包经更新后,代码可以跑通了!实在是不容易哦!!!
在这里插入图片描述

4. 总结

总的而言,感觉出现该错误可能就是我使用的sklearn版本太低了了(scikit-learn=0.21.3),不过好歹问题解决了。网上的相关教程根本没有,所以根据自己的经验来总结这篇文章,希望大家会喜欢。如果有什么疑问欢迎在评论区提出,对于共性问题可能会后续添加到文章介绍中。

如果觉得这篇文章对你有用,记得点赞、收藏并分享给你的小伙伴们哦

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索