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

这篇具有很好参考价值的文章主要介绍了Bug小能手系列(python)_9: 使用sklearn库报错 module ‘numpy‘ has no attribute ‘int‘。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

0. 错误介绍

在使用sklearn库的metricscohen_kappa_score()函数以及preprocessing.OneHotEncoderfit_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))

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

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

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报错,可以通过修改该用法来解决。这里我们点击报错所给的链接,然后得到了下面的图像:
Bug小能手系列(python)_9: 使用sklearn库报错 module ‘numpy‘ has no attribute ‘int‘,Bug小能手,python,bug,sklearn
从上面可以看出,对于调用np.int报错的内容,可以通过将np.int替换为np.int_np.int32或者np.int64来解决该报错。

3.2 库包中存在报错

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

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

pip install scikit-learn --upgrade --user

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

scikit-learn             1.3.0

最后,实验结果表明:sklearn库包经更新后,代码可以跑通了!实在是不容易哦!!!
Bug小能手系列(python)_9: 使用sklearn库报错 module ‘numpy‘ has no attribute ‘int‘,Bug小能手,python,bug,sklearn

4. 总结

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

如果觉得这篇文章对你有用,记得点赞、收藏并分享给你的小伙伴们哦😄。文章来源地址https://www.toymoban.com/news/detail-706095.html

到了这里,关于Bug小能手系列(python)_9: 使用sklearn库报错 module ‘numpy‘ has no attribute ‘int‘的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • python 安装lxml库报错 提示安装Microsoft Visual C++ 14.0

    python 安装lxml库报错 提示安装Microsoft Visual C++ 14.0 【背景】 入手了一台新工作站,安装完系统后直接下载安装了最新版本的python3.11,在跑之前的程序的时候发现依赖包无法安装成功。前期正常的程序时在另外一台服务器上,python版本为3.9,操作系统为windows server2012,现在的环

    2024年02月08日
    浏览(39)
  • python安装第三方库报错(Failed building wheel for 库名)

         在安装cartopy 和 gdal库时提示安装wheel文件错误,具体如下:        “ This error originates from a  subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for cartopy Failed to build cartopy ERROR: Could not build wheels for cartopy, which is required to install pyproject.toml-based projects”     问

    2024年02月12日
    浏览(35)
  • Python 中错误 ImportError: No Module Named Sklearn

    在 Python 中,sklearn 被用作机器学习工具,用于在回归、集群等方面创建程序。很多时候,导入它会抛出错误—— No module named sklearn 。 这意味着由于安装错误、无效的 Python 或 pip 版本或其他问题,系统无法找到它。 假设我们将 sklearn 或任何 Python 库安装到系统中。 系统提示

    2024年02月09日
    浏览(33)
  • Python中的ModuleNotFoundError: No module named ‘sklearn‘解决方案

    Python中的ModuleNotFoundError: No module named \\\'sklearn’解决方案 当你在使用Python编程语言时,如果遇到类似于\\\"ModuleNotFoundError: No module named ‘sklearn’\\\"的错误消息,它表示你尝试导入名为’sklearn’的模块,但该模块未安装或不可用。这通常发生在你没有正确安装所需的第三方库或模块

    2024年02月07日
    浏览(30)
  • rollup 打包vue3组件库报错 ‘createElementVNode‘ is not exported by node\_modules\\vue\\dist\\vue.runtime.esm

    在这里插入图片描述 报错意思就是说 createElementVNode 没有被导出。也就是找不到。然后看下面 1: import { renderSlot as _renderSlot, toDisplayString as _toDisplayString, createElementVNode as _createElementVNode, normalizeClass as _normalizeClass, normalizeStyle as _normalizeStyle, renderList as _renderList, Fragment as _Fragmen

    2024年02月03日
    浏览(35)
  • python|安装skimage库报错:required to install pyproject.toml-based projects

    一、安装报错问题 使用pip安装,先用命令:pip install skimage ,安装失败,然后参考发现skimage库的命令是:pip install scikit-image,也没有成功  二、解决方法 参考帖子 (1条消息) python skimage库的安装方法_AmosLLc的博客-CSDN博客_python安装skimage库 https://blog.csdn.net/AmosLLc/article/details/8

    2023年04月08日
    浏览(28)
  • 安装python库报错:Consider using the `--user` option or check the permissions.

    昨天在服务器上训练ResNet,导包出现了一些小问题: 1)安装tqdm库的时候,出现 Consider using the ‘–user’ option or check the permissions.访问python下的site-package权限不够。添加‘–user’即可,再次尝试使用以下命令 问题解决。 2)使用torch和torchvision的时候出现异常,Invoked with: typi

    2024年02月08日
    浏览(37)
  • 在pycharm中使用ffmpeg库报错:FileNotFoundError: [WinError 2] 系统找不到指定的文件 的解决方案

     我在Pycharm中运行  ffmpeg 库 以下代码时出现错误 报错内容为 以上报错主要可以看最后一句: FileNotFoundError: [WinError 2] 系统找不到指定的文件。 在模块安装正确的情况下,应该是电脑本地没有安装 ffmpeg软件 ,可以搜索 Windows下安装ffmpeg的教程,也可以直接参考以下链接:

    2023年04月14日
    浏览(36)
  • 造轮子系列】面试官问:你能手写Vuex吗(一)?

    前后端面试题库 (面试必备) 推荐:★★★★★ 地址:前端面试题库  web前端面试题库 VS java后端面试题库大全 Vuex 是 Vue.js 的状态管理模式,它主要解决了组件之间共享状态时的问题。在本文中,我们将实现一个简单的状态管理器,来帮助我们更好地理解 Vuex 的实现原理

    2024年02月03日
    浏览(29)
  • 关于VC++运行库报错

    Microsoft Visual C++ (简称 Visual C++ 、 MSVC 、 VC++ 或 VC )是微软公司的C++开发工具,具有一体化开发环境,可提供编辑 C语言,C++以及C++/CLI等程式语言。 VC++集成了便利的调试工具,特别是整合了微软Windows窗口操作系统应用程式接口(Windows API)、三维动画DirectX API,Microsoft .N

    2024年02月06日
    浏览(57)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包