Skip to content

Commit

Permalink
Allow newer autosklearn versions to use the pandas data instead (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
PGijsbers authored Jun 16, 2023
1 parent 4a38f6c commit 6c9ccb3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
14 changes: 8 additions & 6 deletions frameworks/autosklearn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ def setup(*args, **kwargs):
def run(dataset: Dataset, config: TaskConfig):
from frameworks.shared.caller import run_in_venv

X_train, X_test = dataset.train.X_enc, dataset.test.X_enc
y_train, y_test = unsparsify(dataset.train.y_enc, dataset.test.y_enc)
data = dict(
train=dict(
X=X_train,
y=y_train
X=dataset.train.X,
y=dataset.train.y,
X_enc=dataset.train.X_enc,
y_enc=unsparsify(dataset.train.y_enc),
),
test=dict(
X=X_test,
y=y_test
X=dataset.test.X,
y=dataset.test.y,
X_enc=dataset.test.X_enc,
y_enc=unsparsify(dataset.test.y_enc),
),
predictors_type=['Numerical' if p.is_numerical() else 'Categorical' for p in dataset.predictors],
inference_subsample_files=dataset.inference_subsample_files(fmt="parquet"),
Expand Down
28 changes: 18 additions & 10 deletions frameworks/autosklearn/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ def run(dataset, config):
)
log.info("Environment: %s", os.environ)

X_train = dataset.train.X
y_train = dataset.train.y
use_pandas = askl_version >= version.parse("0.15")
X_train = dataset.train.X if use_pandas else dataset.train.X_enc
y_train = dataset.train.y if use_pandas else dataset.train.y_enc
predictors_type = dataset.predictors_type
log.debug("predictors_type=%s", predictors_type)

Expand Down Expand Up @@ -123,6 +124,10 @@ def run(dataset, config):
else:
fit_extra_params['metric'] = perf_metric

if not use_pandas:
fit_extra_params["feat_type"] = predictors_type


constr_params["time_left_for_this_task"] = config.max_runtime_seconds
constr_params["n_jobs"] = n_jobs
constr_params["seed"] = config.seed
Expand All @@ -133,7 +138,7 @@ def run(dataset, config):

auto_sklearn = estimator(**constr_params, **training_params)
with Timer() as training:
auto_sklearn.fit(X_train, y_train, feat_type=predictors_type, **fit_extra_params)
auto_sklearn.fit(X_train, y_train, **fit_extra_params)

def infer(data: Union[str, pd.DataFrame]):
test_data = pd.read_parquet(data) if isinstance(data, str) else data
Expand All @@ -143,27 +148,30 @@ def infer(data: Union[str, pd.DataFrame]):
inference_times = {}
if config.measure_inference_time:
inference_times["file"] = measure_inference_times(infer, dataset.inference_subsample_files)
test_data = dataset.test.X if use_pandas else dataset.test.X_enc
def sample_one_test_row(seed: int):
if use_pandas:
return test_data.sample(1, random_state=seed)
return test_data[default_rng(seed=seed).integers(len(test_data)), :]

inference_times["df"] = measure_inference_times(
infer, [
(1, dataset.test.X[default_rng(seed=i).integers(len(dataset.test.X)), :].reshape(1, -1))
for i in range(100)
],
infer, [(1, sample_one_test_row(seed=i)) for i in range(100)],
)

# Convert output to strings for classification
log.info("Predicting on the test set.")
with Timer() as predict:
X_test = dataset.test.X
X_test = dataset.test.X if use_pandas else dataset.test.X_enc
predictions = auto_sklearn.predict(X_test)
probabilities = auto_sklearn.predict_proba(X_test) if is_classification else None

save_artifacts(auto_sklearn, config)

return result(output_file=config.output_predictions_file,
predictions=predictions,
truth=dataset.test.y,
truth=dataset.test.y if use_pandas else dataset.test.y_enc,
probabilities=probabilities,
target_is_encoded=is_classification,
target_is_encoded=is_classification and not use_pandas,
models_count=len(auto_sklearn.get_models_with_weights()),
training_duration=training.duration,
predict_duration=predict.duration,
Expand Down

0 comments on commit 6c9ccb3

Please sign in to comment.