PyCaret inside Streamlit

Summary

Hi all, seeking for your help please, I run pycaret and saved the model and build streamlit app and when I reach the loading the model and use it to predict it gave me the error in attached pic, I create condo vertical env and the pip list as bellow:
Package Version


altair 4.2.0
ansi2html 1.8.0
appnope 0.1.3
asn1crypto 1.5.1
astor 0.8.1
asttokens 2.2.1
attrs 22.2.0
backcall 0.2.0
base58 2.1.1
blinker 1.5
boto3 1.26.70
botocore 1.29.70
cachetools 5.2.0
category-encoders 2.6.0
certifi 2022.12.7
cffi 1.15.1
charset-normalizer 2.0.12
click 8.1.3
cloudpickle 2.2.1
comm 0.1.2
commonmark 0.9.1
contourpy 1.0.6
cryptography 36.0.2
cycler 0.11.0
Cython 0.29.33
dash 2.8.1
dash-core-components 2.0.0
dash-html-components 2.0.0
dash-table 5.0.0
debugpy 1.6.4
decorator 5.1.1
Deprecated 1.2.13
deprecation 2.1.0
distlib 0.3.6
entrypoints 0.4
enum-compat 0.0.3
executing 1.2.0
fastjsonschema 2.16.2
filelock 3.9.0
Flask 2.1.3
Flask-Cors 3.0.10
fonttools 4.38.0
gitdb 4.0.10
GitPython 3.1.30
greenlet 2.0.1
idna 3.4
imbalanced-learn 0.10.1
importlib-metadata 5.2.0
install 1.3.5
ipykernel 6.19.2
ipython 8.7.0
ipywidgets 8.0.4
itsdangerous 2.1.2
jedi 0.18.2
Jinja2 3.1.2
jmespath 1.0.1
joblib 1.2.0
jsonschema 4.17.3
jupyter_client 7.4.8
jupyter_core 5.1.0
jupyter-dash 0.4.2
jupyterlab-widgets 3.0.5
kaleido 0.2.1
kiwisolver 1.4.4
lightgbm 3.3.4
llvmlite 0.39.1
MarkupSafe 2.1.1
matplotlib 3.6.2
matplotlib-inline 0.1.6
nbformat 5.7.3
nest-asyncio 1.5.6
numba 0.56.4
numpy 1.23.5
orjson 3.8.6
oscrypto 1.3.0
packaging 22.0
pandas 1.4.4
parso 0.8.3
patsy 0.5.3
pexpect 4.8.0
pickleshare 0.7.5
Pillow 9.3.0
pip 23.0
pipenv 2023.2.4
platformdirs 2.6.0
plotly 5.13.0
plotly-resampler 0.8.3.1
pmdarima 2.0.2
prompt-toolkit 3.0.36
protobuf 3.20.3
psutil 5.9.4
ptyprocess 0.7.0
pure-eval 0.2.2
pyarrow 6.0.1
pycaret 3.0.0rc9
pycparser 2.21
pycryptodomex 3.16.0
pydeck 0.8.0
Pygments 2.13.0
PyJWT 2.6.0
Pympler 1.0.1
pyod 1.0.7
pyOpenSSL 22.0.0
pyparsing 3.0.9
pyrsistent 0.19.3
python-dateutil 2.8.2
pytz 2022.6
pytz-deprecation-shim 0.1.0.post0
pyzmq 24.0.1
requests 2.28.1
retrying 1.3.4
rich 12.6.0
s3transfer 0.6.0
schemdraw 0.15
scikit-learn 1.2.0
scikit-plot 0.3.7
scipy 1.10.0
seaborn 0.12.2
semver 2.13.0
setuptools 67.2.0
six 1.16.0
sklearn 0.0.post1
sktime 0.16.1
smmap 5.0.0
smogn 0.1.2
snowflake-connector-python 2.7.9
snowflake-sqlalchemy 1.2.4
SQLAlchemy 1.4.46
st-pages 0.3.0
stack-data 0.6.2
statsmodels 0.13.5
streamlit 1.18.1
streamlit-option-menu 0.3.2
tbats 1.1.2
tenacity 8.2.1
threadpoolctl 3.1.0
toml 0.10.2
toolz 0.12.0
tornado 6.2
tqdm 4.64.1
trace-updater 0.0.9
traitlets 5.7.1
typing_extensions 4.4.0
tzdata 2022.7
tzlocal 4.2
urllib3 1.26.13
validators 0.20.0
virtualenv 20.19.0
virtualenv-clone 0.5.7
watchdog 2.2.1
wcwidth 0.2.5
Werkzeug 2.1.2
wheel 0.38.4
widgetsnbextension 4.0.5
wrapt 1.14.1
xgboost 1.7.3
xxhash 3.2.0
yellowbrick 1.5
zipp 3.11.0

