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_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.20467027648.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.000530{"x": -12701617.407282012, "y": 6621838.159138...
342017-12-27355827Glenmore Water Treatment Plant109551.003078-114.1005715.61762327648.0096.00000012-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()
AutoML directory: AutoML_20
The task is regression with evaluation metric rmse
AutoML will use algorithms: ['Linear', 'Decision Tree', 'Random Forest', 'Extra Trees', 'LightGBM', 'Xgboost', 'Neural Network']
AutoML will ensemble availabe models
AutoML steps: ['simple_algorithms', 'default_algorithms', 'ensemble']
* Step simple_algorithms will try to check up to 2 models
1_DecisionTree rmse 0.050619 trained in 14.53 seconds
2_Linear rmse 0.048289 trained in 6.58 seconds
* Step default_algorithms will try to check up to 5 models
3_Default_LightGBM rmse 0.032221 trained in 9.8 seconds
4_Default_Xgboost rmse 0.033514 trained in 10.93 seconds
5_Default_NeuralNetwork rmse 0.043816 trained in 1.96 seconds
6_Default_RandomForest rmse 0.045735 trained in 3.66 seconds
7_Default_ExtraTrees rmse 0.046594 trained in 3.48 seconds
* Step ensemble will try to check up to 1 model
Ensemble rmse 0.032221 trained in 0.17 seconds
AutoML fit time: 56.1 seconds
AutoML best model: 3_Default_LightGBM
Model has been saved in the path C:\Users\Karthik\Desktop\Base\AutoML\AutoML_20

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('<Pass the path>')

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.8923446019885948
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
326910950.00046439052.8007815182.39999441.5-7.53608.5568390.005607
542510900.01449327993.5996090115.199997014.0-1.556010.9645770.019452
592710700.28804458752.0000002470.399994023.06.55603.3395480.314754
742810940.08343058752.00000023201.600006020.512.513606.2241270.103236
890010510.00717141126.3984381141.5999980-1.5-3.04807.412553-0.002827
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.20467027648.0000001108.80000312-10.5-21.0120-0.001710
110953.38523527648.0000001115.19999712-18.0-29.5400.000350
210955.07631627648.0000000118.40000212-20.0-32.040-0.000757
310955.61762327648.000000096.00000012-18.0-26.580-0.000332
410952.56151227648.0000000118.40000212-17.0-28.540-0.000332
.................................
9510953.88743950457.6015620419.200012026.08.010800.219132
9610955.68098150457.6015620435.200012031.510.012400.189853
9710955.33244750112.0000003195.199997021.012.014000.140498
9810955.28169149766.3984387288.000000024.59.011600.103281
9910955.03377549766.3984381384.000000024.08.511200.166501

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'<Path to the emd file created after saving the model>')
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.0000001108.80000312-10.5-21.01207.204670-0.001710
110950.00035427648.0000001115.19999712-18.0-29.5403.3852350.000350
210950.00053027648.0000000118.40000212-20.0-32.0405.076316-0.000757
310950.00070727648.000000096.00000012-18.0-26.5805.617623-0.000332
410950.00088327648.0000000118.40000212-17.0-28.5402.561512-0.000332
....................................
9510950.24247250457.6015620419.200012026.08.010803.8874390.219132
9610950.22679550457.6015620435.200012031.510.012405.6809810.189853
9710950.13603350112.0000003195.199997021.012.014005.3324470.140498
9810950.13969949766.3984387288.000000024.59.011605.2816910.103281
9910950.19006649766.3984381384.000000024.08.511205.0337750.166501

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.