|
| 1 | +# -*- encoding: utf-8 -*- |
| 2 | +import sklearn.model_selection |
| 3 | +import sklearn.datasets |
| 4 | +import sklearn.metrics |
| 5 | + |
| 6 | +import autosklearn.classification |
| 7 | + |
| 8 | +try: |
| 9 | + import openml |
| 10 | +except ImportError: |
| 11 | + print("#"*80 + """ |
| 12 | + To run this example you need to install openml-python: |
| 13 | +
|
| 14 | + git+https://github.com/renatopp/liac-arff |
| 15 | + # OpenML is currently not on pypi, use an old version to not depend on |
| 16 | + # scikit-learn 0.18 |
| 17 | + requests |
| 18 | + xmltodict |
| 19 | + git+https://github.com/renatopp/liac-arff |
| 20 | + git+https://github.com/openml/""" + |
| 21 | + "openml-python@0b9009b0436fda77d9f7c701bd116aff4158d5e1\n""" + |
| 22 | + "#"*80) |
| 23 | + raise |
| 24 | + |
| 25 | + |
| 26 | +def main(): |
| 27 | + # Load adult dataset from openml.org, see https://www.openml.org/t/2117 |
| 28 | + openml.config.apikey = '610344db6388d9ba34f6db45a3cf71de' |
| 29 | + |
| 30 | + task = openml.tasks.get_task(2117) |
| 31 | + train_indices, test_indices = task.get_train_test_split_indices() |
| 32 | + X, y = task.get_X_and_y() |
| 33 | + |
| 34 | + X_train = X[train_indices] |
| 35 | + y_train = y[train_indices] |
| 36 | + X_test = X[test_indices] |
| 37 | + y_test = y[test_indices] |
| 38 | + |
| 39 | + dataset = task.get_dataset() |
| 40 | + _, _, categorical_indicator = dataset.\ |
| 41 | + get_data(target=task.target_name, return_categorical_indicator=True) |
| 42 | + |
| 43 | + # Create feature type list from openml.org indicator and run autosklearn |
| 44 | + feat_type = ['categorical' if ci else 'numerical' |
| 45 | + for ci in categorical_indicator] |
| 46 | + |
| 47 | + cls = autosklearn.classification.\ |
| 48 | + AutoSklearnClassifier(time_left_for_this_task=120, |
| 49 | + per_run_time_limit=30) |
| 50 | + cls.fit(X_train, y_train, feat_type=feat_type) |
| 51 | + |
| 52 | + predictions = cls.predict(X_test) |
| 53 | + print("Accuracy score", sklearn.metrics.accuracy_score(y_test, predictions)) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + main() |
0 commit comments