#!/usr/bin/env python # coding: utf-8 # # ๐Ÿ‘‰ What is PyCaret? # # PyCaret is an open-source, low-code machine learning library in Python that automates machine learning workflows. It is an end-to-end machine learning and model management tool that speeds up the experiment cycle exponentially and makes you more productive. # # In comparison with the other open-source machine learning libraries, PyCaret is an alternate low-code library that can be used to replace hundreds of lines of code with few words only. This makes experiments exponentially fast and efficient. PyCaret is essentially a Python wrapper around several machine learning libraries and frameworks such as scikit-learn, XGBoost, LightGBM, CatBoost, spaCy, Optuna, Hyperopt, Ray, and many more. # # The design and simplicity of PyCaret is inspired by the emerging role of citizen data scientists, a term first used by Gartner. Citizen Data Scientists are power users who can perform both simple and moderately sophisticated analytical tasks that would previously have required more expertise. Seasoned data scientists are often difficult to find and expensive to hire but citizen data scientists can be an effective way to mitigate this gap and address data-related challenges in the business setting. # # Official Website: https://www.pycaret.org # Documentation: https://pycaret.readthedocs.io/en/latest/ # ![image.png](attachment:image.png) # # ๐Ÿ‘‰ Install PyCaret # Installing PyCaret is very easy and takes only a few minutes. We strongly recommend using a virtual environment to avoid potential conflicts with other libraries. PyCaret's default installation is a slim version of pycaret that only installs hard dependencies that are listed in [requirements.txt](https://github.com/pycaret/pycaret/blob/master/requirements.txt). To install the default version: # # - `pip install pycaret` # # When you install the full version of pycaret, all the optional dependencies as listed [here](https://github.com/pycaret/pycaret/blob/master/requirements-optional.txt) are also installed.To install version: # # - `pip install pycaret[full]` # # ๐Ÿ‘‰Dataset # In[1]: from pycaret.datasets import get_data data = get_data('insurance') # # ๐Ÿ‘‰ Data Preparation # In[2]: from pycaret.regression import * s = setup(data, target = 'charges', session_id = 123) # In[3]: # check transformed X_train get_config('X_train') # In[4]: # list columns of transformed X_train get_config('X_train').columns # # ๐Ÿ‘‰Model Training & Selection # ## Compare Models # In[5]: # train all models using default hyperparameters best = compare_models() # In[6]: print(best) # In[7]: type(best) # ## Create Model # In[8]: # train individual model dt = create_model('dt') # In[9]: print(dt) # ## Tune Hyperparameters # In[14]: get_ipython().run_cell_magic('time', '', '# tune hyperparameters of model\ntuned_dt = tune_model(dt)\n') # In[15]: get_ipython().run_cell_magic('time', '', "# tune hyperparameters of model\ntuned_dt = tune_model(dt, search_library = 'optuna')\n") # In[16]: get_ipython().run_cell_magic('time', '', "# tune hyperparameters of model\ntuned_dt = tune_model(dt, search_library = 'scikit-optimize')\n") # ## Ensemble Model # In[17]: bagged_tunned_dt = ensemble_model(tuned_dt) # In[18]: print(bagged_tunned_dt) # ## Voting Ensemble # In[19]: dt = create_model('dt', verbose=False) lasso = create_model('lasso', verbose=False) knn = create_model('knn', verbose=False) blender = blend_models([dt,lasso,knn]) # In[20]: print(blender) # In[21]: type(blender) # ## Stacking Ensemble # In[22]: stacker = stack_models([dt,lasso,knn]) # In[23]: print(stacker) # # Analyze Model # In[24]: evaluate_model(best) # In[25]: interpret_model(dt) # In[26]: interpret_model(dt, plot = 'reason', observation=1) # # Model Predictions # In[27]: # predict on holdout / test set pred_holdout = predict_model(best); # In[28]: pred_holdout.head() # In[29]: # predict on new data data2 = data.copy() data2.drop('charges', axis=1, inplace=True) data2.head() # In[30]: # finalize model best_final = finalize_model(best) # In[31]: # predict on data2 predictions = predict_model(best_final, data=data2) predictions.head() # # ๐Ÿ‘‰ Save / Load / Deploy Model # In[32]: save_model(best_final, 'insurance-pipeline') # In[33]: loaded_pipeline = load_model('insurance-pipeline') # In[34]: print(loaded_pipeline) # In[35]: # deploy model on AWS S3 deploy_model(best_final, 'insurance-pipeline-aws', platform = 'aws', authentication = {'bucket' : 'pycaret-test'}) # ## THE END # In[ ]: