ai-data-science-team 0.0.0.9009__py3-none-any.whl → 0.0.0.9010__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. ai_data_science_team/_version.py +1 -1
  2. ai_data_science_team/agents/data_cleaning_agent.py +6 -6
  3. ai_data_science_team/agents/data_loader_tools_agent.py +69 -0
  4. ai_data_science_team/agents/data_visualization_agent.py +6 -7
  5. ai_data_science_team/agents/data_wrangling_agent.py +6 -6
  6. ai_data_science_team/agents/feature_engineering_agent.py +6 -6
  7. ai_data_science_team/agents/sql_database_agent.py +6 -6
  8. ai_data_science_team/ml_agents/__init__.py +1 -0
  9. ai_data_science_team/ml_agents/h2o_ml_agent.py +205 -385
  10. ai_data_science_team/ml_agents/mlflow_tools_agent.py +327 -0
  11. ai_data_science_team/multiagents/sql_data_analyst.py +3 -4
  12. ai_data_science_team/parsers/__init__.py +0 -0
  13. ai_data_science_team/{tools → parsers}/parsers.py +0 -1
  14. ai_data_science_team/templates/agent_templates.py +6 -6
  15. ai_data_science_team/tools/data_loader.py +378 -0
  16. ai_data_science_team/tools/dataframe.py +139 -0
  17. ai_data_science_team/tools/h2o.py +643 -0
  18. ai_data_science_team/tools/mlflow.py +961 -0
  19. ai_data_science_team/tools/{metadata.py → sql.py} +1 -137
  20. {ai_data_science_team-0.0.0.9009.dist-info → ai_data_science_team-0.0.0.9010.dist-info}/METADATA +34 -16
  21. ai_data_science_team-0.0.0.9010.dist-info/RECORD +35 -0
  22. ai_data_science_team-0.0.0.9009.dist-info/RECORD +0 -28
  23. /ai_data_science_team/{tools → utils}/logging.py +0 -0
  24. /ai_data_science_team/{tools → utils}/regex.py +0 -0
  25. {ai_data_science_team-0.0.0.9009.dist-info → ai_data_science_team-0.0.0.9010.dist-info}/LICENSE +0 -0
  26. {ai_data_science_team-0.0.0.9009.dist-info → ai_data_science_team-0.0.0.9010.dist-info}/WHEEL +0 -0
  27. {ai_data_science_team-0.0.0.9009.dist-info → ai_data_science_team-0.0.0.9010.dist-info}/top_level.txt +0 -0
@@ -25,16 +25,17 @@ from ai_data_science_team.templates import(
25
25
  create_coding_agent_graph,
26
26
  BaseAgent,
27
27
  )
