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.

test_defaults.py 8.8 kB

Refactor into automl subpackage (#809) * Refactor into automl subpackage Moved some of the packages into an automl subpackage to tidy before the task-based refactor. This is in response to discussions with the group and a comment on the first task-based PR. Only changes here are moving subpackages and modules into the new automl, fixing imports to work with this structure and fixing some dependencies in setup.py. * Fix doc building post automl subpackage refactor * Fix broken links in website post automl subpackage refactor * Fix broken links in website post automl subpackage refactor * Remove vw from test deps as this is breaking the build * Move default back to the top-level I'd moved this to automl as that's where it's used internally, but had missed that this is actually part of the public interface so makes sense to live where it was. * Re-add top level modules with deprecation warnings flaml.data, flaml.ml and flaml.model are re-added to the top level, being re-exported from flaml.automl for backwards compatability. Adding a deprecation warning so that we can have a planned removal later. * Fix model.py line-endings * Pin pytorch-lightning to less than 1.8.0 We're seeing strange lightning related bugs from pytorch-forecasting since the release of lightning 1.8.0. Going to try constraining this to see if we have a fix. * Fix the lightning version pin Was optimistic with setting it in the 1.7.x range, but that isn't compatible with python 3.6 * Remove lightning version pin * Revert dependency version changes * Minor change to retrigger the build * Fix line endings in ml.py and model.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: EgorKraevTransferwise <egor.kraev@transferwise.com>
3 years ago
Refactor into automl subpackage (#809) * Refactor into automl subpackage Moved some of the packages into an automl subpackage to tidy before the task-based refactor. This is in response to discussions with the group and a comment on the first task-based PR. Only changes here are moving subpackages and modules into the new automl, fixing imports to work with this structure and fixing some dependencies in setup.py. * Fix doc building post automl subpackage refactor * Fix broken links in website post automl subpackage refactor * Fix broken links in website post automl subpackage refactor * Remove vw from test deps as this is breaking the build * Move default back to the top-level I'd moved this to automl as that's where it's used internally, but had missed that this is actually part of the public interface so makes sense to live where it was. * Re-add top level modules with deprecation warnings flaml.data, flaml.ml and flaml.model are re-added to the top level, being re-exported from flaml.automl for backwards compatability. Adding a deprecation warning so that we can have a planned removal later. * Fix model.py line-endings * Pin pytorch-lightning to less than 1.8.0 We're seeing strange lightning related bugs from pytorch-forecasting since the release of lightning 1.8.0. Going to try constraining this to see if we have a fix. * Fix the lightning version pin Was optimistic with setting it in the 1.7.x range, but that isn't compatible with python 3.6 * Remove lightning version pin * Revert dependency version changes * Minor change to retrigger the build * Fix line endings in ml.py and model.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: EgorKraevTransferwise <egor.kraev@transferwise.com>
3 years ago
Refactor into automl subpackage (#809) * Refactor into automl subpackage Moved some of the packages into an automl subpackage to tidy before the task-based refactor. This is in response to discussions with the group and a comment on the first task-based PR. Only changes here are moving subpackages and modules into the new automl, fixing imports to work with this structure and fixing some dependencies in setup.py. * Fix doc building post automl subpackage refactor * Fix broken links in website post automl subpackage refactor * Fix broken links in website post automl subpackage refactor * Remove vw from test deps as this is breaking the build * Move default back to the top-level I'd moved this to automl as that's where it's used internally, but had missed that this is actually part of the public interface so makes sense to live where it was. * Re-add top level modules with deprecation warnings flaml.data, flaml.ml and flaml.model are re-added to the top level, being re-exported from flaml.automl for backwards compatability. Adding a deprecation warning so that we can have a planned removal later. * Fix model.py line-endings * Pin pytorch-lightning to less than 1.8.0 We're seeing strange lightning related bugs from pytorch-forecasting since the release of lightning 1.8.0. Going to try constraining this to see if we have a fix. * Fix the lightning version pin Was optimistic with setting it in the 1.7.x range, but that isn't compatible with python 3.6 * Remove lightning version pin * Revert dependency version changes * Minor change to retrigger the build * Fix line endings in ml.py and model.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: EgorKraevTransferwise <egor.kraev@transferwise.com>
3 years ago
Refactor into automl subpackage (#809) * Refactor into automl subpackage Moved some of the packages into an automl subpackage to tidy before the task-based refactor. This is in response to discussions with the group and a comment on the first task-based PR. Only changes here are moving subpackages and modules into the new automl, fixing imports to work with this structure and fixing some dependencies in setup.py. * Fix doc building post automl subpackage refactor * Fix broken links in website post automl subpackage refactor * Fix broken links in website post automl subpackage refactor * Remove vw from test deps as this is breaking the build * Move default back to the top-level I'd moved this to automl as that's where it's used internally, but had missed that this is actually part of the public interface so makes sense to live where it was. * Re-add top level modules with deprecation warnings flaml.data, flaml.ml and flaml.model are re-added to the top level, being re-exported from flaml.automl for backwards compatability. Adding a deprecation warning so that we can have a planned removal later. * Fix model.py line-endings * Pin pytorch-lightning to less than 1.8.0 We're seeing strange lightning related bugs from pytorch-forecasting since the release of lightning 1.8.0. Going to try constraining this to see if we have a fix. * Fix the lightning version pin Was optimistic with setting it in the 1.7.x range, but that isn't compatible with python 3.6 * Remove lightning version pin * Revert dependency version changes * Minor change to retrigger the build * Fix line endings in ml.py and model.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: EgorKraevTransferwise <egor.kraev@transferwise.com>
3 years ago
Refactor into automl subpackage (#809) * Refactor into automl subpackage Moved some of the packages into an automl subpackage to tidy before the task-based refactor. This is in response to discussions with the group and a comment on the first task-based PR. Only changes here are moving subpackages and modules into the new automl, fixing imports to work with this structure and fixing some dependencies in setup.py. * Fix doc building post automl subpackage refactor * Fix broken links in website post automl subpackage refactor * Fix broken links in website post automl subpackage refactor * Remove vw from test deps as this is breaking the build * Move default back to the top-level I'd moved this to automl as that's where it's used internally, but had missed that this is actually part of the public interface so makes sense to live where it was. * Re-add top level modules with deprecation warnings flaml.data, flaml.ml and flaml.model are re-added to the top level, being re-exported from flaml.automl for backwards compatability. Adding a deprecation warning so that we can have a planned removal later. * Fix model.py line-endings * Pin pytorch-lightning to less than 1.8.0 We're seeing strange lightning related bugs from pytorch-forecasting since the release of lightning 1.8.0. Going to try constraining this to see if we have a fix. * Fix the lightning version pin Was optimistic with setting it in the 1.7.x range, but that isn't compatible with python 3.6 * Remove lightning version pin * Revert dependency version changes * Minor change to retrigger the build * Fix line endings in ml.py and model.py Co-authored-by: Qingyun Wu <qingyun.wu@psu.edu> Co-authored-by: EgorKraevTransferwise <egor.kraev@transferwise.com>
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import sys
  2. import pickle
  3. from sklearn.datasets import load_iris, fetch_california_housing, load_breast_cancer
  4. from sklearn.model_selection import train_test_split
  5. import pandas as pd
  6. from flaml import AutoML
  7. from flaml.default import (
  8. preprocess_and_suggest_hyperparams,
  9. suggest_hyperparams,
  10. suggest_learner,
  11. )
  12. from flaml.default import portfolio, regret
  13. def test_greedy_feedback(path="test/default", strategy="greedy-feedback"):
  14. # sys.argv = f"portfolio.py --output {path} --input {path} --metafeatures {path}/all/metafeatures.csv --task binary --estimator lgbm xgboost xgb_limitdepth rf extra_tree --strategy {strategy}".split()
  15. # portfolio.main()
  16. # sys.argv = f"portfolio.py --output {path} --input {path} --metafeatures {path}/all/metafeatures.csv --task multiclass --estimator lgbm xgboost xgb_limitdepth rf extra_tree --strategy {strategy}".split()
  17. # portfolio.main()
  18. sys.argv = f"portfolio.py --output {path} --input {path} --metafeatures {path}/all/metafeatures.csv --task regression --estimator lgbm --strategy {strategy}".split()
  19. portfolio.main()
  20. def test_build_portfolio(path="test/default", strategy="greedy"):
  21. sys.argv = f"portfolio.py --output {path} --input {path} --metafeatures {path}/all/metafeatures.csv --task binary --estimator lgbm xgboost xgb_limitdepth rf extra_tree --strategy {strategy}".split()
  22. portfolio.main()
  23. sys.argv = f"portfolio.py --output {path} --input {path} --metafeatures {path}/all/metafeatures.csv --task multiclass --estimator lgbm xgboost xgb_limitdepth rf extra_tree --strategy {strategy}".split()
  24. portfolio.main()
  25. sys.argv = f"portfolio.py --output {path} --input {path} --metafeatures {path}/all/metafeatures.csv --task regression --estimator lgbm xgboost xgb_limitdepth rf extra_tree --strategy {strategy}".split()
  26. portfolio.main()
  27. def test_iris(as_frame=True):
  28. automl = AutoML()
  29. automl_settings = {
  30. "time_budget": 2,
  31. "metric": "accuracy",
  32. "task": "classification",
  33. "log_file_name": "test/iris.log",
  34. "n_jobs": 1,
  35. "starting_points": "data",
  36. }
  37. X_train, y_train = load_iris(return_X_y=True, as_frame=as_frame)
  38. automl.fit(X_train, y_train, **automl_settings)
  39. automl_settings["starting_points"] = "data:test/default"
  40. automl.fit(X_train, y_train, **automl_settings)
  41. def test_housing(as_frame=True):
  42. automl = AutoML()
  43. automl_settings = {
  44. "time_budget": 2,
  45. "task": "regression",
  46. "estimator_list": ["xgboost", "lgbm"],
  47. "log_file_name": "test/housing.log",
  48. "n_jobs": 1,
  49. "starting_points": "data",
  50. "max_iter": 0,
  51. }
  52. X_train, y_train = fetch_california_housing(return_X_y=True, as_frame=as_frame)
  53. automl.fit(X_train, y_train, **automl_settings)
  54. def test_regret():
  55. sys.argv = "regret.py --result_csv test/default/lgbm/results.csv --task_type binary --output test/default/lgbm/binary_regret.csv".split()
  56. regret.main()
  57. def test_suggest_classification():
  58. location = "test/default"
  59. X_train, y_train = load_breast_cancer(return_X_y=True, as_frame=True)
  60. suggested = suggest_hyperparams("classification", X_train, y_train, "lgbm", location=location)
  61. print(suggested)
  62. suggested = preprocess_and_suggest_hyperparams("classification", X_train, y_train, "xgboost", location=location)
  63. print(suggested)
  64. suggested = suggest_hyperparams("classification", X_train, y_train, "xgb_limitdepth", location=location)
  65. print(suggested)
  66. X, y = load_iris(return_X_y=True, as_frame=True)
  67. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
  68. (
  69. hyperparams,
  70. estimator_class,
  71. X,
  72. y,
  73. feature_transformer,
  74. label_transformer,
  75. ) = preprocess_and_suggest_hyperparams("classification", X_train, y_train, "lgbm", location=location)
  76. with open("test/default/feature_transformer", "wb") as f:
  77. pickle.dump(feature_transformer, f, pickle.HIGHEST_PROTOCOL)
  78. model = estimator_class(**hyperparams) # estimator_class is LGBMClassifier
  79. model.fit(X, y)
  80. X_test = feature_transformer.transform(X_test)
  81. y_pred = label_transformer.inverse_transform(pd.Series(model.predict(X_test).astype(int)))
  82. print(y_pred)
  83. suggested = suggest_hyperparams("classification", X_train, y_train, "xgboost", location=location)
  84. print(suggested)
  85. suggested = preprocess_and_suggest_hyperparams(
  86. "classification", X_train, y_train, "xgb_limitdepth", location=location
  87. )
  88. print(suggested)
  89. suggested = suggest_hyperparams("classification", X_train, y_train, "xgb_limitdepth", location=location)
  90. suggested = suggest_learner(
  91. "classification",
  92. X_train,
  93. y_train,
  94. estimator_list=["xgboost", "xgb_limitdepth"],
  95. location=location,
  96. )
  97. print(suggested)
  98. def test_suggest_regression():
  99. location = "test/default"
  100. X_train, y_train = fetch_california_housing(return_X_y=True, as_frame=True)
  101. suggested = suggest_hyperparams("regression", X_train, y_train, "lgbm", location=location)
  102. print(suggested)
  103. suggested = preprocess_and_suggest_hyperparams("regression", X_train, y_train, "xgboost", location=location)
  104. print(suggested)
  105. suggested = suggest_hyperparams("regression", X_train, y_train, "xgb_limitdepth", location=location)
  106. print(suggested)
  107. suggested = suggest_learner("regression", X_train, y_train, location=location)
  108. print(suggested)
  109. def test_rf():
  110. from flaml.default import RandomForestRegressor, RandomForestClassifier
  111. X_train, y_train = load_breast_cancer(return_X_y=True, as_frame=True)
  112. rf = RandomForestClassifier()
  113. rf.fit(X_train[:100], y_train[:100])
  114. rf.predict(X_train)
  115. rf.predict_proba(X_train)
  116. print(rf)
  117. location = "test/default"
  118. X_train, y_train = fetch_california_housing(return_X_y=True, as_frame=True)
  119. rf = RandomForestRegressor(default_location=location)
  120. rf.fit(X_train[:100], y_train[:100])
  121. rf.predict(X_train)
  122. print(rf)
  123. def test_extratrees():
  124. from flaml.default import ExtraTreesRegressor, ExtraTreesClassifier
  125. X_train, y_train = load_iris(return_X_y=True, as_frame=True)
  126. classifier = ExtraTreesClassifier()
  127. classifier.fit(X_train[:100], y_train[:100])
  128. classifier.predict(X_train)
  129. classifier.predict_proba(X_train)
  130. print(classifier)
  131. location = "test/default"
  132. X_train, y_train = fetch_california_housing(return_X_y=True, as_frame=True)
  133. regressor = ExtraTreesRegressor(default_location=location)
  134. regressor.fit(X_train[:100], y_train[:100])
  135. regressor.predict(X_train)
  136. print(regressor)
  137. def test_lgbm():
  138. from flaml.default import LGBMRegressor, LGBMClassifier
  139. X_train, y_train = load_breast_cancer(return_X_y=True, as_frame=True)
  140. classifier = LGBMClassifier(n_jobs=1)
  141. classifier.fit(X_train, y_train)
  142. classifier.predict(X_train, pred_contrib=True)
  143. classifier.predict_proba(X_train)
  144. print(classifier.get_params())
  145. print(classifier)
  146. print(classifier.classes_)
  147. location = "test/default"
  148. X_train, y_train = fetch_california_housing(return_X_y=True, as_frame=True)
  149. regressor = LGBMRegressor(default_location=location)
  150. regressor.fit(X_train, y_train)
  151. regressor.predict(X_train)
  152. print(regressor)
  153. def test_xgboost():
  154. from flaml.default import XGBRegressor, XGBClassifier
  155. X_train, y_train = load_breast_cancer(return_X_y=True, as_frame=True)
  156. classifier = XGBClassifier(max_depth=0)
  157. classifier.fit(X_train[:100], y_train[:100])
  158. classifier.predict(X_train)
  159. classifier.predict_proba(X_train)
  160. print(classifier)
  161. print(classifier.classes_)
  162. location = "test/default"
  163. X_train, y_train = fetch_california_housing(return_X_y=True, as_frame=True)
  164. regressor = XGBRegressor(default_location=location)
  165. regressor.fit(X_train[:100], y_train[:100])
  166. regressor.predict(X_train)
  167. print(regressor)
  168. def test_nobudget():
  169. X_train, y_train = load_breast_cancer(return_X_y=True, as_frame=True)
  170. automl = AutoML()
  171. automl.fit(
  172. X_train[:20],
  173. y_train[:20],
  174. estimator_list=["lgbm", "extra_tree", "rf"],
  175. max_iter=12,
  176. starting_points="data",
  177. log_file_name="test/default/no_budget.txt",
  178. log_type="all",
  179. )
  180. automl.fit(X_train[:20], y_train[:20], estimator_list=["lgbm", "extra_tree", "rf"])
  181. # make sure that zero-shot config out of the search space does not degnerate to low cost init config
  182. assert automl.best_config_per_estimator["extra_tree"]["n_estimators"] > 4
  183. # make sure that the zero-shot config {} is not modified
  184. assert "criterion" not in automl.best_config_per_estimator["rf"]
  185. if __name__ == "__main__":
  186. test_build_portfolio("flaml/default")