Python 运行时警告:numpy.dtype 大小已更改,可能表示二进制不兼容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40845304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:05:11  来源:igfitidea点击:

RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility

pythonnumpyscikit-learn

提问by Blue482

I have this error for trying to load a saved SVM model. I have tried uninstalling sklearn, NumPy and SciPy, reinstalling the latest versions all-together again (using pip). I am still getting this error. Why?

尝试加载已保存的 SVM 模型时出现此错误。我已经尝试卸载 sklearn、NumPy 和 SciPy,再次重新安装最新版本(使用 pip)。我仍然收到此错误。为什么?

In [1]: import sklearn; print sklearn.__version__
0.18.1
In [3]: import numpy; print numpy.__version__
1.11.2
In [5]: import scipy; print scipy.__version__
0.18.1
In [7]: import pandas; print pandas.__version__
0.19.1

In [10]: clf = joblib.load('model/trained_model.pkl')
---------------------------------------------------------------------------
RuntimeWarning                            Traceback (most recent call last)
<ipython-input-10-5e5db1331757> in <module>()
----> 1 clf = joblib.load('sentiment_classification/model/trained_model.pkl')

/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.pyc in load(filename, mmap_mode)
    573                     return load_compatibility(fobj)
    574
--> 575                 obj = _unpickle(fobj, filename, mmap_mode)
    576
    577     return obj

/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.pyc in _unpickle(fobj, filename, mmap_mode)
    505     obj = None
    506     try:
