Supervised learning of tabular data using AutoML

Introduction

Pipeline of activities in a typical machine learning project involves data preprocessing, exploratory data analysis, feature selection/feature engineering, model selection, hyper parameter tuning, generating model explanation and model selection/evaluation. This is an iterative process and data scientists spend a lot of time going through multiple iterations of this pipeline before they are able to identify the best model. AutoML aims to automates this workflow.

arcgis.learn users will now be able to use AutoML for supervised learning classification or regression problems involving tabular data. The AutoML implementation in arcgis.learn builds upon the implementation from MLJar (https://github.com/mljar/mljar-supervised)

Prepare tabular data

Data can be feature layer, spatially enabled dataframe with/without rasters or just a simple dataframe. The data for AutoML is prepared the same way it is prepared for supervised learning ML Models.

%matplotlib inline

from IPython.display import Image, HTML
import arcgis
from arcgis.gis import GIS
from arcgis.learn import prepare_tabulardata,AutoML
from sklearn.preprocessing import MinMaxScaler,RobustScaler

Here we will be taking a feature layer hosted on ArcGIS Online, convert it to a spatially enabled dataframe and prepare the data using prepare_tabulardata method from arcgis.learn. More details about data preparation for ML Models can be found here

gis = GIS('home')
calgary_no_southland_solar = gis.content.search('calgary_no_southland_solar owner:api_data_owner', 'feature layer')[0]
calgary_no_southland_solar
calgary_no_southland_solar

Feature Layer Collection by api_data_owner
Last Modified: October 23, 2020
0 comments, 11,832 views
calgary_no_southland_solar_layer = calgary_no_southland_solar.layers[0]
calgary_no_southland_solar_layer_sdf = calgary_no_southland_solar_layer.query().sdf
calgary_no_southland_solar_layer_sdf=calgary_no_southland_solar_layer_sdf[['FID','date','ID','solar_plan','altitude_m',
                                                                           'latitude','longitude','wind_speed','dayl__s_',
                                                                           'prcp__mm_d','srad__W_m_','swe__kg_m_', 'tmax__deg',
                                                                           'tmin__deg','vp__Pa_','kWh_filled','capacity_f',
                                                                           'SHAPE']]
calgary_no_southland_solar_layer_sdf.head()
FIDdateIDsolar_planaltitude_mlatitudelongitudewind_speeddayl__s_prcp__mm_dsrad__W_m_swe__kg_m_tmax__degtmin__degvp__Pa_kWh_filledcapacity_fSHAPE
012017-12-24355827Glenmore Water Treatment Plant109551.003078-114.1005717.2046727648.01108.80000312-10.5-21.01201.2423570.000177{"x": -12701617.407282012, "y": 6621838.159138...
122017-12-25355827Glenmore Water Treatment Plant109551.003078-114.1005713.38523527648.01115.19999712-18.0-29.5402.4777140.000354{"x": -12701617.407282012, "y": 6621838.159138...
232017-12-26355827Glenmore Water Treatment Plant109551.003078-114.1005715.07631627648.00118.40000212-20.0-32.0403.7130710.00053{"x": -12701617.407282012, "y": 6621838.159138...
342017-12-27355827Glenmore Water Treatment Plant109551.003078-114.1005715.61762327648.0096.012-18.0-26.5804.9484290.000707{"x": -12701617.407282012, "y": 6621838.159138...
452017-12-28355827Glenmore Water Treatment Plant109551.003078-114.1005712.56151227648.00118.40000212-17.0-28.5406.1837860.000883{"x": -12701617.407282012, "y": 6621838.159138...
X = ['altitude_m', 'wind_speed', 'dayl__s_', 'prcp__mm_d','srad__W_m_','swe__kg_m_','tmax__deg','tmin__deg','vp__Pa_']
preprocessors =  [('altitude_m', 'wind_speed', 'dayl__s_', 'prcp__mm_d','srad__W_m_','swe__kg_m_','tmax__deg',
                   'tmin__deg','vp__Pa_', RobustScaler())]
data = prepare_tabulardata(calgary_no_southland_solar_layer,
                           'capacity_f',
                           explanatory_variables=X,                           
                           preprocessors=preprocessors)

Train model using AutoML

from arcgis.learn import AutoML

AutoML class accepts the following paramters:

  • data (Required Paramter): Returned data object from prepare_tabulardata function in the previous step.

  • total_time_limit (Optional parameter): It is the total time in seconds that must be used for AutoML training. Default set is 3600 (1 Hr). At the completion of total_time_limit, the training of AutoML completes and the best model trained until then is used.

  • mode (Optional Parameter): Model can be either Explain. Perform or Compete. Default is Explain.

  • algorithms (Optional Parameter): This parameter takes in list of algorithms as input. The algorithms could be subset of the following: Linear,Decision Tree,Random Forest,Extra Trees,LightGBM,Xgboost,Neural Network.

  • eval_metric (Optional Parameter): The metric to be used to compare models.

AutoML modes

  • Explain : To to be used when you want to explain and understand the data. Uses 75%/25% train/test split. Uses the following models: Baseline, Linear, Decision Tree, Random Forest, XGBoost, Neural Network, and Ensemble. Has full explanations in reports: learning curves, importance plots, and SHAP plots.
  • Perform : To be used when you want to train a model that will be used in real-life use cases. Uses 5-fold CV (Cross-Validation). Uses the following models: Linear, Random Forest, LightGBM, XGBoost,Neural Network, and Ensemble. Has learning curves and importance plots in reports.
  • Compete : To be used for machine learning competitions (maximum performance). Uses 10-fold CV (Cross-Validation). Uses the following models: Decision Tree, Random Forest, Extra Trees, XGBoost, Neural Network, Nearest Neighbors, Ensemble, and Stacking.It has only learning curves in the reports.
AutoML_class_obj = AutoML(data=data)

After creating the AutoML object by passing the data obtained from prepare_tabulardata and using default values for other parameters, now we proceed to train the model using AutoML. This is done by calling the fit method as shown below. New folder will be created and all the models and their varients are saved in that folder.

AutoML_class_obj.fit()
Neural Network algorithm was disabled because it doesn't support n_jobs parameter.
AutoML directory: ~\AppData\Local\Temp\scratch\tmpmbhb97_l
The task is regression with evaluation metric rmse
AutoML will use algorithms: ['Linear', 'Decision Tree', 'Random Trees', 'Extra Trees', 'LightGBM', 'Xgboost']
AutoML will ensemble available models
AutoML steps: ['simple_algorithms', 'default_algorithms', 'ensemble']
* Step simple_algorithms will try to check up to 2 models
1_DecisionTree rmse 0.048893 trained in 3.63 seconds
Exception while producing SHAP explanations. 'float' object has no attribute 'shape'
Continuing ...
2_Linear rmse 0.046288 trained in 1.1 seconds
* Step default_algorithms will try to check up to 4 models
3_Default_LightGBM rmse 0.028985 trained in 7.23 seconds
There was an error during 4_Default_Xgboost training.
Please check ~\AppData\Local\Temp\scratch\tmpmbhb97_l\errors.md for details.
5_Default_RandomTrees rmse 0.043642 trained in 4.5 seconds
6_Default_ExtraTrees rmse 0.044894 trained in 6.55 seconds
* Step ensemble will try to check up to 1 model
Ensemble rmse 0.028985 trained in 0.28 seconds
AutoML fit time: 32.35 seconds
AutoML best model: 3_Default_LightGBM
All the evaluated models are saved in the path  ~\AppData\Local\Temp\scratch\tmpmbhb97_l

Once the best model is identified after the completion of fit method, the model is then saved by calling the save method. The transforms and the encoders used on the training data, along with the Esri Model Definition (EMD) file and the dlpk is then saved in the path specified by the user.

AutoML_class_obj.save('AutoML_class_obj')

We can get the score of the best model, visualize the results on validation dataset and also get predictions on new data using the corresponding methods shown below.

AutoML_class_obj.score()
0.962025310798984
AutoML_class_obj.show_results()
altitude_mcapacity_fdayl__s_prcp__mm_dsrad__W_m_swe__kg_m_tmax__degtmin__degvp__Pa_wind_speedcapacity_f_results
148910550.01955529376.0096.00-4.5-12.02405.8191280.017498
350211120.25301553568.00473.600006024.58.06805.0978130.019465
430410700.24806150112.00422.399994029.07.58003.7336510.021266
549110900.01859734905.6015620265.600006283.0-14.52008.4353820.016190
767910960.11201544582.3984380288.0022.510.512804.8868890.015759
AutoML_class_obj.predict(data._dataframe.iloc[:100][X],prediction_type="dataframe")
altitude_mwind_speeddayl__s_prcp__mm_dsrad__W_m_swe__kg_m_tmax__degtmin__degvp__Pa_prediction_results
010957.2046727648.01108.80000312-10.5-21.0120-0.002004
110953.38523527648.01115.19999712-18.0-29.5400.000117
210955.07631627648.00118.40000212-20.0-32.0400.001523
310955.61762327648.0096.012-18.0-26.5800.000313
410952.56151227648.00118.40000212-17.0-28.5400.000577
.................................
9510953.88743950457.6015620419.200012026.08.010800.218459
9610955.68098150457.6015620435.200012031.510.012400.229766
9710955.33244750112.03195.199997021.012.014000.131819
9810955.28169149766.3984387288.0024.59.011600.130322
9910955.03377549766.3984381384.0024.08.511200.182289

100 rows × 10 columns

As in the case of MLModel and FullyConnectedNetwork, predictions can also be obtained in the form of feature class and rasters.

Additionally it is also possible to generate and view the report of all the models trained by the AutoML, the performance and the hyperparamters used in each of the model variant. The report can be generated using the report method as shown below. The reports, also show the learning curves and feature importance charts for each of the model evaluated during the training.

AutoML_class_obj.report()

report.PNG

Reload trained model for prediction

The trained AutoML can be reloaded from the disk to get the predictions on new data. This is done using from_model method. This method takes in the path to the emd file as the input. The best model that was identified using the training phase will automatically be picked up and the prediction can be done on the new data using this model by calling the predict method.

from arcgis.learn import AutoML
AutoML_test_reload=AutoML.from_model(r'AutoML_class_obj')
AutoML_test_reload.predict(data._dataframe.iloc[:100],prediction_type="dataframe")
altitude_mcapacity_fdayl__s_prcp__mm_dsrad__W_m_swe__kg_m_tmax__degtmin__degvp__Pa_wind_speedprediction_results
010950.00017727648.01108.80000312-10.5-21.01207.20467-0.002004
110950.00035427648.01115.19999712-18.0-29.5403.3852350.000117
210950.0005327648.00118.40000212-20.0-32.0405.0763160.001523
310950.00070727648.0096.012-18.0-26.5805.6176230.000313
410950.00088327648.00118.40000212-17.0-28.5402.5615120.000577
....................................
9510950.24247250457.6015620419.200012026.08.010803.8874390.218459
9610950.22679550457.6015620435.200012031.510.012405.6809810.229766
9710950.13603350112.03195.199997021.012.014005.3324470.131819
9810950.13969949766.3984387288.0024.59.011605.2816910.130322
9910950.19006649766.3984381384.0024.08.511205.0337750.182289

100 rows × 11 columns

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.