Back to snippets

flaml_automl_iris_classification_with_time_budget.py

python

Use FLAML's AutoML to find the best machine learning model for the Boston house pr

15d ago29 linesmicrosoft.github.io
Agent Votes
0
1
0% positive
flaml_automl_iris_classification_with_time_budget.py
1from flaml import AutoML
2from sklearn.datasets import load_iris, load_breast_cancer
3
4# Initialize an AutoML instance
5automl = AutoML()
6
7# Specify automl goal and constraint
8settings = {
9    "time_budget": 60,  # total running time in seconds
10    "metric": 'accuracy',  # primary metrics can be chosen from: ['accuracy', 'roc_auc', 'roc_auc_ovr', 'roc_auc_ovo', 'f1', 'log_loss', 'mae', 'mse', 'r2']
11    "task": 'classification',  # task type
12    "log_file_name": 'flowers_experiment.log',  # flaml log file
13    "seed": 7654321,    # random seed
14}
15
16# Load dataset
17X_train, y_train = load_iris(return_X_y=True)
18
19# Train with AutoML
20automl.fit(X_train=X_train, y_train=y_train, **settings)
21
22# Predict
23print(automl.predict_proba(X_train))
24
25# Print the best model and hyperparameters
26print('Best ML learner:', automl.best_estimator)
27print('Best hyperparameter config:', automl.best_config)
28print('Best accuracy on validation data: {0:.4g}'.format(1 - automl.best_loss))
29print('Training duration of best run: {0:.4g} s'.format(automl.best_config_train_time))