Skip to content

Commit

Permalink
move feature usage to hypergbm
Browse files Browse the repository at this point in the history
  • Loading branch information
oaksharks committed May 5, 2023
1 parent f73040d commit ededd1c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 17 deletions.
15 changes: 1 addition & 14 deletions hypernets/model/objectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

random_state = get_random_state()


def calc_psi(x_array, y_array, n_bins=10, eps=1e-6):
def calc_ratio(y_proba):
y_proba_1d = y_proba.reshape(1, -1)
Expand Down Expand Up @@ -131,18 +132,6 @@ def _evaluate_cv(self, trial, estimator, X_trains, y_trains, X_vals, y_vals, X_t
return self._get_psi_score(estimator, X_train=X_train, X_test=X_test)


class FeatureUsageObjective(Objective):
def __init__(self):
super(FeatureUsageObjective, self).__init__('feature_usage', 'min', need_train_data=False,
need_val_data=True, need_test_data=False)

def _evaluate(self, trial, estimator, X_train, y_train, X_val, y_val, X_test=None, **kwargs) -> float:
return estimator.data_pipeline[0].features[0][1].steps[0][1].feature_usage()

def _evaluate_cv(self, trial, estimator, X_trains, y_trains, X_vals, y_vals, X_test=None, **kwargs) -> float:
return estimator.cv_models_[0].data_pipeline[0].features[0][1].steps[0][1].feature_usage()


class ElapsedObjective(Objective):

def __init__(self):
Expand Down Expand Up @@ -368,8 +357,6 @@ def copy_opt(opt_names):
elif name == 'psi':
copy_opt(['n_bins', 'task', 'average', 'eps'])
return PSIObjective(**opts)
elif name == 'feature_usage':
return FeatureUsageObjective()
elif name == 'pred_perf':
return PredictionPerformanceObjective()
else:
Expand Down
5 changes: 2 additions & 3 deletions hypernets/tests/searchers/test_nsga2_searcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,9 @@ class TestRNSGA2:

@pytest.mark.parametrize('recombination', ["shuffle", "uniform", "single_point"])
@pytest.mark.parametrize('cv', [True, False])
@pytest.mark.parametrize('objective', ['feature_usage', 'nf'])
def test_nsga2_training(self, recombination: str, cv: bool, objective: str):
def test_nsga2_training(self, recombination: str, cv: bool):
set_random_state(1234)
hk1 = self.run_nsga2_training(recombination=const.COMBINATION_SHUFFLE, cv=cv, objective=objective)
hk1 = self.run_nsga2_training(recombination=const.COMBINATION_SHUFFLE, cv=cv, objective='nf')
pop1 = hk1.searcher.get_historical_population()
scores1 = np.asarray([indi.scores for indi in pop1])
assert scores1.ndim == 2
Expand Down

0 comments on commit ededd1c

Please sign in to comment.