streamlit Web 应用程序上的序列化机器学习模型(pickle)会抛出错误

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

我建立了一个爱彼迎机器学习模型来预测新加坡的价格。使用pickle,我在python 3.9中序列化了训练好的模型并将其推送到github。

位于此处的 Streamlit Web 应用程序 - https://just4jc-singapore-airbnb-new-streamlit-app-khwftb.streamlit.app/ 工作了一段时间,最近抛出了一个错误。

Traceback (most recent call last):

      File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script

        exec(code, module.__dict__)

      File "/app/singapore-airbnb/new.py", line 119, in <module>

        rf = pickle.load(file)

      File "sklearn/tree/_tree.pyx", line 714, in sklearn.tree._tree.Tree.__setstate__

      File "sklearn/tree/_tree.pyx", line 1418, in sklearn.tree._tree._check_node_ndarray

    ValueError: 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')]

    /home/appuser/venv/lib/python3.9/site-packages/sklearn/base.py:347: InconsistentVersionWarning: Trying to unpickle estimator DecisionTreeRegressor from version 1.2.0 when using version 1.3.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:

    https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations

      warnings.warn(

    2023-07-03 06:59:06.382 Uncaught app exception

    Traceback (most recent call last):

      File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script

        exec(code, module.__dict__)

      File "/app/singapore-airbnb/new.py", line 119, in <module>

        rf = pickle.load(file)

      File "sklearn/tree/_tree.pyx", line 714, in sklearn.tree._tree.Tree.__setstate__

      File "sklearn/tree/_tree.pyx", line 1418, in sklearn.tree._tree._check_node_ndarray

    ValueError: 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')]

    /home/appuser/venv/lib/python3.9/site-packages/sklearn/base.py:347: InconsistentVersionWarning: Trying to unpickle estimator DecisionTreeRegressor from version 1.2.0 when using version 1.3.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:

    https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations

      warnings.warn(

    2023-07-03 06:59:19.079 Uncaught app exception

    Traceback (most recent call last):

      File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script

        exec(code, module.__dict__)

      File "/app/singapore-airbnb/new.py", line 119, in <module>

        rf = pickle.load(file)

      File "sklearn/tree/_tree.pyx", line 714, in sklearn.tree._tree.Tree.__setstate__

      File "sklearn/tree/_tree.pyx", line 1418, in sklearn.tree._tree._check_node_ndarray

    ValueError: 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')]

    /home/appuser/venv/lib/python3.9/site-packages/sklearn/base.py:347: InconsistentVersionWarning: Trying to unpickle estimator DecisionTreeRegressor from version 1.2.0 when using version 1.3.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:

    https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations

      warnings.warn(

    2023-07-03 07:21:24.303 Uncaught app exception

    Traceback (most recent call last):

      File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script

        exec(code, module.__dict__)

      File "/app/singapore-airbnb/new.py", line 119, in <module>

        rf = pickle.load(file)

      File "sklearn/tree/_tree.pyx", line 714, in sklearn.tree._tree.Tree.__setstate__

      File "sklearn/tree/_tree.pyx", line 1418, in sklearn.tree._tree._check_node_ndarray

    ValueError: 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')]

感谢对此的任何帮助。

pickle streamlit
2个回答
5
投票

错误消息中的此警告行表明这是版本控制冲突:

InconsistentVersionWarning: Trying to unpickle estimator DecisionTreeRegressor from version 1.2.0 when using version 1.3.0. This might lead to breaking code or invalid results. Use at your own risk.

这是您用于生成/pickle 模型的

sklearn
版本与应用程序用于打开模型的版本之间的版本控制冲突。 1.3版本于2023年6月发布

要解决您的问题,您可以:

  1. 重新训练您的模型以与新版本兼容。
  2. 切换到之前版本的
    sklearn

1
投票

我也出现了同样的错误,我通过在github上编辑requirement.txt解决了它

按类型

scikit-learn==1.2.2

然后错误就消失了 非常感谢 anusunalia ,我刚刚从 discuss.streamlit

完成了这个解决方案
© www.soinside.com 2019 - 2024. All rights reserved.