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 5.9 kB

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