异常类型:ValueError:pickle 中的节点数组具有不兼容的 dtype

问题描述 投票:0回答:1

所以我从 Github 复制了一个 DjangoML 项目。 ML 模型本质上是一个诊断系统,可以根据患者的血液检测值来预测患者是否处于危险之中。

我正在 VSCode 本地运行此项目,但尚未部署后端/ML 模型。

我在尝试在网络浏览器中预测结果时遇到了这个问题。

我在这个项目中使用Python 3.11.0。

The requirements.txt file includes:

 asgiref==3.5.0

 Django==4.0.3

  joblib==1.1.0

 numpy~=1.26.4

 pandas==2.2.2

 python-dateutil==2.8.2

 pytz==2022.1

 scikit-learn==1.0.2

 scipy==1.8.0

 six==1.16.0

 sklearn==0.0

 sqlparse==0.4.2 

threadpoolctl==3.1.0 

tzdata==2021.5 

 `Traceback (most recent call last):

File "D:\Django_MachineLearning_HealthcareApp-main\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner response = get_response(request) 

File "D:\Django_MachineLearning_HealthcareApp-main\venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "D:\Django_MachineLearning_HealthcareApp-main\backend\views.py", line 76, in lpredictor result = ValuePredictor(llis, 7, mname) 

File "D:\Django_MachineLearning_HealthcareApp-main\backend\views.py", line 60, in ValuePredictor trained_model = joblib.load(rf'{mdname}_model.pkl') 

File "D:\Django_MachineLearning_HealthcareApp-main\venv\Lib\site-packages\joblib\numpy_pickle.py", line 658, in load obj = _unpickle(fobj, filename, mmap_mode) 

File "D:\Django_MachineLearning_HealthcareApp-main\venv\Lib\site-packages\joblib\numpy_pickle.py", line 577, in _unpickle obj = unpickler.load()

File "C:\Users\vanda\AppData\Local\Programs\Python\Python311\Lib\pickle.py", line 1213, in load dispatch[key[0]](self)

File "D:\Django_MachineLearning_HealthcareApp-main\venv\Lib\site-packages\joblib\numpy_pickle.py", line 402, in load_build Unpickler.load_build(self)

File "C:\Users\vang\AppData\Local\Programs\Python\Python311\Lib\pickle.py", line 1718, in load_build setstate(state) File "sklearn\tree\_tree.pyx", line 865, in sklearn.tree._tree.Tree.setstate <source code not available>

File "sklearn\tree\_tree.pyx", line 1571, in sklearn.tree._tree._check_node_ndarray <source code not available>

Exception Type: ValueError at /diagnose/liver/report

Exception Location:sklearn\tree\_tree.pyx, line 1571, in sklearn.tree._tree._check_node_ndarray Exception Value: node array from the pickle has an incompatible dtype:

expected: {'names': ['left_child', 'right_child', 'feature', 'threshold', 'impurity', 'n_node_samples', 'weighted_n_node_samples', 'missing_go_to_left'], 'formats': ['<i8', '<i8', '<i8', '<f8', '<f8', '<i8', '<f8', 'u1'], 'offsets': [0, 8, 16, 24, 32, 40, 48, 56], 'itemsize': 64}

got     : [('left_child', '<i8'), ('right_child', '<i8'), ('feature', '<i8'), ('threshold', '<f8'), ('impurity', '<f8'), ('n_node_samples', '<i8'), ('weighted_n_node_samples', '<f8')] `

我是 Python 和机器学习领域的新手,非常感谢任何有关这方面的帮助和解释。

python-3.x django scikit-learn pickle
1个回答
0
投票

您似乎正在尝试解开使用旧版本的 scikit-learn 和 joblib 训练和腌制的模型。 joblib pickled 的模型格式最近发生了变化,因此破坏了与旧文件的兼容性。

解决方案只是降级这些软件包的版本以匹配正确的版本。最有可能的是您发布的要求中指定的 scikit-learn==1.0.2 和 joblib==1.1.0。 实际上,您可能也必须降级您的Python版本,因为scikit-learn 1.0.2似乎需要python 3.10(而不是3.11)

© www.soinside.com 2019 - 2024. All rights reserved.