mirror of
https://github.com/microsoft/autogen.git
synced 2026-04-20 03:02:16 -04:00
example update (#359)
update some examples for consistencies with others.
This commit is contained in:
@@ -2,6 +2,7 @@ import unittest
|
||||
import numpy as np
|
||||
import scipy.sparse
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
from sklearn.model_selection import train_test_split
|
||||
import pandas as pd
|
||||
from datetime import datetime
|
||||
from flaml import AutoML
|
||||
@@ -221,14 +222,28 @@ class TestClassification(unittest.TestCase):
|
||||
print(automl_experiment.best_estimator)
|
||||
|
||||
def test_ray_classification(self):
|
||||
from sklearn.datasets import make_classification
|
||||
X, y = load_breast_cancer(return_X_y=True)
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
|
||||
|
||||
X, y = make_classification(1000, 10)
|
||||
automl = AutoML()
|
||||
try:
|
||||
automl.fit(X, y, time_budget=10, task="classification", use_ray=True)
|
||||
automl.fit(
|
||||
X, y, time_budget=10, task="classification", n_concurrent_trials=2
|
||||
X_train,
|
||||
y_train,
|
||||
X_val=X_test,
|
||||
y_val=y_test,
|
||||
time_budget=10,
|
||||
task="classification",
|
||||
use_ray=True,
|
||||
)
|
||||
automl.fit(
|
||||
X_train,
|
||||
y_train,
|
||||
X_val=X_test,
|
||||
y_val=y_test,
|
||||
time_budget=10,
|
||||
task="classification",
|
||||
n_concurrent_trials=2,
|
||||
)
|
||||
except ImportError:
|
||||
return
|
||||
|
||||
@@ -1,30 +1,28 @@
|
||||
import ray
|
||||
import lightgbm as lgb
|
||||
import numpy as np
|
||||
import sklearn.datasets
|
||||
import sklearn.metrics
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
from sklearn.metrics import accuracy_score
|
||||
from sklearn.model_selection import train_test_split
|
||||
from flaml import tune
|
||||
from flaml.model import LGBMEstimator
|
||||
|
||||
data, target = sklearn.datasets.load_breast_cancer(return_X_y=True)
|
||||
train_x, test_x, train_y, test_y = train_test_split(data, target, test_size=0.25)
|
||||
X, y = load_breast_cancer(return_X_y=True)
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
|
||||
|
||||
|
||||
def train_breast_cancer(config):
|
||||
params = LGBMEstimator(**config).params
|
||||
train_set = lgb.Dataset(train_x, label=train_y)
|
||||
train_set = lgb.Dataset(X_train, label=y_train)
|
||||
gbm = lgb.train(params, train_set)
|
||||
preds = gbm.predict(test_x)
|
||||
preds = gbm.predict(X_test)
|
||||
pred_labels = np.rint(preds)
|
||||
tune.report(
|
||||
mean_accuracy=sklearn.metrics.accuracy_score(test_y, pred_labels), done=True
|
||||
)
|
||||
tune.report(mean_accuracy=accuracy_score(y_test, pred_labels), done=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
ray.init(address="auto")
|
||||
flaml_lgbm_search_space = LGBMEstimator.search_space(train_x.shape)
|
||||
flaml_lgbm_search_space = LGBMEstimator.search_space(X_train.shape)
|
||||
config_search_space = {
|
||||
hp: space["domain"] for hp, space in flaml_lgbm_search_space.items()
|
||||
}
|
||||
|
||||
@@ -36,6 +36,14 @@ low_cost_partial_config = {
|
||||
for hp, space in flaml_lgbm_search_space.items()
|
||||
if "low_cost_init_value" in space
|
||||
}
|
||||
# initial points to evaluate
|
||||
points_to_evaluate = [
|
||||
{
|
||||
hp: space["init_value"]
|
||||
for hp, space in flaml_lgbm_search_space.items()
|
||||
if "init_value" in space
|
||||
}
|
||||
]
|
||||
# run the tuning, minimizing mse, with total time budget 3 seconds
|
||||
analysis = tune.run(
|
||||
train_lgbm,
|
||||
@@ -43,6 +51,7 @@ analysis = tune.run(
|
||||
mode="min",
|
||||
config=config_search_space,
|
||||
low_cost_partial_config=low_cost_partial_config,
|
||||
points_to_evaluate=points_to_evaluate,
|
||||
time_budget_s=3,
|
||||
num_samples=-1,
|
||||
)
|
||||
Reference in New Issue
Block a user