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

相关推荐

  1. Numpy

    2024-07-14 12:18:01       67 阅读
  2. Python中 NumPyPandas介绍

    2024-07-14 12:18:01       18 阅读
  3. numpy一些常用函数

    2024-07-14 12:18:01       46 阅读
  4. Numpy介绍

    2024-07-14 12:18:01       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 12:18:01       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 12:18:01       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 12:18:01       62 阅读
  4. Python语言-面向对象

    2024-07-14 12:18:01       72 阅读

热门阅读

  1. 如何隐藏 Ubuntu 顶部状态栏

    2024-07-14 12:18:01       34 阅读
  2. springboot2——功能和原理

    2024-07-14 12:18:01       27 阅读
  3. Oracle 数据清理

    2024-07-14 12:18:01       22 阅读
  4. win32:第一个窗口程序-初始化实例(part.5)

    2024-07-14 12:18:01       27 阅读
  5. Flask与Celery实现Python调度服务

    2024-07-14 12:18:01       25 阅读
  6. `speech_recognition` 是一个流行的库

    2024-07-14 12:18:01       23 阅读
  7. 致十年后的自己

    2024-07-14 12:18:01       14 阅读
  8. 25秋招面试算法题 (Go版本)

    2024-07-14 12:18:01       25 阅读
  9. yii2 AssetBundle使用

    2024-07-14 12:18:01       24 阅读
  10. 如何使用IPython的并行计算能力处理大数据

    2024-07-14 12:18:01       24 阅读
  11. 如何定义版本号--语义化版本

    2024-07-14 12:18:01       22 阅读
  12. IOS热门面试题(一)

    2024-07-14 12:18:01       17 阅读
  13. Set和Map的用法

    2024-07-14 12:18:01       25 阅读