|
| 1 | +""" |
| 2 | +====================== |
| 3 | +Tabular Classification |
| 4 | +====================== |
| 5 | +
|
| 6 | +The following example shows how to fit a sample classification model parallely on 2 cores |
| 7 | +with AutoPyTorch |
| 8 | +""" |
| 9 | +import os |
| 10 | +import tempfile as tmp |
| 11 | +import warnings |
| 12 | + |
| 13 | +os.environ['JOBLIB_TEMP_FOLDER'] = tmp.gettempdir() |
| 14 | +os.environ['OMP_NUM_THREADS'] = '1' |
| 15 | +os.environ['OPENBLAS_NUM_THREADS'] = '1' |
| 16 | +os.environ['MKL_NUM_THREADS'] = '1' |
| 17 | + |
| 18 | +warnings.simplefilter(action='ignore', category=UserWarning) |
| 19 | +warnings.simplefilter(action='ignore', category=FutureWarning) |
| 20 | + |
| 21 | +import sklearn.datasets |
| 22 | +import sklearn.model_selection |
| 23 | + |
| 24 | +from autoPyTorch.api.tabular_classification import TabularClassificationTask |
| 25 | + |
| 26 | +if __name__ == '__main__': |
| 27 | + ############################################################################ |
| 28 | + # Data Loading |
| 29 | + # ============ |
| 30 | + X, y = sklearn.datasets.fetch_openml(data_id=40981, return_X_y=True, as_frame=True) |
| 31 | + X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split( |
| 32 | + X, |
| 33 | + y, |
| 34 | + random_state=1, |
| 35 | + ) |
| 36 | + |
| 37 | + ############################################################################ |
| 38 | + # Build and fit a classifier |
| 39 | + # ========================== |
| 40 | + api = TabularClassificationTask( |
| 41 | + n_jobs=2, |
| 42 | + seed=42, |
| 43 | + ) |
| 44 | + |
| 45 | + ############################################################################ |
| 46 | + # Search for an ensemble of machine learning algorithms |
| 47 | + # ===================================================== |
| 48 | + api.search( |
| 49 | + X_train=X_train, |
| 50 | + y_train=y_train, |
| 51 | + X_test=X_test.copy(), |
| 52 | + y_test=y_test.copy(), |
| 53 | + optimize_metric='accuracy', |
| 54 | + total_walltime_limit=300, |
| 55 | + func_eval_time_limit_secs=50, |
| 56 | + # Each one of the 2 jobs is allocated 3GB |
| 57 | + memory_limit=3072, |
| 58 | + ) |
| 59 | + |
| 60 | + ############################################################################ |
| 61 | + # Print the final ensemble performance |
| 62 | + # ==================================== |
| 63 | + print(api.run_history, api.trajectory) |
| 64 | + y_pred = api.predict(X_test) |
| 65 | + score = api.score(y_pred, y_test) |
| 66 | + print(score) |
| 67 | + # Print the final ensemble built by AutoPyTorch |
| 68 | + print(api.show_models()) |
0 commit comments