使用 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.

numpypandas、scipy、scikit-learn和matplotlib是Python中常用的科学计算和数据可视化。它们之间的关系是: - Numpy是一个用于进行数值计算的,提供了多维数组对象和一组用于操作数组的函数。它是其他科学计算的基础。 - Pandas是一个用于数据分析和处理的,提供了高效的数据结构和数据分析工具。它建立在Numpy之上,可以方便地处理和操作数据。 - Scipy是一个用于科学计算的,提供了许多数学、科学和工程计算的功能。它建立在Numpy之上,并提供了更高级的数学和科学计算功能。 - Scikit-learn是一个用于机器学习的,提供了各种机器学习算法和工具。它建立在Numpy和Scipy之上,并提供了用于训练和评估模型的函数和工具。 - Matplotlib是一个用于数据可视化的,提供了各种绘图函数和工具。它可以用于创建各种类型的图表和图形,包括线图、散点图、柱状图等。 如果你想学习这些,可以按照以下学习路径进行: 1. 确定学习目的,即你想要使用这些来解决什么问题或实现什么功能。 2. 搜索相关资源,包括官方文档、教程、示例代码等。你可以参考引用\[1\]和引用\[2\]中提供的学习资料。 3. 制定学习计划,确定你需要学习的内容和学习的顺序。你可以按照引用\[2\]中提供的学习路径原则进行规划。 总之,numpypandas、scipy、scikit-learn和matplotlib是Python中常用的科学计算和数据可视化,它们之间有着密切的关系,并且可以相互配合使用来进行数据分析、科学计算和机器学习等任务。 #### 引用[.reference_title] - *1* *2* [NumpyPandas、SciPy、Scikit-Learn、Matplotlib的关系以及学习资料](https://blog.csdn.net/u014410989/article/details/89947128)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [安装conda和pandasnumpyscikit-learn、seaborn、matplotlib、xlutils](https://blog.csdn.net/q839039228/article/details/124516133)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值