#!/usr/bin/env python # coding: utf-8 # # PyCaret 2 Classification Example # This notebook is created using PyCaret 2.0. Last updated : 31-07-2020 # In[1]: # check version from pycaret.utils import version version() # # 1. Data Repository # In[2]: from pycaret.datasets import get_data index = get_data('index') # In[4]: data = get_data('juice') # # 2. Initialize Setup # In[5]: from pycaret.classification import * clf1 = setup(data, target = 'Purchase', session_id=123, log_experiment=True, experiment_name='juice1') # # 3. Compare Baseline # In[6]: best_model = compare_models() # # 4. Create Model # In[7]: lr = create_model('lr') # In[8]: dt = create_model('dt') # In[9]: rf = create_model('rf', fold = 5) # In[10]: models() # In[11]: models(type='ensemble').index.tolist() # In[12]: ensembled_models = compare_models(whitelist = models(type='ensemble').index.tolist(), fold = 3) # # 5. Tune Hyperparameters # In[13]: tuned_lr = tune_model(lr) # In[14]: tuned_rf = tune_model(rf) # # 6. Ensemble Model # In[15]: bagged_dt = ensemble_model(dt) # In[16]: boosted_dt = ensemble_model(dt, method = 'Boosting') # # 7. Blend Models # In[17]: blender = blend_models(estimator_list = [boosted_dt, bagged_dt, tuned_rf], method = 'soft') # # 8. Stack Models # In[18]: stacker = stack_models(estimator_list = [boosted_dt,bagged_dt,tuned_rf], meta_model=rf) # # 9. Analyze Model # In[19]: plot_model(rf) # In[20]: plot_model(rf, plot = 'confusion_matrix') # In[21]: plot_model(rf, plot = 'boundary') # In[42]: plot_model(rf, plot = 'feature') # In[43]: plot_model(rf, plot = 'pr') # In[44]: plot_model(rf, plot = 'class_report') # In[22]: evaluate_model(rf) # # 10. Interpret Model # In[23]: catboost = create_model('catboost', cross_validation=False) # In[24]: interpret_model(catboost) # In[25]: interpret_model(catboost, plot = 'correlation') # In[26]: interpret_model(catboost, plot = 'reason', observation = 12) # # 11. AutoML() # In[27]: best = automl(optimize = 'Recall') best # # 12. Predict Model # In[28]: pred_holdouts = predict_model(lr) pred_holdouts.head() # In[30]: new_data = data.copy() new_data.drop(['Purchase'], axis=1, inplace=True) predict_new = predict_model(best, data=new_data) predict_new.head() # # 13. Save / Load Model # In[31]: save_model(best, model_name='best-model') # In[32]: loaded_bestmodel = load_model('best-model') print(loaded_bestmodel) # In[33]: from sklearn import set_config set_config(display='diagram') loaded_bestmodel[0] # In[34]: from sklearn import set_config set_config(display='text') # # 14. Deploy Model # In[35]: deploy_model(best, model_name = 'best-aws', authentication = {'bucket' : 'pycaret-test'}) # # 15. Get Config / Set Config # In[36]: X_train = get_config('X_train') X_train.head() # In[37]: get_config('seed') # In[38]: from pycaret.classification import set_config set_config('seed', 999) # In[39]: get_config('seed') # # 16. MLFlow UI # In[ ]: # !mlflow ui # # End # Thank you. For more information / tutorials on PyCaret, please visit https://www.pycaret.org