在Streamlit Web应用中序列化的机器学习模型(pickle)引发了错误。

huangapple go评论130阅读模式
英文:

serialized machine learning model (pickle) on streamlit web app throws an error

问题

I have built an airbnb machine learning model to predict prices for Singapore. Using pickle, I've serialized the trained model in python 3.9 and pushed it to github.

The streamlit web app located here - https://just4jc-singapore-airbnb-new-streamlit-app-khwftb.streamlit.app/ worked for some time and recently throws an error.

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

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

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

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

英文:

I have built an airbnb machine learning model to predict prices for Singapore. Using pickle, I've serialised the trained model in python 3.9 and pushed it to github.

The streamlit web app located here - https://just4jc-singapore-airbnb-new-streamlit-app-khwftb.streamlit.app/ worked for sometime and recently throws an error.

Traceback (most recent call last):
File &quot;/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py&quot;, line 552, in _run_script
exec(code, module.__dict__)
File &quot;/app/singapore-airbnb/new.py&quot;, line 119, in &lt;module&gt;
rf = pickle.load(file)
File &quot;sklearn/tree/_tree.pyx&quot;, line 714, in sklearn.tree._tree.Tree.__setstate__
File &quot;sklearn/tree/_tree.pyx&quot;, line 1418, in sklearn.tree._tree._check_node_ndarray
ValueError: node array from the pickle has an incompatible dtype:
- expected: {&#39;names&#39;: [&#39;left_child&#39;, &#39;right_child&#39;, &#39;feature&#39;, &#39;threshold&#39;, &#39;impurity&#39;, &#39;n_node_samples&#39;, &#39;weighted_n_node_samples&#39;, &#39;missing_go_to_left&#39;], &#39;formats&#39;: [&#39;&lt;i8&#39;, &#39;&lt;i8&#39;, &#39;&lt;i8&#39;, &#39;&lt;f8&#39;, &#39;&lt;f8&#39;, &#39;&lt;i8&#39;, &#39;&lt;f8&#39;, &#39;u1&#39;], &#39;offsets&#39;: [0, 8, 16, 24, 32, 40, 48, 56], &#39;itemsize&#39;: 64}
- got     : [(&#39;left_child&#39;, &#39;&lt;i8&#39;), (&#39;right_child&#39;, &#39;&lt;i8&#39;), (&#39;feature&#39;, &#39;&lt;i8&#39;), (&#39;threshold&#39;, &#39;&lt;f8&#39;), (&#39;impurity&#39;, &#39;&lt;f8&#39;), (&#39;n_node_samples&#39;, &#39;&lt;i8&#39;), (&#39;weighted_n_node_samples&#39;, &#39;&lt;f8&#39;)]
/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 &quot;/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py&quot;, line 552, in _run_script
exec(code, module.__dict__)
File &quot;/app/singapore-airbnb/new.py&quot;, line 119, in &lt;module&gt;
rf = pickle.load(file)
File &quot;sklearn/tree/_tree.pyx&quot;, line 714, in sklearn.tree._tree.Tree.__setstate__
File &quot;sklearn/tree/_tree.pyx&quot;, line 1418, in sklearn.tree._tree._check_node_ndarray
ValueError: node array from the pickle has an incompatible dtype:
- expected: {&#39;names&#39;: [&#39;left_child&#39;, &#39;right_child&#39;, &#39;feature&#39;, &#39;threshold&#39;, &#39;impurity&#39;, &#39;n_node_samples&#39;, &#39;weighted_n_node_samples&#39;, &#39;missing_go_to_left&#39;], &#39;formats&#39;: [&#39;&lt;i8&#39;, &#39;&lt;i8&#39;, &#39;&lt;i8&#39;, &#39;&lt;f8&#39;, &#39;&lt;f8&#39;, &#39;&lt;i8&#39;, &#39;&lt;f8&#39;, &#39;u1&#39;], &#39;offsets&#39;: [0, 8, 16, 24, 32, 40, 48, 56], &#39;itemsize&#39;: 64}
- got     : [(&#39;left_child&#39;, &#39;&lt;i8&#39;), (&#39;right_child&#39;, &#39;&lt;i8&#39;), (&#39;feature&#39;, &#39;&lt;i8&#39;), (&#39;threshold&#39;, &#39;&lt;f8&#39;), (&#39;impurity&#39;, &#39;&lt;f8&#39;), (&#39;n_node_samples&#39;, &#39;&lt;i8&#39;), (&#39;weighted_n_node_samples&#39;, &#39;&lt;f8&#39;)]
/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 &quot;/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py&quot;, line 552, in _run_script
exec(code, module.__dict__)
File &quot;/app/singapore-airbnb/new.py&quot;, line 119, in &lt;module&gt;
rf = pickle.load(file)
File &quot;sklearn/tree/_tree.pyx&quot;, line 714, in sklearn.tree._tree.Tree.__setstate__
File &quot;sklearn/tree/_tree.pyx&quot;, line 1418, in sklearn.tree._tree._check_node_ndarray
ValueError: node array from the pickle has an incompatible dtype:
- expected: {&#39;names&#39;: [&#39;left_child&#39;, &#39;right_child&#39;, &#39;feature&#39;, &#39;threshold&#39;, &#39;impurity&#39;, &#39;n_node_samples&#39;, &#39;weighted_n_node_samples&#39;, &#39;missing_go_to_left&#39;], &#39;formats&#39;: [&#39;&lt;i8&#39;, &#39;&lt;i8&#39;, &#39;&lt;i8&#39;, &#39;&lt;f8&#39;, &#39;&lt;f8&#39;, &#39;&lt;i8&#39;, &#39;&lt;f8&#39;, &#39;u1&#39;], &#39;offsets&#39;: [0, 8, 16, 24, 32, 40, 48, 56], &#39;itemsize&#39;: 64}
- got     : [(&#39;left_child&#39;, &#39;&lt;i8&#39;), (&#39;right_child&#39;, &#39;&lt;i8&#39;), (&#39;feature&#39;, &#39;&lt;i8&#39;), (&#39;threshold&#39;, &#39;&lt;f8&#39;), (&#39;impurity&#39;, &#39;&lt;f8&#39;), (&#39;n_node_samples&#39;, &#39;&lt;i8&#39;), (&#39;weighted_n_node_samples&#39;, &#39;&lt;f8&#39;)]
/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 &quot;/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py&quot;, line 552, in _run_script
exec(code, module.__dict__)
File &quot;/app/singapore-airbnb/new.py&quot;, line 119, in &lt;module&gt;
rf = pickle.load(file)
File &quot;sklearn/tree/_tree.pyx&quot;, line 714, in sklearn.tree._tree.Tree.__setstate__
File &quot;sklearn/tree/_tree.pyx&quot;, line 1418, in sklearn.tree._tree._check_node_ndarray
ValueError: node array from the pickle has an incompatible dtype:
- expected: {&#39;names&#39;: [&#39;left_child&#39;, &#39;right_child&#39;, &#39;feature&#39;, &#39;threshold&#39;, &#39;impurity&#39;, &#39;n_node_samples&#39;, &#39;weighted_n_node_samples&#39;, &#39;missing_go_to_left&#39;], &#39;formats&#39;: [&#39;&lt;i8&#39;, &#39;&lt;i8&#39;, &#39;&lt;i8&#39;, &#39;&lt;f8&#39;, &#39;&lt;f8&#39;, &#39;&lt;i8&#39;, &#39;&lt;f8&#39;, &#39;u1&#39;], &#39;offsets&#39;: [0, 8, 16, 24, 32, 40, 48, 56], &#39;itemsize&#39;: 64}
- got     : [(&#39;left_child&#39;, &#39;&lt;i8&#39;), (&#39;right_child&#39;, &#39;&lt;i8&#39;), (&#39;feature&#39;, &#39;&lt;i8&#39;), (&#39;threshold&#39;, &#39;&lt;f8&#39;), (&#39;impurity&#39;, &#39;&lt;f8&#39;), (&#39;n_node_samples&#39;, &#39;&lt;i8&#39;), (&#39;weighted_n_node_samples&#39;, &#39;&lt;f8&#39;)]

Appreciate any help on this.

答案1

得分: 3

这个警告行在您的错误消息中表示这是一个版本冲突:

InconsistentVersionWarning: 在使用版本 1.3.0 时尝试从版本 1.2.0 反序列化评估器 DecisionTreeRegressor。这可能导致代码错误或无效的结果。请自行承担风险。

这是您用于生成/序列化模型的 scikit-learn 版本与应用程序用于打开它的版本之间的版本冲突。版本 1.3 于六月发布,切换到较早的版本可能会解决您的问题。

英文:

This warning line within your error messages indicates this is a versioning conflict:

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.

This is a versioning conflict between the version of sklearn that you used to generate/pickle the model and the version the application is using to open it. Version 1.3 was released in June and switching to a prior version would likely solve your problem.

答案2

得分: 0

相同的错误也发生在我身上,我通过在GitHub上编辑requirement.txt文件并输入scikit-learn==1.2.2来解决它,然后错误就消失了。非常感谢anusunalia,我只是从discuss.streamlit的解决方案中采用了这个方法。

英文:

The Same error was appeared with me and I solved it by edit requirement.txt on github

by type

scikit-learn==1.2.2

then the error has been disappeared
Many thanks to anusunalia , I just did this solution from discuss.streamlit

huangapple
  • 本文由 发表于 2023年7月4日 21:06:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定