28
- from ai_data_science_team.tools.parsers import PythonOutputParser
29
- from ai_data_science_team.tools.regex import (
28
+ from ai_data_science_team.parsers.parsers import PythonOutputParser
29
+ from ai_data_science_team.utils.regex import (
30
30
  relocate_imports_inside_function,
31
31
  add_comments_to_top,
32
32
  format_agent_name,
33
33
  format_recommended_steps,
34
34
  get_generic_summary,
35
35
  )
36
- from ai_data_science_team.tools.metadata import get_dataframe_summary
37
- from ai_data_science_team.tools.logging import log_ai_function
36
+ from ai_data_science_team.tools.dataframe import get_dataframe_summary
37
+ from ai_data_science_team.utils.logging import log_ai_function
38
+ from ai_data_science_team.tools.h2o import H2O_AUTOML_DOCUMENTATION
38
39
 
39
40
  AGENT_NAME = "h2o_ml_agent"
40
41
  LOG_PATH = os.path.join(os.getcwd(), "logs/")
@@ -60,7 +61,7 @@ class H2OMLAgent(BaseAgent):
60
61
  function_name : str, optional
61
62
  Name of the function that performs the AutoML training. Defaults to "h2o_automl".
62
63
  model_directory : str or None, optional
63
- Directory to save the model. If None, defaults to log_path (if available).
64
+ Directory to save the H2O Machine Learning model. If None, defaults to log_path (if available).
64
65
  If both are None, no model is saved. Defaults to None.
65
66
  overwrite : bool, optional
66
67
  Whether to overwrite the log file if it exists. Defaults to True.
@@ -70,7 +71,16 @@ class H2OMLAgent(BaseAgent):
70
71
  If True, skips the recommended steps prompt. Defaults to False.
71
72
  bypass_explain_code : bool, optional
72
73
  If True, skips the code-explanation step. Defaults to False.
73
-
74
+ enable_mlflow : bool, default False
75
+ Whether to enable MLflow logging. If False, skip MLflow entirely.
76
+ mlflow_tracking_uri : str or None
77
+ If provided, sets MLflow tracking URI at runtime.
78
+ mlflow_experiment_name : str
79
+ Name of the MLflow experiment (created if doesn't exist).
80
+ mlflow_run_name : str, default None
81
+ A custom name for the MLflow run.
82
+
83
+
74
84
  Methods
75
85
  -------
76
86
  update_params(**kwargs)
@@ -157,11 +167,15 @@ class H2OMLAgent(BaseAgent):
157
167
  log_path=None,
158
168
  file_name="h2o_automl.py",
159
169
  function_name="h2o_automl",
160
- model_directory=None, # New
170
+ model_directory=None,
161
171
  overwrite=True,
162
172
  human_in_the_loop=False,
163
173
  bypass_recommended_steps=False,
164
- bypass_explain_code=False
174
+ bypass_explain_code=False,
175
+ enable_mlflow=False,
176
+ mlflow_tracking_uri=None,
177
+ mlflow_experiment_name="H2O AutoML",
178
+ mlflow_run_name=None,
165
179
  ):
166
180
  self._params = {
167
181
  "model": model,
@@ -174,14 +188,18 @@ class H2OMLAgent(BaseAgent):
174
188
  "overwrite": overwrite,
175
189
  "human_in_the_loop": human_in_the_loop,
176
190
  "bypass_recommended_steps": bypass_recommended_steps,
177
- "bypass_explain_code": bypass_explain_code
191
+ "bypass_explain_code": bypass_explain_code,
192
+ "enable_mlflow": enable_mlflow,
193
+ "mlflow_tracking_uri": mlflow_tracking_uri,
194
+ "mlflow_experiment_name": mlflow_experiment_name,
195
+ "mlflow_run_name": mlflow_run_name,
178
196
  }
179
197
  self._compiled_graph = self._make_compiled_graph()
180
198
  self.response = None
181
199
 
182
200
  def _make_compiled_graph(self):
183
201
  """
184
- Creates the compiled graph for the H2O ML agent.
202
+ Creates the compiled graph for the agent.
185
203
  """
186
204
  self.response = None
187
205
  return make_h2o_ml_agent(**self._params)
@@ -194,7 +212,7 @@ class H2OMLAgent(BaseAgent):
194
212
  self._params[k] = v
195
213
  self._compiled_graph = self._make_compiled_graph()
196
214
 
197
- def ainvoke_agent(
215
+ async def ainvoke_agent(
198
216
  self,
199
217
  data_raw: pd.DataFrame,
200
218
  user_instructions: str=None,
@@ -207,7 +225,7 @@ class H2OMLAgent(BaseAgent):
207
225
  Asynchronously trains an H2O AutoML model for the provided dataset,
208
226
  saving the best model to disk if model_directory or log_path is available.
209
227
  """
210
- response = self._compiled_graph.ainvoke({
228
+ response = await self._compiled_graph.ainvoke({
211
229
  "user_instructions": user_instructions,
212
230
  "data_raw": data_raw.to_dict(),
213
231
  "target_variable": target_variable,
@@ -328,6 +346,10 @@ def make_h2o_ml_agent(
328
346
  human_in_the_loop=False,
329
347
  bypass_recommended_steps=False,
330
348
  bypass_explain_code=False,
349
+ enable_mlflow=False,
350
+ mlflow_tracking_uri=None,
351
+ mlflow_experiment_name="H2O AutoML",
352
+ mlflow_run_name=None,
331
353
  ):
332
354
  """
333
355
  Creates a machine learning agent that uses H2O for AutoML.
@@ -372,6 +394,7 @@ def make_h2o_ml_agent(
372
394
  leaderboard: dict
373
395
  best_model_id: str
374
396
  model_path: str
397
+ model_results: dict
375
398
  target_variable: str
376
399
  all_datasets_summary: str
377
400
  h2o_train_function: str
@@ -464,12 +487,13 @@ def make_h2o_ml_agent(
464
487
  We have two variables for deciding where to save the model:
465
488
  model_directory = {model_directory}
466
489
  log_path = {log_path}
490
+
491
+ IMPORTANT: MLflow Parameters if the user wants to enable MLflow with H2O AutoML:
492
+ enable_mlflow: {enable_mlflow}
493
+ mlflow_tracking_uri: {mlflow_tracking_uri}
494
+ mlflow_experiment_name: {mlflow_experiment_name}
495
+ mlflow_run_name: {mlflow_run_name}
467
496
 
468
- Logic:
469
- 1. If both model_directory and log_path are None, do NOT save the model (set model_path = None).
470
- 2. Otherwise, pick model_directory if it's not None, else pick log_path.
471
- Then call `h2o.save_model(model=aml.leader, path=the_directory, force=True)` to save the model.
472
- 3. Return model_path as part of the final dictionary.
473
497
 
474
498
  Additional Requirements:
475
499
  - Convert `data_raw` (pandas DataFrame) into an H2OFrame.
@@ -480,7 +504,8 @@ def make_h2o_ml_agent(
480
504
  - If the user does not specify anything special, use H2OAutoML defaults (including stacked ensembles).
481
505
  - Focus on maximizing accuracy (or the most relevant metric if it's not classification)
482
506
  while remaining flexible to user instructions.
483
- - Return a dict with keys: leaderboard, best_model_id, and model_path.
507
+ - Return a dict with keys: leaderboard, best_model_id, model_path, and model_results.
508
+ - If enable_mlfow is True, log the top metrics and save the model as an artifact. (See example function)
484
509
 
485
510
  Initial User Instructions (Disregard any instructions that are unrelated to modeling):
486
511
  {user_instructions}
@@ -493,44 +518,160 @@ def make_h2o_ml_agent(
493
518
 
494
519
  Return only code in ```python``` with a single function definition. Use this as an example starting template:
495
520
  ```python
496
- def {function_name}(data_raw):
521
+ def {function_name}(
522
+ data_raw: List[Dict[str, Any]],
523
+ target: str,
524
+ max_runtime_secs: int,
525
+ exclude_algos: List[str],
526
+ balance_classes: bool,
527
+ nfolds: int,
528
+ seed: int,
529
+ max_models: int,
530
+ stopping_metric: str,
531
+ stopping_tolerance: float,
532
+ stopping_rounds: int,
533
+ sort_metric: str ,
534
+ model_directory: Optional[str] = None,
535
+ log_path: Optional[str] = None,
536
+ enable_mlflow: bool,
537
+ mlflow_tracking_uri: Optional[str],
538
+ mlflow_experiment_name: str,
539
+ mlflow_run_name: str,
540
+ **kwargs # Additional parameters for H2OAutoML (feel free to add these based on user instructions and recommended steps)
541
+ ):
542
+
497
543
  import h2o
498
544
  from h2o.automl import H2OAutoML
499
545
  import pandas as pd
500
-
501
- # Initialize or connect to H2O if not already started
502
- h2o.init()
503
-
504
- # Convert the pandas DataFrame to an H2OFrame
505
- data_h2o = h2o.H2OFrame(data_raw)
506
-
507
- # Identify the target variable
508
- target = {target_variable}
509
- x = [col for col in data_h2o.columns if col != target]
510
-
511
- # Example: Use advanced parameters if recommended (e.g., nfolds, max_runtime_secs, etc.)
512
- # Adjust them based on user instructions and recommended steps:
513
- aml = H2OAutoML(
514
- max_runtime_secs=60, # default if no user instructions override
515
- seed=42,
516
- nfolds=5, # default if no user instructions override
517
- # e.g., balance_classes=True, etc. if recommended
518
- )
519
- aml.train(x=x, y=target, training_frame=data_h2o)
520
-
521
- # Determine model saving logic
522
- if {model_directory} is None and {log_path} is None:
523
- model_path = None
546
+ import json
547
+
548
+ # Optional MLflow usage
549
+ if enable_mlflow:
550
+ import mlflow
551
+ if mlflow_tracking_uri:
552
+ mlflow.set_tracking_uri(mlflow_tracking_uri)
553
+ mlflow.set_experiment(mlflow_experiment_name)
554
+ run_context = mlflow.start_run(run_name=mlflow_run_name)
524
555
  else:
525
- path_to_save = {model_directory} if {model_directory} else {log_path}
526
- model_path = h2o.save_model(model=aml.leader, path=path_to_save, force=True)
527
-
528
- return dict(
529
- leaderboard = h2o.automl.get_leaderboard(aml, extra_columns="ALL").as_data_frame().to_dict(),
530
- best_model_id = aml.leader.model_id,
531
- model_path = model_path,
532
- )
556
+ # Dummy context manager to skip MLflow if not enabled
557
+ from contextlib import nullcontext
558
+ run_context = nullcontext()
559
+
560
+ exclude_algos = exclude_algos or ["DeepLearning"] # default if not provided
561
+
562
+ # Convert data to DataFrame
563
+ df = pd.DataFrame(data_raw)
564
+
565
+ with run_context as run:
566
+ # If using MLflow, track run ID
567
+ run_id = None
568
+ if enable_mlflow and run is not None:
569
+ run_id = run.info.run_id
570
+ import mlflow
571
+
572
+
573
+ # Initialize H2O
574
+ h2o.init()
575
+
576
+ # Create H2OFrame
577
+ data_h2o = h2o.H2OFrame(df)
578
+
579
+ # Setup AutoML
580
+ aml = H2OAutoML(
581
+ max_runtime_secs=max_runtime_secs,
582
+ exclude_algos=exclude_algos,
583
+ balance_classes=balance_classes,
584
+ nfolds=nfolds,
585
+ seed=seed,
586
+ max_models=max_models,
587
+ stopping_metric=stopping_metric,
588
+ stopping_tolerance=stopping_tolerance,
589
+ stopping_rounds=stopping_rounds,
590
+ sort_metric=sort_metric,
591
+ **kwargs
592
+ )
593
+
594
+ # Train
595
+ x = [col for col in data_h2o.columns if col != target]
596
+ aml.train(x=x, y=target, training_frame=data_h2o)
597
+
598
+ # Save model if we have a directory/log path
599
+ if model_directory is None and log_path is None:
600
+ model_path = None
601
+ else:
602
+ path_to_save = model_directory if model_directory else log_path
603
+ model_path = h2o.save_model(model=aml.leader, path=path_to_save, force=True)
604
+
605
+ # Leaderboard (DataFrame -> dict)
606
+ leaderboard_df = pd.DataFrame(aml.leaderboard)
607
+ leaderboard_dict = leaderboard_df.to_dict()
608
+
609
+ # Gather top-model metrics from the first row
610
+ top_metrics = leaderboard_df.iloc[0].to_dict()
611
+
612
+ # Construct model_results
613
+ model_results = dict(
614
+ model_flavor= "H2O AutoML",
615
+ model_path= model_path,
616
+ best_model_id= aml.leader.model_id,
617
+ metrics= top_metrics # all metrics from the top row
618
+ )
619
+
620
+ # IMPORTANT: Log these to MLflow if enabled
621
+ if enable_mlflow and run is not None:
622
+
623
+ # Log the top metrics if numeric
624
+ numeric_metrics = {{k: v for k, v in top_metrics.items() if isinstance(v, (int, float))}}
625
+ mlflow.log_metrics(numeric_metrics)
626
+
627
+ # Log artifact if we saved the model
628
+ mlflow.h2o.log_model(aml.leader, artifact_path="model")
629
+
630
+ # Log the leaderboard
631
+ mlflow.log_table(leaderboard_dict, "leaderboard.json")
632
+
633
+ # Log these parameters (if specified)
634
+ mlflow.log_params(dict(
635
+ target= target,
636
+ max_runtime_secs= max_runtime_secs,
637
+ exclude_algos= str(exclude_algos),
638
+ balance_classes= balance_classes,
639
+ nfolds= nfolds,
640
+ seed= seed,
641
+ max_models= max_models,
642
+ stopping_metric= stopping_metric,
643
+ stopping_tolerance= stopping_tolerance,
644
+ stopping_rounds= stopping_rounds,
645
+ sort_metric= sort_metric,
646
+ model_directory= model_directory,
647
+ log_path= log_path
648
+ ))
649
+
650
+ # Build the output
651
+ output = dict(
652
+ leaderboard= leaderboard_dict,
653
+ best_model_id= aml.leader.model_id,
654
+ model_path= model_path,
655
+ model_results= model_results,
656
+ mlflow_run_id= run_id
657
+ )
658
+
659
+ return output
533
660
  ```
661
+
662
+ Avoid these errors:
663
+
664
+ - WARNING mlflow.models.model: Model logged without a signature and input example. Please set `input_example` parameter when logging the model to auto infer the model signature.
665
+
666
+ - 'list' object has no attribute 'tolist'
667
+
668
+ - with h2o.utils.threading.local_context(polars_enabled=True, datatable_enabled=True): pandas_df = h2o_df.as_data_frame() # Convert to pandas DataFrame using pd.DataFrame(h2o_df)
669
+
670
+ - dtype is only supported for one column frames
671
+
672
+ - h2o.is_running() module 'h2o' has no attribute 'is_running'. Solution: just do h2o.init() and it will check if H2O is running.
673
+
674
+
534
675
  """,
535
676
  input_variables=[
536
677
  "user_instructions",
@@ -539,7 +680,11 @@ def make_h2o_ml_agent(
539
680
  "recommended_steps",
540
681
  "all_datasets_summary",
541
682
  "model_directory",
542
- "log_path"
683
+ "log_path",
684
+ "enable_mlflow",
685
+ "mlflow_tracking_uri",
686
+ "mlflow_experiment_name",
687
+ "mlflow_run_name",
543
688
  ]
544
689
  )
545
690
 
@@ -553,7 +698,11 @@ def make_h2o_ml_agent(
553
698
  "recommended_steps": recommended_steps,
554
699
  "all_datasets_summary": all_datasets_summary_str,
555
700
  "model_directory": model_directory,
556
- "log_path": log_path
701
+ "log_path": log_path,
702
+ "enable_mlflow": enable_mlflow,
703
+ "mlflow_tracking_uri": mlflow_tracking_uri,
704
+ "mlflow_experiment_name": mlflow_experiment_name,
705
+ "mlflow_run_name": mlflow_run_name,
557
706
  })
558
707
 
559
708
  resp = relocate_imports_inside_function(resp)
@@ -572,7 +721,7 @@ def make_h2o_ml_agent(
572
721
  "h2o_train_function": resp,
573
722
  "h2o_train_function_path": file_path,
574
723
  "h2o_train_file_name": f_name,
575
- "h2o_train_function_name": function_name
724
+ "h2o_train_function_name": function_name,
576
725
  }
577
726
 
578
727
  # Human Review
@@ -621,10 +770,12 @@ def make_h2o_ml_agent(
621
770
  lb = result["h2o_train_result"].get("leaderboard", {})
622
771
  best_id = result["h2o_train_result"].get("best_model_id", None)
623
772
  mpath = result["h2o_train_result"].get("model_path", None)
773
+ model_results = result["h2o_train_result"].get("model_results", {})
624
774
 
625
775
  result["leaderboard"] = lb
626
776
  result["best_model_id"] = best_id
627
777
  result["model_path"] = mpath
778
+ result["model_results"] = model_results
628
779
 
629
780
  return result
630
781
 
@@ -699,334 +850,3 @@ def make_h2o_ml_agent(
699
850
 
700
851
  return app
701
852
 
702
- H2O_AUTOML_DOCUMENTATION = """
703
- Title: H2O AutoML: Automatic Machine Learning
704
- Source: https://docs.h2o.ai/h2o/latest-stable/h2o-docs/automl.html
705
-
706
- AutoML Interface
707
- The H2O AutoML interface is designed to have as few parameters as possible so that all the user needs to do is point to their dataset, identify the response column and optionally specify a time constraint or limit on the number of total models trained. Below are the parameters that can be set by the user in the R and Python interfaces. See the Web UI via H2O Wave section below for information on how to use the H2O Wave web interface for AutoML.
708
-
709
- In both the R and Python API, AutoML uses the same data-related arguments, x, y, training_frame, validation_frame, as the other H2O algorithms. Most of the time, all you'll need to do is specify the data arguments. You can then configure values for max_runtime_secs and/or max_models to set explicit time or number-of-model limits on your run.
710
-
711
- Required Parameters
712
- Required Data Parameters
713
- y: This argument is the name (or index) of the response column.
714
-
715
- training_frame: Specifies the training set.
716
-
717
- Required Stopping Parameters
718
- One of the following stopping strategies (time or number-of-model based) must be specified. When both options are set, then the AutoML run will stop as soon as it hits one of either When both options are set, then the AutoML run will stop as soon as it hits either of these limits.
719
-
720
- max_runtime_secs: This argument specifies the maximum time that the AutoML process will run for. The default is 0 (no limit), but dynamically sets to 1 hour if none of max_runtime_secs and max_models are specified by the user.
721
-
722
- max_models: Specify the maximum number of models to build in an AutoML run, excluding the Stacked Ensemble models. Defaults to NULL/None. Always set this parameter to ensure AutoML reproducibility: all models are then trained until convergence and none is constrained by a time budget.
723
-
724
- Optional Parameters
725
- Optional Data Parameters
726
- x: A list/vector of predictor column names or indexes. This argument only needs to be specified if the user wants to exclude columns from the set of predictors. If all columns (other than the response) should be used in prediction, then this does not need to be set.
727
-
728
- validation_frame: This argument is ignored unless nfolds == 0, in which a validation frame can be specified and used for early stopping of individual models and early stopping of the grid searches (unless max_models or max_runtime_secs overrides metric-based early stopping). By default and when nfolds > 1, cross-validation metrics will be used for early stopping and thus validation_frame will be ignored.
729
-
730
- leaderboard_frame: This argument allows the user to specify a particular data frame to use to score and rank models on the leaderboard. This frame will not be used for anything besides leaderboard scoring. If a leaderboard frame is not specified by the user, then the leaderboard will use cross-validation metrics instead, or if cross-validation is turned off by setting nfolds = 0, then a leaderboard frame will be generated automatically from the training frame.
731
-
732
- blending_frame: Specifies a frame to be used for computing the predictions that serve as the training frame for the Stacked Ensemble models metalearner. If provided, all Stacked Ensembles produced by AutoML will be trained using Blending (a.k.a. Holdout Stacking) instead of the default Stacking method based on cross-validation.
733
-
734
- fold_column: Specifies a column with cross-validation fold index assignment per observation. This is used to override the default, randomized, 5-fold cross-validation scheme for individual models in the AutoML run.
735
-
736
- weights_column: Specifies a column with observation weights. Giving some observation a weight of zero is equivalent to excluding it from the dataset; giving an observation a relative weight of 2 is equivalent to repeating that row twice. Negative weights are not allowed.
737
-
738
- Optional Miscellaneous Parameters
739
- nfolds: Specify a value >= 2 for the number of folds for k-fold cross-validation of the models in the AutoML run or specify “-1” to let AutoML choose if k-fold cross-validation or blending mode should be used. Blending mode will use part of training_frame (if no blending_frame is provided) to train Stacked Ensembles. Use 0 to disable cross-validation; this will also disable Stacked Ensembles (thus decreasing the overall best model performance). This value defaults to “-1”.
740
-
741
- balance_classes: Specify whether to oversample the minority classes to balance the class distribution. This option is not enabled by default and can increase the data frame size. This option is only applicable for classification. If the oversampled size of the dataset exceeds the maximum size calculated using the max_after_balance_size parameter, then the majority classes will be undersampled to satisfy the size limit.
742
-
743
- class_sampling_factors: Specify the per-class (in lexicographical order) over/under-sampling ratios. By default, these ratios are automatically computed during training to obtain the class balance. Note that this requires balance_classes set to True.
744
-
745
- max_after_balance_size: Specify the maximum relative size of the training data after balancing class counts (balance_classes must be enabled). Defaults to 5.0. (The value can be less than 1.0).
746
-
747
- max_runtime_secs_per_model: Specify the max amount of time dedicated to the training of each individual model in the AutoML run. Defaults to 0 (disabled). Note that models constrained by a time budget are not guaranteed reproducible.
748
-
749
- stopping_metric: Specify the metric to use for early stopping. Defaults to AUTO. The available options are:
750
-
751
- - AUTO: This defaults to logloss for classification and deviance for regression.
752
- - deviance (mean residual deviance)
753
- - logloss
754
- - MSE
755
- - RMSE
756
- - MAE
757
- - RMSLE
758
- - AUC (area under the ROC curve)
759
- - AUCPR (area under the Precision-Recall curve)
760
- - lift_top_group
761
- - misclassification
762
- - mean_per_class_error
763
-
764
- stopping_tolerance: This option specifies the relative tolerance for the metric-based stopping criterion to stop a grid search and the training of individual models within the AutoML run. This value defaults to 0.001 if the dataset is at least 1 million rows; otherwise it defaults to a bigger value determined by the size of the dataset and the non-NA-rate. In that case, the value is computed as 1/sqrt(nrows * non-NA-rate).
765
-
766
- stopping_rounds: This argument is used to stop model training when the stopping metric (e.g. AUC) doesn't improve for this specified number of training rounds, based on a simple moving average. In the context of AutoML, this controls early stopping both within the random grid searches as well as the individual models. Defaults to 3 and must be an non-negative integer. To disable early stopping altogether, set this to 0.
767
-
768
- sort_metric: Specifies the metric used to sort the Leaderboard by at the end of an AutoML run. Available options include:
769
-
770
- - AUTO: This defaults to AUC for binary classification, mean_per_class_error for multinomial classification, and deviance for regression.
771
- - deviance (mean residual deviance)
772
- - logloss
773
- - MSE
774
- - RMSE
775
- - MAE
776
- - RMSLE
777
- - AUC (area under the ROC curve)
778
- - AUCPR (area under the Precision-Recall curve)
779
- - mean_per_class_error
780
-
781
- seed: Integer. Set a seed for reproducibility. AutoML can only guarantee reproducibility under certain conditions. H2O Deep Learning models are not reproducible by default for performance reasons, so if the user requires reproducibility, then exclude_algos must contain "DeepLearning". In addition max_models must be used because max_runtime_secs is resource limited, meaning that if the available compute resources are not the same between runs, AutoML may be able to train more models on one run vs another. Defaults to NULL/None.
782
-
783
- project_name: Character string to identify an AutoML project. Defaults to NULL/None, which means a project name will be auto-generated based on the training frame ID. More models can be trained and added to an existing AutoML project by specifying the same project name in multiple calls to the AutoML function (as long as the same training frame is used in subsequent runs).
784
-
785
- exclude_algos: A list/vector of character strings naming the algorithms to skip during the model-building phase. An example use is exclude_algos = ["GLM", "DeepLearning", "DRF"] in Python or exclude_algos = c("GLM", "DeepLearning", "DRF") in R. Defaults to None/NULL, which means that all appropriate H2O algorithms will be used if the search stopping criteria allows and if the include_algos option is not specified. This option is mutually exclusive with include_algos. See include_algos below for the list of available options.
786
-
787
- include_algos: A list/vector of character strings naming the algorithms to include during the model-building phase. An example use is include_algos = ["GLM", "DeepLearning", "DRF"] in Python or include_algos = c("GLM", "DeepLearning", "DRF") in R. Defaults to None/NULL, which means that all appropriate H2O algorithms will be used if the search stopping criteria allows and if no algorithms are specified in exclude_algos. This option is mutually exclusive with exclude_algos. The available algorithms are:
788
-
789
- - DRF (This includes both the Distributed Random Forest (DRF) and Extremely Randomized Trees (XRT) models. Refer to the Extremely Randomized Trees section in the DRF chapter and the histogram_type parameter description for more information.)
790
- - GLM (Generalized Linear Model with regularization)
791
- - XGBoost (XGBoost GBM)
792
- - GBM (H2O GBM)
793
- - DeepLearning (Fully-connected multi-layer artificial neural network)
794
- - StackedEnsemble (Stacked Ensembles, includes an ensemble of all the base models and ensembles using subsets of the base models)
795
-
796
- modeling_plan: The list of modeling steps to be used by the AutoML engine. (They may not all get executed, depending on other constraints.)
797
-
798
- preprocessing: The list of preprocessing steps to run. Only ["target_encoding"] is currently supported. There is more information about how Target Encoding is automatically applied here. Experimental.
799
-
800
- exploitation_ratio: Specify the budget ratio (between 0 and 1) dedicated to the exploitation (vs exploration) phase. By default, the exploitation phase is disabled (exploitation_ratio=0) as this is still experimental; to activate it, it is recommended to try a ratio around 0.1. Note that the current exploitation phase only tries to fine-tune the best XGBoost and the best GBM found during exploration. Experimental.
801
-
802
- monotone_constraints: A mapping that represents monotonic constraints. Use +1 to enforce an increasing constraint and -1 to specify a decreasing constraint.
803
-
804
- keep_cross_validation_predictions: Specify whether to keep the predictions of the cross-validation predictions. This needs to be set to TRUE if running the same AutoML object for repeated runs because CV predictions are required to build additional Stacked Ensemble models in AutoML. This option defaults to FALSE.
805
-
806
- keep_cross_validation_models: Specify whether to keep the cross-validated models. Keeping cross-validation models may consume significantly more memory in the H2O cluster. This option defaults to FALSE.
807
-
808
- keep_cross_validation_fold_assignment: Enable this option to preserve the cross-validation fold assignment. Defaults to FALSE.
809
-
810
- verbosity: (Optional: Python and R only) The verbosity of the backend messages printed during training. Must be one of "debug", "info", "warn". Defaults to NULL/None (client logging disabled).
811
-
812
- export_checkpoints_dir: Specify a directory to which generated models will automatically be exported.
813
-
814
- Notes
815
- Validation Options
816
- If the user turns off cross-validation by setting nfolds == 0, then cross-validation metrics will not be available to populate the leaderboard. In this case, we need to make sure there is a holdout frame (i.e. the “leaderboard frame”) to score the models on so that we can generate model performance metrics for the leaderboard. Without cross-validation, we will also require a validation frame to be used for early stopping on the models. Therefore, if either of these frames are not provided by the user, they will be automatically partitioned from the training data. If either frame is missing, 10% of the training data will be used to create a missing frame (if both are missing then a total of 20% of the training data will be used to create a 10% validation and 10% leaderboard frame).
817
-
818
- XGBoost Memory Requirements
819
- XGBoost, which is included in H2O as a third party library, requires its own memory outside the H2O (Java) cluster. When running AutoML with XGBoost (it is included by default), be sure you allow H2O no more than 2/3 of the total available RAM. Example: If you have 60G RAM, use h2o.init(max_mem_size = "40G"), leaving 20G for XGBoost.
820
-
821
- Scikit-learn Compatibility
822
- H2OAutoML can interact with the h2o.sklearn module. The h2o.sklearn module exposes 2 wrappers for H2OAutoML (H2OAutoMLClassifier and H2OAutoMLRegressor), which expose the standard API familiar to sklearn users: fit, predict, fit_predict, score, get_params, and set_params. It accepts various formats as input data (H2OFrame, numpy array, pandas Dataframe) which allows them to be combined with pure sklearn components in pipelines. For an example using H2OAutoML with the h2o.sklearn module, click here.
823
-
824
- Explainability
825
- AutoML objects are fully supported though the H2O Model Explainability interface. A large number of multi-model comparison and single model (AutoML leader) plots can be generated automatically with a single call to h2o.explain(). We invite you to learn more at page linked above.
826
-
827
- Code Examples
828
-
829
- Training
830
- Here’s an example showing basic usage of the h2o.automl() function in R and the H2OAutoML class in Python. For demonstration purposes only, we explicitly specify the x argument, even though on this dataset, that’s not required. With this dataset, the set of predictors is all columns other than the response. Like other H2O algorithms, the default value of x is “all columns, excluding y”, so that will produce the same result.
831
-
832
- ``` python
833
- import h2o
834
- from h2o.automl import H2OAutoML
835
-
836
- # Start the H2O cluster (locally)
837
- h2o.init()
838
-
839
- # Import a sample binary outcome train/test set into H2O
840
- train = h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/higgs/higgs_train_10k.csv")
841
- test = h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/higgs/higgs_test_5k.csv")
842
-
843
- # Identify predictors and response
844
- x = train.columns
845
- y = "response"
846
- x.remove(y)
847
-
848
- # For binary classification, response should be a factor
849
- train[y] = train[y].asfactor()
850
- test[y] = test[y].asfactor()
851
-
852
- # Run AutoML for 20 base models
853
- aml = H2OAutoML(max_models=20, seed=1)
854
- aml.train(x=x, y=y, training_frame=train)
855
-
856
- # View the AutoML Leaderboard
857
- lb = aml.leaderboard
858
- lb.head(rows=lb.nrows) # Print all rows instead of default (10 rows)
859
-
860
- # model_id auc logloss mean_per_class_error rmse mse
861
- # --------------------------------------------------- -------- --------- ---------------------- -------- --------
862
- # StackedEnsemble_AllModels_AutoML_20181212_105540 0.789801 0.551109 0.333174 0.43211 0.186719
863
- # StackedEnsemble_BestOfFamily_AutoML_20181212_105540 0.788425 0.552145 0.323192 0.432625 0.187165
864
- # XGBoost_1_AutoML_20181212_105540 0.784651 0.55753 0.325471 0.434949 0.189181
865
- # XGBoost_grid_1_AutoML_20181212_105540_model_4 0.783523 0.557854 0.318819 0.435249 0.189441
866
- # XGBoost_grid_1_AutoML_20181212_105540_model_3 0.783004 0.559613 0.325081 0.435708 0.189841
867
- # XGBoost_2_AutoML_20181212_105540 0.78136 0.55888 0.347074 0.435907 0.190015
868
- # XGBoost_3_AutoML_20181212_105540 0.780847 0.559589 0.330739 0.43613 0.190209
869
- # GBM_5_AutoML_20181212_105540 0.780837 0.559903 0.340848 0.436191 0.190263
870
- # GBM_2_AutoML_20181212_105540 0.780036 0.559806 0.339926 0.436415 0.190458
871
- # GBM_1_AutoML_20181212_105540 0.779827 0.560857 0.335096 0.436616 0.190633
872
- # GBM_3_AutoML_20181212_105540 0.778669 0.56179 0.325538 0.437189 0.191134
873
- # XGBoost_grid_1_AutoML_20181212_105540_model_2 0.774411 0.575017 0.322811 0.4427 0.195984
874
- # GBM_4_AutoML_20181212_105540 0.771426 0.569712 0.33742 0.44107 0.194543
875
- # GBM_grid_1_AutoML_20181212_105540_model_1 0.769752 0.572583 0.344331 0.442452 0.195764
876
- # GBM_grid_1_AutoML_20181212_105540_model_2 0.754366 0.918567 0.355855 0.496638 0.246649
877
- # DRF_1_AutoML_20181212_105540 0.742892 0.595883 0.355403 0.452774 0.205004
878
- # XRT_1_AutoML_20181212_105540 0.742091 0.599346 0.356583 0.453117 0.205315
879
- # DeepLearning_grid_1_AutoML_20181212_105540_model_2 0.741795 0.601497 0.368291 0.454904 0.206937
880
- # XGBoost_grid_1_AutoML_20181212_105540_model_1 0.693554 0.620702 0.40588 0.465791 0.216961
881
- # DeepLearning_1_AutoML_20181212_105540 0.69137 0.637954 0.409351 0.47178 0.222576
882
- # DeepLearning_grid_1_AutoML_20181212_105540_model_1 0.690084 0.661794 0.418469 0.476635 0.227181
883
- # GLM_grid_1_AutoML_20181212_105540_model_1 0.682648 0.63852 0.397234 0.472683 0.223429
884
- #
885
- # [22 rows x 6 columns]
886
-
887
- # The leader model is stored here
888
- aml.leader
889
- ```
890
-
891
- Prediction
892
- Using the predict() function with AutoML generates predictions on the leader model from the run. The order of the rows in the results is the same as the order in which the data was loaded, even if some rows fail (for example, due to missing values or unseen factor levels).
893
-
894
- ``` python
895
- # To generate predictions on a test set, you can make predictions
896
- # directly on the `H2OAutoML` object or on the leader model
897
- # object directly
898
- preds = aml.predict(test)
899
-
900
- # or:
901
- preds = aml.leader.predict(test)
902
- ```
903
-
904
- AutoML Output
905
-
906
- Leaderboard
907
- The AutoML object includes a “leaderboard” of models that were trained in the process, including the 5-fold cross-validated model performance (by default). The number of folds used in the model evaluation process can be adjusted using the nfolds parameter. If you would like to score the models on a specific dataset, you can specify the leaderboard_frame argument in the AutoML run, and then the leaderboard will show scores on that dataset instead.
908
-
909
- The models are ranked by a default metric based on the problem type (the second column of the leaderboard). In binary classification problems, that metric is AUC, and in multiclass classification problems, the metric is mean per-class error. In regression problems, the default sort metric is RMSE. Some additional metrics are also provided, for convenience.
910
-
911
- To help users assess the complexity of AutoML models, the h2o.get_leaderboard function has been been expanded by allowing an extra_columns parameter. This parameter allows you to specify which (if any) optional columns should be added to the leaderboard. This defaults to None. Allowed options include:
912
-
913
- - training_time_ms: A column providing the training time of each model in milliseconds. (Note that this doesn't include the training of cross validation models.)
914
-
915
- - predict_time_per_row_ms: A column providing the average prediction time by the model for a single row.
916
-
917
- - ALL: Adds columns for both training_time_ms and predict_time_per_row_ms.
918
-
919
- ``` python
920
- # Get leaderboard with all possible columns
921
- lb = h2o.automl.get_leaderboard(aml, extra_columns = "ALL")
922
- lb
923
- ```
924
-
925
- Examine Models
926
- To examine the trained models more closely, you can interact with the models, either by model ID, or a convenience function which can grab the best model of each model type (ranked by the default metric, or a metric of your choosing).
927
-
928
- ``` python
929
- # Get the best model using the metric
930
- m = aml.leader
931
- # this is equivalent to
932
- m = aml.get_best_model()
933
-
934
- # Get the best model using a non-default metric
935
- m = aml.get_best_model(criterion="logloss")
936
-
937
- # Get the best XGBoost model using default sort metric
938
- xgb = aml.get_best_model(algorithm="xgboost")
939
-
940
- # Get the best XGBoost model, ranked by logloss
941
- xgb = aml.get_best_model(algorithm="xgboost", criterion="logloss")
942
- ```
943
-
944
- Get a specific model by model ID:
945
-
946
- ``` python
947
- # Get a specific model by model ID
948
- m = h2o.get_model("StackedEnsemble_BestOfFamily_AutoML_20191213_174603")
949
- ```
950
-
951
- Once you have retreived the model in R or Python, you can inspect the model parameters as follows:
952
-
953
- ``` python
954
- # View the parameters for the XGBoost model selected above
955
- xgb.params.keys()
956
-
957
- # Inspect individual parameter values
958
- xgb.params['ntrees']
959
- ```
960
-
961
- AutoML Log
962
- When using Python or R clients, you can also access meta information with the following AutoML object properties:
963
-
964
- - event_log: an H2OFrame with selected AutoML backend events generated during training.
965
-
966
- - training_info: a dictionary exposing data that could be useful for post-analysis (e.g. various timings). If you want training and prediction times for each model, it's easier to explore that data in the extended leaderboard using the h2o.get_leaderboard() function.
967
-
968
- ``` python
969
- # Get AutoML event log
970
- log = aml.event_log
971
-
972
- # Get training timing info
973
- info = aml.training_info
974
- ```
975
-
976
- Experimental Features
977
-
978
- Preprocessing
979
- As of H2O 3.32.0.1, AutoML now has a preprocessing option with minimal support for automated Target Encoding of high cardinality categorical variables. The only currently supported option is preprocessing = ["target_encoding"]: we automatically tune a Target Encoder model and apply it to columns that meet certain cardinality requirements for the tree-based algorithms (XGBoost, H2O GBM and Random Forest).
980
-
981
- FAQ
982
-
983
- 1. Which models are trained in the AutoML process?
984
-
985
- The current version of AutoML trains and cross-validates the following algorithms: three pre-specified XGBoost GBM (Gradient Boosting Machine) models, a fixed grid of GLMs, a default Random Forest (DRF), five pre-specified H2O GBMs, a near-default Deep Neural Net, an Extremely Randomized Forest (XRT), a random grid of XGBoost GBMs, a random grid of H2O GBMs, and a random grid of Deep Neural Nets. In some cases, there will not be enough time to complete all the algorithms, so some may be missing from the leaderboard. In other cases, the grids will stop early, and if there's time left, the top two random grids will be restarted to train more models. AutoML trains multiple Stacked Ensemble models throughout the process (more info about the ensembles below).
986
-
987
- Particular algorithms (or groups of algorithms) can be switched off using the exclude_algos argument. This is useful if you already have some idea of the algorithms that will do well on your dataset, though sometimes this can lead to a loss of performance because having more diversity among the set of models generally increases the performance of the Stacked Ensembles. As a first step you could leave all the algorithms on, and examine their performance characteristics (e.g. prediction speed) to get a sense of what might be practically useful in your specific use-case, and then turn off algorithms that are not interesting or useful to you. We recommend using the H2O Model Explainability interface to explore and further evaluate your AutoML models, which can inform your choice of model (if you have other goals beyond simply maximizing model accuracy).
988
-
989
- A list of the hyperparameters searched over for each algorithm in the AutoML process is included in the appendix below. More details about the hyperparameter ranges for the models in addition to the hard-coded models will be added to the appendix at a later date.
990
-
991
- AutoML trains several Stacked Ensemble models during the run (unless ensembles are turned off using exclude_algos). We have subdivided the model training in AutoML into “model groups” with different priority levels. After each group is completed, and at the very end of the AutoML process, we train (at most) two additional Stacked Ensembles with the existing models. There are currently two types of Stacked Ensembles: one which includes all the base models (“All Models”), and one comprised only of the best model from each algorithm family (“Best of Family”). The Best of Family ensembles are more optimized for production use since it only contains six (or fewer) base models. It should be relatively fast to use in production (to generate predictions on new data) without much degradation in model performance when compared to the final “All Models” ensemble, for example. This may be useful if you want the model performance boost from ensembling without the added time or complexity of a large ensemble. You can also inspect some of the earlier “All Models” Stacked Ensembles that have fewer models as an alternative to the Best of Family ensembles. The metalearner used in all ensembles is a variant of the default Stacked Ensemble metalearner: a non-negative GLM with regularization (Lasso or Elastic net, chosen by CV) to encourage more sparse ensembles. The metalearner also uses a logit transform (on the base learner CV preds) for classification tasks before training.
992
-
993
- For information about how previous versions of AutoML were different than the current one, there's a brief description here.
994
-
995
- 2. How do I save AutoML runs?
996
-
997
- Rather than saving an AutoML object itself, currently, the best thing to do is to save the models you want to keep, individually. A utility for saving all of the models at once, along with a way to save the AutoML object (with leaderboard), will be added in a future release.
998
-
999
- 3. Can we make use of GPUs with AutoML?
1000
-
1001
- XGBoost models in AutoML can make use of GPUs. Keep in mind that the following requirements must be met:
1002
-
1003
- - NVIDIA GPUs (GPU Cloud, DGX Station, DGX-1, or DGX-2)
1004
- - CUDA 8
1005
-
1006
- You can monitor your GPU utilization via the nvidia-smi command. Refer to https://developer.nvidia.com/nvidia-system-management-interface for more information.
1007
-
1008
- 4. Why don't I see XGBoost models?
1009
-
1010
- AutoML includes XGBoost GBMs (Gradient Boosting Machines) among its set of algorithms. This feature is currently provided with the following restrictions:
1011
-
1012
- - XGBoost is not currently available on Windows machines. Follow here: https://github.com/h2oai/h2o-3/issues/7139 for updates.
1013
-
1014
- - XGBoost is used only if it is available globally and if it hasn't been explicitly disabled. You can check if XGBoost is available by using the h2o.xgboost.available() in R or h2o.estimators.xgboost.H2OXGBoostEstimator.available() in Python.
1015
-
1016
- 5. Why doesn't AutoML use all the time that it's given?
1017
-
1018
- If you're using 3.34.0.1 or later, AutoML should use all the time that it's given using max_runtime_secs. However, if you're using an earlier version, then early stopping was enabled by default and you can stop early. With early stopping, AutoML will stop once there's no longer “enough” incremental improvement. The user can tweak the early stopping paramters to be more or less sensitive. Set stopping_rounds higher if you want to slow down early stopping and let AutoML train more models before it stops.
1019
-
1020
- 6. Does AutoML support MOJOs?
1021
-
1022
- AutoML will always produce a model which has a MOJO. Though it depends on the run, you are most likely to get a Stacked Ensemble. While all models are importable, only individual models are exportable.
1023
-
1024
- 7. Why doesn't AutoML use all the time that it's given?
1025
-
1026
- If you're using 3.34.0.1 or later, AutoML should use all the time that it's given using max_runtime_secs. However, if you're using an earlier version, then early stopping was enabled by default and you can stop early. With early stopping, AutoML will stop once there's no longer “enough” incremental improvement. The user can tweak the early stopping paramters to be more or less sensitive. Set stopping_rounds higher if you want to slow down early stopping and let AutoML train more models before it stops.
1027
-
1028
- 8. What is the history of H2O AutoML?
1029
-
1030
- The H2O AutoML algorithm was first released in H2O 3.12.0.1 on June 6, 2017 by Erin LeDell, and is based on research from her PhD thesis. New features and performance improvements have been made in every major version of H2O since the initial release.
1031
-
1032
- """