Steps to reproduce

Code snippet:

 if st.button('Predict'):  
        X_test=pd.read_csv('data/X_test.csv', index_col=None)  
        saved_reg = load_model('regression Model') 

        predictions = predict_model(saved_reg, data=X_test)
        st.dataframe(predictions)

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

Explain what you expect to happen when you run the code above.

Actual behavior:

AttributeError: ‘SimpleImputer’ object has no attribute ‘keep_empty_features’
Traceback:
File “/usr/local/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 565, in run_script
exec(code, module.dict)
File “/Users/My_Name/Desktop/regression_model/ml_model/app/pages/04_🏈_CFB.py”, line 295, in
st.button(‘Predict’, on_click=predict(X_test))
File “/Users/My_name/Desktop/regression_model/ml_model/app/pages/04_🏈_CFB.py”, line 288, in predict
predictions = predict_model(saved_reg, data=test_df)
File “/usr/local/lib/python3.10/site-packages/pycaret/regression/functional.py”, line 1921, in predict_model
return experiment.predict_model(
File “/usr/local/lib/python3.10/site-packages/pycaret/regression/oop.py”, line 2214, in predict_model
return super().predict_model(
File “/usr/local/lib/python3.10/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py”, line 4842, in predict_model
[f"{k}={v}" for k, v in locals().items() if k != “data”]
File “/usr/local/lib/python3.10/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py”, line 4842, in
[f"{k}={v}" for k, v in locals().items() if k != “data”]
File “/usr/local/lib/python3.10/site-packages/sklearn/base.py”, line 238, in repr
repr
= pp.pformat(self)
File “/usr/local/Cellar/python@3.10/3.10.10/Frameworks/Python.framework/Versions/3.10/lib/python3.10/pprint.py”, line 157, in pformat
self._format(object, sio, 0, 0, {}, 0)
File “/usr/local/Cellar/python@3.10/3.10.10/Frameworks/Python.framework/Versions/3.10/lib/python3.10/pprint.py”, line 174, in _format
rep = self._repr(object, context, level)
File “/usr/local/Cellar/python@3.10/3.10.10/Frameworks/Python.framework/Versions/3.10/lib/python3.10/pprint.py”, line 454, in _repr
repr, readable, recursive = self.format(object, context.copy(),
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 189, in format
return _safe_repr(
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 452, in _safe_repr
vrepr, vreadable, vrecur = saferepr(
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 419, in _safe_repr
orepr, oreadable, orecur = _safe_repr(
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 419, in _safe_repr
orepr, oreadable, orecur = _safe_repr(
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 452, in _safe_repr
vrepr, vreadable, vrecur = saferepr(
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 440, in _safe_repr
params = _changed_params(object)
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 93, in _changed_params
params = estimator.get_params(deep=False)
File “/usr/local/lib/python3.10/site-packages/sklearn/base.py”, line 170, in get_params
value = getattr(self, key)

Debug info

  • Streamlit version: 1.18.1
  • Python version: 3.8.16
  • Pycaret version: 3.0.0rc9
  • Using Conda virtual env
  • OS version: macOS Monterey 12.6
  • Browser version: safari 16.2

Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.

Links

  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

the same saved model I used out of streamlit and it worked as expected and I was able to load it and use to predict

Thanks help please

Can you post a minimal sample code where you can reproduce the issue?

Please share a link to your public github repo.
I can only guess, but I suspect that the installed version of scikit-learn is incompatible either with the model or with the installed version of PyCaret.

Hi Franki thanks for taking time to look at my issues and yeah I think that is the issue, can you tell me which version to use for all of them please…thanks

Hi Fredy, thanks for taking time to look at my issues

if st.button('Predict'):  
        X_test=pd.read_csv('data/X_test.csv', index_col=None)  
        saved_reg = load_model('regression Model') 

        predictions = predict_model(saved_reg, data=X_test)
        st.dataframe(predictions)

Everything before this code chance works as expected, but when to try to load the saved pycaret model it gives the error: AttributeError: ‘SimpleImputer’ object has no attribute ‘keep_empty_features’

Thanks

That code will not run on my computer.
What is X_test.csv looks like?

In the pycaret quick start, have you tried the regression example?

It is normal data hot encoded, and it work out of streamlit I mean when I call the load_model it work and use it to predict and it work as expected, but inside the streamlit app it did not, so mostly it is version incompatibility but still do not how to fix it… I appreciate your answer thanks

and yes I look at pycaret tutorial and work as is and works well…thanks Ferdy hope you have any idea

Have you tried it in streamlit? Does it work?

yes I load the saved model inside streamlit but not running the whole process and it gave me this error: AttributeError: ‘SimpleImputer’ object has no attribute ‘keep_empty_features’
Traceback:
File “/usr/local/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 565, in run_script
exec(code, module.dict)
File “/Users/My_Name/Desktop/regression_model/ml_model/app/pages/04_🏈_CFB.py”, line 295, in
st.button(‘Predict’, on_click=predict(X_test))
File “/Users/My_name/Desktop/regression_model/ml_model/app/pages/04_🏈_CFB.py”, line 288, in predict
predictions = predict_model(saved_reg, data=test_df)
File “/usr/local/lib/python3.10/site-packages/pycaret/regression/functional.py”, line 1921, in predict_model
return experiment.predict_model(
File “/usr/local/lib/python3.10/site-packages/pycaret/regression/oop.py”, line 2214, in predict_model
return super().predict_model(
File “/usr/local/lib/python3.10/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py”, line 4842, in predict_model
[f"{k}={v}" for k, v in locals().items() if k != “data”]
File “/usr/local/lib/python3.10/site-packages/pycaret/internal/pycaret_experiment/supervised_experiment.py”, line 4842, in
[f"{k}={v}" for k, v in locals().items() if k != “data”]
File “/usr/local/lib/python3.10/site-packages/sklearn/base.py”, line 238, in repr
repr
= pp.pformat(self)
File
“/usr/local/Cellar/python@3.10/3.10.10/Frameworks/Python.framework/Versions/3.10/lib/python3.10/pprint.py”, line 454, in _repr
repr, readable, recursive = self.format(object, context.copy(),
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 189, in format
return _safe_repr(
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 452, in _safe_repr
vrepr, vreadable, vrecur = saferepr(
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 419, in _safe_repr
orepr, oreadable, orecur = _safe_repr(
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 419, in _safe_repr
orepr, oreadable, orecur = _safe_repr(
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 452, in _safe_repr
vrepr, vreadable, vrecur = saferepr(
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 440, in _safe_repr
params = _changed_params(object)
File “/usr/local/lib/python3.10/site-packages/sklearn/utils/_pprint.py”, line 93, in _changed_params
params = estimator.get_params(deep=False)
File “/usr/local/lib/python3.10/site-packages/sklearn/base.py”, line 170, in get_params
value = getattr(self, key)

You are using python 3.10. But Pycaret only supports 3.9 for ubuntu. See installation. Perhaps this could be one of the issues.

well I notice that the error keep saying 3.10, but in my virtual env I created for the app it shows that python version is * Python version: 3.8.16

Go to your venv and activate it. Then send the command:

streamlit run [your app script]

that what I did I use condo activate my_verenv that I created it for the app and then streamlit run app.py which is the file I wrote my code in and it keep showing python 3.10

The information about your python version is contradictory.
Please share your public github link, otherwise we are poking around in the dark.

I successfully run the regression example from the quick start with streamlit locally. I use python 3.8 on windows 10.

This is my requirements.txt.

alembic==1.9.4
altair==4.2.2
asttokens==2.2.1
attrs==22.2.0
backcall==0.2.0
backports.zoneinfo==0.2.1
blinker==1.5
blis==0.7.9
Boruta==0.3
cachetools==5.3.0
catalogue==1.0.2
certifi==2022.12.7
charset-normalizer==3.0.1
click==8.1.3
cloudpickle==2.2.1
colorama==0.4.6
colorlover==0.3.0
comm==0.1.2
contourpy==1.0.7
cufflinks==0.17.3
cycler==0.11.0
cymem==2.0.7
Cython==0.29.14
databricks-cli==0.17.4
debugpy==1.6.6
decorator==5.1.1
docker==6.0.1
entrypoints==0.4
executing==1.2.0
Flask==2.2.3
fonttools==4.38.0
funcy==1.18
gensim==3.8.3
gitdb==4.0.10
GitPython==3.1.31
greenlet==2.0.2
htmlmin==0.1.12
idna==3.4
ImageHash==4.3.1
imbalanced-learn==0.7.0
importlib-metadata==5.2.0
importlib-resources==5.12.0
ipykernel==6.21.2
ipython==8.10.0
ipywidgets==8.0.4
itsdangerous==2.1.2
jedi==0.18.2
Jinja2==3.1.2
joblib==1.2.0
jsonschema==4.17.3
jupyter-client==8.0.3
jupyter-core==5.2.0
jupyterlab-widgets==3.0.5
kiwisolver==1.4.4
kmodes==0.12.2
lightgbm==3.3.5
llvmlite==0.37.0
Mako==1.2.4
Markdown==3.4.1
markdown-it-py==2.1.0
MarkupSafe==2.1.2
matplotlib==3.7.0
matplotlib-inline==0.1.6
mdurl==0.1.2
mlflow==2.1.1
mlxtend==0.21.0
multimethod==1.9.1
murmurhash==1.0.9
nest-asyncio==1.5.6
networkx==3.0
nltk==3.8.1
numba==0.54.1
numexpr==2.8.4
numpy==1.20.3
oauthlib==3.2.2
packaging==22.0
pandas==1.5.3
pandas-profiling==3.6.6
parso==0.8.3
patsy==0.5.3
phik==0.12.3
pickleshare==0.7.5
Pillow==9.4.0
pkgutil-resolve-name==1.3.10
plac==1.1.3
platformdirs==3.0.0
plotly==5.13.0
preshed==3.0.8
prompt-toolkit==3.0.36
protobuf==3.20.3
psutil==5.9.4
pure-eval==0.2.2
pyarrow==10.0.1
pycaret==2.3.10
pydantic==1.10.5
pydeck==0.8.0
Pygments==2.14.0
PyJWT==2.6.0
pyLDAvis==3.4.0
Pympler==1.0.1
pynndescent==0.5.8
pyod==1.0.7
pyparsing==3.0.9
pyrsistent==0.19.3
python-dateutil==2.8.2
pytz==2022.7.1
pytz-deprecation-shim==0.1.0.post0
PyWavelets==1.4.1
pywin32==305
PyYAML==5.4.1
pyzmq==25.0.0
querystring-parser==1.2.4
regex==2022.10.31
requests==2.28.2
rich==13.3.1
scikit-learn==0.23.2
scikit-plot==0.3.7
scipy==1.5.4
seaborn==0.12.2
semver==2.13.0
shap==0.41.0
six==1.16.0
slicer==0.0.7
smart-open==6.3.0
smmap==5.0.0
spacy==2.3.9
SQLAlchemy==1.4.46
sqlparse==0.4.3
srsly==1.0.6
stack-data==0.6.2
statsmodels==0.13.5
streamlit==1.18.1
tabulate==0.9.0
tangled-up-in-unicode==0.2.0
tenacity==8.2.1
textblob==0.17.1
thinc==7.4.6
threadpoolctl==3.1.0
toml==0.10.2
toolz==0.12.0
tornado==6.2
tqdm==4.64.1
traitlets==5.9.0
typeguard==2.13.3
typing-extensions==4.5.0
tzdata==2022.7
tzlocal==4.2
umap-learn==0.5.3
urllib3==1.26.14
validators==0.20.0
visions==0.7.5
waitress==2.1.2
wasabi==0.10.1
watchdog==2.2.1
wcwidth==0.2.6
websocket-client==1.5.1
Werkzeug==2.2.3
widgetsnbextension==4.0.5
wordcloud==1.8.2.2
ydata-profiling==4.0.0
yellowbrick==1.5
zipp==3.14.0

Those packages are from pycaret==2.3.10 and streamlit 1.18.1.

Important, whenever the package is common to both pycaret and streamlit, follow the pycaret package version. For example, numpy is common, I use numpy==1.20.3 because this is the version that is required by pycaret.

Install the packages in requirements.txt with:

pip install -r requirements.txt

code

main.py

import streamlit as st
from pycaret.datasets import get_data
from pycaret.regression import *


if __name__ == '__main__':
    data = get_data('insurance')
    st.write('#### Data set')
    st.dataframe(data, height=200)

    is_do_regression = st.button('Do regression')

    if is_do_regression:
        # Set silent to true to disable asking questions.
        s = setup(data, target='charges', silent=True)

        best = compare_models()  # get best model
        save_model(best, 'my_best_pipeline')

        st.write('#### Metrics')
        st.dataframe(pull(), height=200)

        # Analyzes the performance of a trained model on the test set.
        # Only available in Notebook.
        # evaluate_model(best)

        # Plots
        plot_model(best, plot='residuals', display_format='streamlit')
        plot_model(best, plot='feature', display_format='streamlit')
        plot_model(best, plot='error', display_format='streamlit')

        # Predicts label on the holdout set.
        pred_holdout = predict_model(best)
        st.write('#### Predictions from holdout set')
        st.dataframe(pred_holdout, height=200)

        # Predicts label on the data.
        # predictions = predict_model(best, data=data)

        # Test the saved model.
        loaded_model = load_model('my_best_pipeline')
        predictions = predict_model(loaded_model, data=data)
        st.write('#### Predictions from data set')
        st.dataframe(predictions, height=200)

pycaret actually supports streamlit plot.

command

streamlit run main.py

Output

image

After pressing the button.

This is a nice package, plotting is cool. Metrics comparison from different models is a nice feature too.

I have not yet tried running it from the cloud.

References

https://pycaret.readthedocs.io/en/latest/index.html

https://www.datacamp.com/tutorial/guide-for-automating-ml-workflows-using-pycaret

2 Likes

I really appreciate your help I will mirror that and keep you post,thanks for spending time on this

Franky It is not public repo it is for my work so that is why could not share…I appreciate your trying to help me…Regards

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.