使用 NumPy 及其相关库(如 pandas、scikit-learn 等)时,由于 NumPy 的版本不兼容或者某些依赖库与 NumPy 的版本不匹配
题意:
numpy.dtype size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject
问题背景:
I want to call my Python module from the Matlab. I received the error:
Error using numpy_ops>init thinc.backends.numpy_ops
Python Error:
ValueError: numpy.dtype size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject.
The Python script is as follows
import spacy
def text_recognizer(model_path, text):
try:
# Load the trained model
nlp = spacy.load(model_path)
print("Model loaded successfully.")
# Process the given text
doc = nlp(text)
ent_labels = [(ent.text, ent.label_) for ent in doc.ents]
return ent_labels
The Matlab script is as follows
% Set up the Python environment
pe = pyenv;
py.importlib.import_module('final_output');
% Add the directory containing the Python script to the Python path
path_add = fileparts(which('final_output.py'));
if count(py.sys.path, path_add) == 0
insert(py.sys.path, int64(0), path_add);
end
% Define model path and text to process
model_path = 'D:\trained_model\\output\\model-best';
text = 'Roses are red';
% Call the Python function
pyOut = py.final_output.text_recognizer(model_path, text);
% Convert the output to a MATLAB cell array
entity_labels = cell(pyOut);
disp(entity_labels);
I found one solution to update Numpy, what I did, but nothing changed. I am using Python 3.9 and Numpy version 2.0.0
The error was received when I tried to call the Python module using a Matlab script.
How can I fix the issue?
问题解决:
The reason is that pandas
defines its numpy
dependency freely as "anything newer than certain version of numpy". The problem occured, when numpy==2.0.0
has been released on June 16th 2024, because it is no longer compatible with your pandas version.
The solution is to pin down the numpy
version to any before the 2.0.0
. Today it could be (this is the most recent numpy 1
release):
numpy==1.26.4
To be added in your requirements or to the pip command you use (but together with installing pandas).
Nowadays pip
is very flexible and can handle the issue flawesly. You just need to ask it to install both pandas
and numpy
of given versions in the same pip install
invocation.
原文地址:https://blog.csdn.net/suiusoar/article/details/140408689
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!