You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AutoML-Regression.md 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # AutoML - Regression
  2. ### Prerequisites
  3. Install the [automl] option.
  4. ```bash
  5. pip install "flaml[automl]"
  6. ```
  7. ### A basic regression example
  8. ```python
  9. from flaml import AutoML
  10. from sklearn.datasets import fetch_california_housing
  11. # Initialize an AutoML instance
  12. automl = AutoML()
  13. # Specify automl goal and constraint
  14. automl_settings = {
  15. "time_budget": 1, # in seconds
  16. "metric": 'r2',
  17. "task": 'regression',
  18. "log_file_name": "california.log",
  19. }
  20. X_train, y_train = fetch_california_housing(return_X_y=True)
  21. # Train with labeled input data
  22. automl.fit(X_train=X_train, y_train=y_train,
  23. **automl_settings)
  24. # Predict
  25. print(automl.predict(X_train))
  26. # Print the best model
  27. print(automl.model.estimator)
  28. ```
  29. #### Sample output
  30. ```
  31. [flaml.automl: 11-15 07:08:19] {1485} INFO - Data split method: uniform
  32. [flaml.automl: 11-15 07:08:19] {1489} INFO - Evaluation method: holdout
  33. [flaml.automl: 11-15 07:08:19] {1540} INFO - Minimizing error metric: 1-r2
  34. [flaml.automl: 11-15 07:08:19] {1577} INFO - List of ML learners in AutoML Run: ['lgbm', 'rf', 'catboost', 'xgboost', 'extra_tree']
  35. [flaml.automl: 11-15 07:08:19] {1826} INFO - iteration 0, current learner lgbm
  36. [flaml.automl: 11-15 07:08:19] {1944} INFO - Estimated sufficient time budget=846s. Estimated necessary time budget=2s.
  37. [flaml.automl: 11-15 07:08:19] {2029} INFO - at 0.2s, estimator lgbm's best error=0.7393, best estimator lgbm's best error=0.7393
  38. [flaml.automl: 11-15 07:08:19] {1826} INFO - iteration 1, current learner lgbm
  39. [flaml.automl: 11-15 07:08:19] {2029} INFO - at 0.3s, estimator lgbm's best error=0.7393, best estimator lgbm's best error=0.7393
  40. [flaml.automl: 11-15 07:08:19] {1826} INFO - iteration 2, current learner lgbm
  41. [flaml.automl: 11-15 07:08:19] {2029} INFO - at 0.3s, estimator lgbm's best error=0.5446, best estimator lgbm's best error=0.5446
  42. [flaml.automl: 11-15 07:08:19] {1826} INFO - iteration 3, current learner lgbm
  43. [flaml.automl: 11-15 07:08:19] {2029} INFO - at 0.4s, estimator lgbm's best error=0.2807, best estimator lgbm's best error=0.2807
  44. [flaml.automl: 11-15 07:08:19] {1826} INFO - iteration 4, current learner lgbm
  45. [flaml.automl: 11-15 07:08:19] {2029} INFO - at 0.5s, estimator lgbm's best error=0.2712, best estimator lgbm's best error=0.2712
  46. [flaml.automl: 11-15 07:08:19] {1826} INFO - iteration 5, current learner lgbm
  47. [flaml.automl: 11-15 07:08:19] {2029} INFO - at 0.5s, estimator lgbm's best error=0.2712, best estimator lgbm's best error=0.2712
  48. [flaml.automl: 11-15 07:08:19] {1826} INFO - iteration 6, current learner lgbm
  49. [flaml.automl: 11-15 07:08:20] {2029} INFO - at 0.6s, estimator lgbm's best error=0.2712, best estimator lgbm's best error=0.2712
  50. [flaml.automl: 11-15 07:08:20] {1826} INFO - iteration 7, current learner lgbm
  51. [flaml.automl: 11-15 07:08:20] {2029} INFO - at 0.7s, estimator lgbm's best error=0.2197, best estimator lgbm's best error=0.2197
  52. [flaml.automl: 11-15 07:08:20] {1826} INFO - iteration 8, current learner xgboost
  53. [flaml.automl: 11-15 07:08:20] {2029} INFO - at 0.8s, estimator xgboost's best error=1.4958, best estimator lgbm's best error=0.2197
  54. [flaml.automl: 11-15 07:08:20] {1826} INFO - iteration 9, current learner xgboost
  55. [flaml.automl: 11-15 07:08:20] {2029} INFO - at 0.8s, estimator xgboost's best error=1.4958, best estimator lgbm's best error=0.2197
  56. [flaml.automl: 11-15 07:08:20] {1826} INFO - iteration 10, current learner xgboost
  57. [flaml.automl: 11-15 07:08:20] {2029} INFO - at 0.9s, estimator xgboost's best error=0.7052, best estimator lgbm's best error=0.2197
  58. [flaml.automl: 11-15 07:08:20] {1826} INFO - iteration 11, current learner xgboost
  59. [flaml.automl: 11-15 07:08:20] {2029} INFO - at 0.9s, estimator xgboost's best error=0.3619, best estimator lgbm's best error=0.2197
  60. [flaml.automl: 11-15 07:08:20] {1826} INFO - iteration 12, current learner xgboost
  61. [flaml.automl: 11-15 07:08:20] {2029} INFO - at 0.9s, estimator xgboost's best error=0.3619, best estimator lgbm's best error=0.2197
  62. [flaml.automl: 11-15 07:08:20] {1826} INFO - iteration 13, current learner xgboost
  63. [flaml.automl: 11-15 07:08:20] {2029} INFO - at 1.0s, estimator xgboost's best error=0.3619, best estimator lgbm's best error=0.2197
  64. [flaml.automl: 11-15 07:08:20] {1826} INFO - iteration 14, current learner extra_tree
  65. [flaml.automl: 11-15 07:08:20] {2029} INFO - at 1.1s, estimator extra_tree's best error=0.7197, best estimator lgbm's best error=0.2197
  66. [flaml.automl: 11-15 07:08:20] {2242} INFO - retrain lgbm for 0.0s
  67. [flaml.automl: 11-15 07:08:20] {2247} INFO - retrained model: LGBMRegressor(colsample_bytree=0.7610534336273627,
  68. learning_rate=0.41929025492645006, max_bin=255,
  69. min_child_samples=4, n_estimators=45, num_leaves=4,
  70. reg_alpha=0.0009765625, reg_lambda=0.009280655005879943,
  71. verbose=-1)
  72. [flaml.automl: 11-15 07:08:20] {1608} INFO - fit succeeded
  73. [flaml.automl: 11-15 07:08:20] {1610} INFO - Time taken to find the best model: 0.7289648056030273
  74. [flaml.automl: 11-15 07:08:20] {1624} WARNING - Time taken to find the best model is 73% of the provided time budget and not all estimators' hyperparameter search converged. Consider increasing the time budget.
  75. ```
  76. ### Multi-output regression
  77. We can combine `sklearn.MultiOutputRegressor` and `flaml.AutoML` to do AutoML for multi-output regression.
  78. ```python
  79. from flaml import AutoML
  80. from sklearn.datasets import make_regression
  81. from sklearn.model_selection import train_test_split
  82. from sklearn.multioutput import MultiOutputRegressor
  83. # create regression data
  84. X, y = make_regression(n_targets=3)
  85. # split into train and test data
  86. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=42)
  87. # train the model
  88. model = MultiOutputRegressor(AutoML(task="regression", time_budget=60))
  89. model.fit(X_train, y_train)
  90. # predict
  91. print(model.predict(X_test))
  92. ```
  93. It will perform AutoML for each target, each taking 60 seconds.