Skip to content

A programming framework for agentic AI

max_leaves (#138)

Chi Wang15fd8adac473
diff --git a/flaml/automl.py b/flaml/automl.py
index 68a10f39c..e22de4656 100644
--- a/flaml/automl.py
+++ b/flaml/automl.py
@@ -95,6 +95,7 @@ class SearchState:
time2eval = result['time2eval']
trained_estimator = result[
'trained_estimator']
+ del result['trained_estimator'] # free up RAM
else:
obj, time2eval, trained_estimator = np.inf, 0.0, None
train_loss = config = None
diff --git a/flaml/model.py b/flaml/model.py
index 40288480c..fa88c8e81 100644
--- a/flaml/model.py
+++ b/flaml/model.py
@@ -186,6 +186,13 @@ class SKLearnEstimator(BaseEstimator):
X = X.copy()
cat_columns = X.select_dtypes(include=['category']).columns
X[cat_columns] = X[cat_columns].apply(lambda x: x.cat.codes)
+ elif isinstance(X, np.ndarray) and X.dtype.kind not in 'buif':
+ # numpy array is not of numeric dtype
+ X = pd.DataFrame(X)
+ for col in X.columns:
+ if isinstance(X[col][0], str):
+ X[col] = X[col].astype('category').cat.codes
+ X = X.to_numpy()
return X
@@ -484,7 +491,8 @@ class RandomForestEstimator(SKLearnEstimator, LGBMEstimator):
@classmethod
def search_space(cls, data_size, task, **params):
- upper = min(2048, int(data_size))
+ data_size = int(data_size)
+ upper = min(2048, data_size)
space = {
'n_estimators': {
'domain': tune.lograndint(lower=4, upper=upper),
@@ -495,6 +503,11 @@ class RandomForestEstimator(SKLearnEstimator, LGBMEstimator):
'domain': tune.loguniform(lower=0.1, upper=1.0),
'init_value': 1.0,
},
+ 'max_leaves': {
+ 'domain': tune.lograndint(lower=4, upper=min(32768, data_size)),
+ 'init_value': 4,
+ 'low_cost_init_value': 4,
+ },
}
if task != 'regression':
space['criterion'] = {
@@ -503,17 +516,14 @@ class RandomForestEstimator(SKLearnEstimator, LGBMEstimator):
}
return space
- @classmethod
- def size(cls, config):
- return 1.0
-
@classmethod
def cost_relative2lgbm(cls):
return 2.0
def __init__(
self, task='binary:logistic', n_jobs=1,
- n_estimators=4, max_features=1.0, criterion='gini', **params
+ n_estimators=4, max_features=1.0, criterion='gini', max_leaves=4,
+ **params
):
super().__init__(task, **params)
del self.params['objective']
@@ -522,6 +532,7 @@ class RandomForestEstimator(SKLearnEstimator, LGBMEstimator):
"n_estimators": int(round(n_estimators)),
"n_jobs": n_jobs,
'max_features': float(max_features),
+ "max_leaf_nodes": params.get('max_leaf_nodes', int(round(max_leaves))),
})
if 'regression' in task:
self.estimator_class = RandomForestRegressor
@@ -781,4 +792,13 @@ class KNeighborsEstimator(BaseEstimator):
raise ValueError(
"kneighbor requires at least one numeric feature")
X = X.drop(cat_columns, axis=1)
+ elif isinstance(X, np.ndarray) and X.dtype.kind not in 'buif':
+ # drop categocial columns if any
+ X = pd.DataFrame(X)
+ cat_columns = []
+ for col in X.columns:
+ if isinstance(X[col][0], str):
+ cat_columns.append(col)
+ X = X.drop(cat_columns, axis=1)
+ X = X.to_numpy()
return X
diff --git a/flaml/searcher/suggestion.py b/flaml/searcher/suggestion.py
index ff20690f0..d49579fba 100644
--- a/flaml/searcher/suggestion.py
+++ b/flaml/searcher/suggestion.py
@@ -702,4 +702,4 @@ class OptunaSearch(Searcher):
for path, domain in domain_vars
}
- return values
\ No newline at end of file
+ return values
diff --git a/flaml/version.py b/flaml/version.py
index 40e294f71..db55ef192 100644
--- a/flaml/version.py
+++ b/flaml/version.py
@@ -1 +1 @@
-__version__ = "0.5.9"
+__version__ = "0.5.10"
diff --git a/test/test_automl.py b/test/test_automl.py
index 3f667529c..c23286e01 100644
--- a/test/test_automl.py
+++ b/test/test_automl.py
@@ -151,6 +151,30 @@ class TestAutoML(unittest.TestCase):
'''The main flaml automl API'''
automl.fit(X_train=X_train, y_train=y_train, **settings)
+ def test_preprocess(self):
+ automl = AutoML()
+ X = pd.DataFrame({
+ 'f1': [1, -2, 3, -4, 5, -6, -7, 8, -9, -10, -11, -12, -13, -14],
+ 'f2': [3., 16., 10., 12., 3., 14., 11., 12., 5., 14., 20., 16., 15., 11.,],
+ 'f3': ['a', 'b', 'a', 'c', 'c', 'b', 'b', 'b', 'b', 'a', 'b', 'e', 'e', 'a'],
+ 'f4': [True, True, False, True, True, False, False, False, True, True, False, False, True, True],
+ })
+ y = pd.Series([0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1])
+
+ automl_settings = {
+ "time_budget": 3,
+ "task": 'classification',
+ "n_jobs": 1,
+ "estimator_list": ['xgboost', 'catboost', 'kneighbor'],
+ "eval_method": "cv",
+ "n_splits": 3,
+ "metric": "accuracy",
+ "log_training_metric": True,
+ "verbose": 1,
+ "ensemble": True,
+ }
+ automl.fit(X, y, **automl_settings)
+
def test_dataframe(self):
self.test_classification(True)