--> 507         obj = unpickler.load()
    508         if unpickler.compat_mode:
    509             warnings.warn("The file '%s' has been generated with a "

/usr/lib/python2.7/pickle.pyc in load(self)
    862             while 1:
    863                 key = read(1)
--> 864                 dispatch[key](self)
    865         except _Stop, stopinst:
    866             return stopinst.value

/usr/lib/python2.7/pickle.pyc in load_global(self)
   1094         module = self.readline()[:-1]
   1095         name = self.readline()[:-1]
-> 1096         klass = self.find_class(module, name)
   1097         self.append(klass)
   1098     dispatch[GLOBAL] = load_global

/usr/lib/python2.7/pickle.pyc in find_class(self, module, name)
   1128     def find_class(self, module, name):
   1129         # Subclasses may override this
-> 1130         __import__(module)
   1131         mod = sys.modules[module]
   1132         klass = getattr(mod, name)

/usr/local/lib/python2.7/dist-packages/sklearn/svm/__init__.py in <module>()
     11 # License: BSD 3 clause (C) INRIA 2010
     12
---> 13 from .classes import SVC, NuSVC, SVR, NuSVR, OneClassSVM, LinearSVC, \
     14         LinearSVR
     15 from .bounds import l1_min_c

/usr/local/lib/python2.7/dist-packages/sklearn/svm/classes.py in <module>()
      2 import numpy as np
      3
----> 4 from .base import _fit_liblinear, BaseSVC, BaseLibSVM
      5 from ..base import BaseEstimator, RegressorMixin
      6 from ..linear_model.base import LinearClassifierMixin, SparseCoefMixin, \

/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py in <module>()
      6 from abc import ABCMeta, abstractmethod
      7
----> 8 from . import libsvm, liblinear
      9 from . import libsvm_sparse
     10 from ..base import BaseEstimator, ClassifierMixin

__init__.pxd in init sklearn.svm.libsvm (sklearn/svm/libsvm.c:10207)()

RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 80

UPDATE:OK, by following here, and

更新:好的,按照这里,和

pip uninstall -y scipy scikit-learn
pip install --no-binary scipy scikit-learn

The error has now gone, though I still have no idea why it occurred in the first place...

错误现在已经消失了,尽管我仍然不知道为什么它首先发生......

采纳答案by ivan_pozdeev

According to MAINT: silence Cython warnings about changes dtype/ufunc size. - numpy/numpy:

根据MAINT:沉默 Cython 关于更改 dtype/ufunc 大小的警告。- 麻木/麻木

These warnings are visible whenever you import scipy (or another package) that was compiled against an older numpy than is installed.

每当您导入针对比安装的旧 numpy 编译的 scipy(或其他包)时,这些警告都是可见的。

and the checks are inserted by Cython (hence are present in any module compiled with it).

并且检查由 Cython 插入(因此存在于用它编译的任何模块中)。

Long story short, these warnings should be benign in the particular case of numpy, and these messages are filtered out since numpy 1.8(the branch this commit went onto). While scikit-learn 0.18.1is compiled against numpy 1.6.1.

长话短说,这些警告在 的特殊情况下应该是良性的numpy,并且这些消息自numpy 1.8(此提交进入的分支)以来被过滤掉。Whilescikit-learn 0.18.1是针对numpy 1.6.1.

To filter these warnings yourself, you can do the same as the patch does:

要自己过滤这些警告,您可以执行与补丁相同的操作

import warnings
warnings.filterwarnings("ignore", message="numpy.dtype size changed")
warnings.filterwarnings("ignore", message="numpy.ufunc size changed")

Of course, you can just recompile all affected modules from source against your local numpywith pip install --no-binary :all:1 insteadif you have the ballstools for that.

当然,你可以重新编译从源代码的所有受影响的模块对本地numpypip install --no-binary :all:1而不是如果你有该工具。



Longer story: the patch's proponent claimsthere should be no risk specifically with numpy, and 3rd-party packages are intentionally built against older versions:

长话短说:补丁的支持者声称不应该有风险numpy,并且 3rd-party 软件包是故意针对旧版本构建的:

[Rebuilding everything against current numpy is] not a feasible solution, and certainly shouldn't be necessary. Scipy (as many other packages) is compatible with a number of versions of numpy. So when we distribute scipy binaries, we build them against the lowest supported numpy version (1.5.1 as of now) and they work with 1.6.x, 1.7.x and numpy master as well.

The real correct would be for Cython only to issue warnings when the size of dtypes/ufuncs has changes in a way that breaks the ABI, and be silent otherwise.

[根据当前的 numpy 重建所有内容] 不是一个可行的解决方案,当然也没有必要。Scipy(与许多其他软件包一样)与许多版本的 numpy 兼容。因此,当我们分发 scipy 二进制文件时,我们针对支持的最低 numpy 版本(目前为 1.5.1)构建它们,并且它们也适用于 1.6.x、1.7.x 和 numpy master。

真正正确的是 Cython 仅在 dtypes/ufuncs 的大小以破坏 ABI 的方式发生变化时发出警告,否则保持沉默。

As a result, Cython's devs agreed to trust the numpy team with maintaining binary compatibility by hand, so we can probably expect that using versions with breaking ABI changes would yield a specially-crafted exception or some other explicit show-stopper.

因此,Cython 的开发人员同意信任 numpy 团队手动维护二进制兼容性,因此我们可以预期,使用具有破坏性 ABI 更改的版本会产生特制的异常或其他一些显式的显示阻止器。



1The previously available --no-use-wheeloption has been removed since pip 10.0.0.

1以前提供的--no-use-wheel选项已被删除,因为pip 10.0.0

回答by Parthiban.S

It's the issue of new numpy version (1.15.0)

这是新的numpy版本(1.15.0)的问题

You can downgrade numpy and this problem will be fixed:

您可以降级 numpy,此问题将得到解决:

sudo pip uninstall numpy
sudo pip install numpy==1.14.5

sudo pip uninstall numpy
sudo pip install numpy==1.14.5

Finally numpy 1.15.1 version is released so the warning issues are fixed.

sudo pip install numpy==1.15.1

最后 numpy 1.15.1 版本发布,因此警告问题已修复。

须藤pip安装numpy==1.15.1

This is working..

这是工作..

回答by Hemanth Kumar Talla

I've tried the above-mentioned ways, but nothing worked. But the issue was gone after I installed the libraries through apt install,

我已经尝试了上述方法,但没有任何效果。但是在我通过 apt install 安装库后问题就消失了,

For Python3,

对于 Python3,

pip3 uninstall -y numpy scipy pandas scikit-learn
sudo apt update
sudo apt install python3-numpy python3-scipy python3-pandas python3-sklearn 

For Python2,

对于 Python2,

pip uninstall -y numpy scipy pandas scikit-learn
sudo apt update
sudo apt install python-numpy python-scipy python-pandas python-sklearn 

Hope that helps.

希望有帮助。

回答by H. Shad

if you are in an anaconda environment use:

如果您在 anaconda 环境中,请使用:

conda update --all

回答by satyam_sareen

Just upgrade your numpy module, right now it is 1.15.4. For windows

只需升级您的 numpy 模块,现在它是 1.15.4。窗户用

pip install numpy --upgrade

回答by Temak

This error occurs because the installed packages were build agains different version of numpy.
We need to rebuild scipy and scikit-learn against the local numpy.

发生此错误是因为已安装的软件包是针对不同版本的 numpy 构建的。
我们需要针对本地numpy.

For new pip(in my case pip 18.0) this worked:

对于新的pip(在我的情况下pip 18.0),这有效:

pip uninstall -y scipy scikit-learn
pip install --no-binary scipy,scikit-learn -I scipy scikit-learn

--no-binarytakes a list of names of packages that you want to ignore binaries for. In this case we passed --no-binary scipy,scikit-learnwhich will ignore binaries for packages scipy,scikit-learn. Didn't help me

--no-binary获取要忽略二进制文件的软件包名称列表。在这种情况下,我们通过了--no-binary scipy,scikit-learn这将忽略包 scipy、scikit-learn 的二进制文件。没有帮助我

回答by serv-inc

Meta-information: The recommended way to install sklearn

元信息:安装sklearn的推荐方式

If you already have a working installation of numpy and scipy, the easiest way to install scikit-learn is using pip

pip install -U scikit-learn 

or conda:

conda install scikit-learn

如果您已经安装了 numpy 和 scipy,那么安装 scikit-learn 的最简单方法是使用 pip

pip install -U scikit-learn 

conda

conda install scikit-learn

[... do not compile from source using pip]

[...不要使用 pip 从源代码编译]

If you don't already have a python installation with numpy and scipy, we recommend to install either via your package manager or via apythonbundle. These come with numpy, scipy, scikit-learn, matplotlib and many other helpful scientific and data processing libraries.

如果您还没有一个Python安装与numpy的和SciPy的,我们建议通过你的包管理器,或通过任何安装一个python的。这些带有 numpy、scipy、scikit-learn、matplotlib 和许多其他有用的科学和数据处理库。

回答by mattip

Note that as of cython 0.29 there is a new check_size optionthat eliminates the warning at the source, so no work-arounds should be needed once that version percolates to the various packages

请注意,从 cython 0.29 开始,有一个新的check_size 选项可以消除源代码中的警告,因此一旦该版本渗透到各种包中,就不需要解决方法

回答by Dan

My enviroment is Python 2.7.15

我的环境是 Python 2.7.15

I try

我试试

pip uninstall
pip install --no-use-wheel

but it does not work. It shows the error:

但它不起作用。它显示错误:

no such option: --no-use-wheel

没有这样的选项:--no-use-wheel

Then I try:

然后我尝试:

pip uninstall
pip install --user --install-option="--prefix=" -U scikit-learn

And it works: the useless warnings do not show.

它有效:无用的警告不显示。

回答by Hao Xiang

When import scipy, error info shows: RuntimeWarning: builtin.type size changed, may indicate binary incompatibility. Expected zd, got zd

导入 scipy 时,错误信息显示: RuntimeWarning: builtin.type size changed,可能表示二进制不兼容。预期 zd,得到 zd

I solved this problem by updating python version from 2.7.2 to 2.7.13

我通过将 python 版本从 2.7.2 更新到 2.7.13 解决了这个问题