Iris Multi-metric

[1]:
from sklearn_genetic import GASearchCV
from sklearn_genetic.space import Categorical, Integer, Continuous
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.metrics import make_scorer
from sklearn.metrics import balanced_accuracy_score

Import the data and split it in train and test sets

[2]:
data = load_iris()
X, y = data["data"], data["target"]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=0)

Define the GASearchCV options and Multi-metric

[3]:
clf = DecisionTreeClassifier()

params_grid = {
    "min_weight_fraction_leaf": Continuous(0, 0.5),
    "criterion": Categorical(["gini", "entropy"]),
    "max_depth": Integer(2, 20),
    "max_leaf_nodes": Integer(2, 30),
}

scoring = {"accuracy": "accuracy",
           "balanced_accuracy": make_scorer(balanced_accuracy_score)}

Define the GASearchCV options

[4]:
# Low number of generations and population
# Just to see the effect of multimetric
# In logbook and cv_results_

evolved_estimator = GASearchCV(
    clf,
    scoring=scoring,
    population_size=3,
    generations=2,
    crossover_probability=0.9,
    mutation_probability=0.05,
    param_grid=params_grid,
    algorithm="eaSimple",
    n_jobs=-1,
    verbose=True,
    error_score='raise',
    refit="accuracy")

Fit the model and see some results

[5]:
evolved_estimator.fit(X_train, y_train)
y_predict_ga = evolved_estimator.predict(X_test)
gen     nevals  fitness         fitness_std     fitness_max     fitness_min
0       3       0.856902        0.117921        0.940285        0.690137
1       2       0.940285        0               0.940285        0.940285
2       2       0.940285        0               0.940285        0.940285
[6]:
evolved_estimator.cv_results_

[6]:
{'param_min_weight_fraction_leaf': [0.22963955365985156,
  0.11807874354582698,
  0.4566955700628974,
  0.11807874354582698,
  0.22963955365985156,
  0.22963955365985156,
  0.22963955365985156],
 'param_criterion': ['gini',
  'entropy',
  'entropy',
  'gini',
  'entropy',
  'entropy',
  'gini'],
 'param_max_depth': [2, 9, 10, 2, 9, 2, 9],
 'param_max_leaf_nodes': [13, 7, 3, 7, 13, 13, 13],
 'split0_test_accuracy': [0.9117647058823529,
  0.9117647058823529,
  0.6764705882352942,
  0.9117647058823529,
  0.9117647058823529,
  0.9117647058823529,
  0.9117647058823529],
 'split1_test_accuracy': [0.9696969696969697,
  0.9696969696969697,
  0.696969696969697,
  0.9696969696969697,
  0.9696969696969697,
  0.9696969696969697,
  0.9696969696969697],
 'split2_test_accuracy': [0.9393939393939394,
  0.9393939393939394,
  0.696969696969697,
  0.9393939393939394,
  0.9393939393939394,
  0.9393939393939394,
  0.9393939393939394],
 'mean_test_accuracy': [0.9402852049910874,
  0.9402852049910874,
  0.690136660724896,
  0.9402852049910874,
  0.9402852049910874,
  0.9402852049910874,
  0.9402852049910874],
 'std_test_accuracy': [0.023659142890153965,
  0.023659142890153965,
  0.009663372529584432,
  0.023659142890153965,
  0.023659142890153965,
  0.023659142890153965,
  0.023659142890153965],
 'rank_test_accuracy': array([1, 1, 7, 1, 1, 1, 1]),
 'split0_train_accuracy': [0.9696969696969697,
  0.9696969696969697,
  0.696969696969697,
  0.9696969696969697,
  0.9696969696969697,
  0.9696969696969697,
  0.9696969696969697],
 'split1_train_accuracy': [0.9552238805970149,
  0.9552238805970149,
  0.6865671641791045,
  0.9552238805970149,
  0.9552238805970149,
  0.9552238805970149,
  0.9552238805970149],
 'split2_train_accuracy': [0.9701492537313433,
  0.9701492537313433,
  0.6865671641791045,
  0.9701492537313433,
  0.9701492537313433,
  0.9701492537313433,
  0.9701492537313433],
 'mean_train_accuracy': [0.9650233680084427,
  0.9650233680084427,
  0.6900346751093019,
  0.9650233680084427,
  0.9650233680084427,
  0.9650233680084427,
  0.9650233680084427],
 'std_train_accuracy': [0.006931743665052123,
  0.006931743665052123,
  0.004903800985162277,
  0.006931743665052123,
  0.006931743665052123,
  0.006931743665052123,
  0.006931743665052123],
 'rank_train_accuracy': array([1, 1, 7, 1, 1, 1, 1]),
 'split0_test_balanced_accuracy': [0.9090909090909092,
  0.9090909090909092,
  0.6666666666666666,
  0.9090909090909092,
  0.9090909090909092,
  0.9090909090909092,
  0.9090909090909092],
 'split1_test_balanced_accuracy': [0.9722222222222222,
  0.9722222222222222,
  0.6666666666666666,
  0.9722222222222222,
  0.9722222222222222,
  0.9722222222222222,
  0.9722222222222222],
 'split2_test_balanced_accuracy': [0.9333333333333332,
  0.9388888888888888,
  0.6666666666666666,
  0.9333333333333332,
  0.9388888888888888,
  0.9333333333333332,
  0.9333333333333332],
 'mean_test_balanced_accuracy': [0.9382154882154882,
  0.94006734006734,
  0.6666666666666666,
  0.9382154882154882,
  0.94006734006734,
  0.9382154882154882,
  0.9382154882154882],
 'std_test_balanced_accuracy': [0.02600342607735869,
  0.02578671796107406,
  0.0,
  0.02600342607735869,
  0.02578671796107406,
  0.02600342607735869,
  0.02600342607735869],
 'rank_test_balanced_accuracy': array([3, 1, 7, 3, 1, 3, 3]),
 'split0_train_balanced_accuracy': [0.9722222222222222,
  0.9722222222222222,
  0.6666666666666666,
  0.9722222222222222,
  0.9722222222222222,
  0.9722222222222222,
  0.9722222222222222],
 'split1_train_balanced_accuracy': [0.9551414768806072,
  0.9551414768806072,
  0.6666666666666666,
  0.9551414768806072,
  0.9551414768806072,
  0.9551414768806072,
  0.9551414768806072],
 'split2_train_balanced_accuracy': [0.9710144927536232,
  0.9710144927536232,
  0.6666666666666666,
  0.9710144927536232,
  0.9710144927536232,
  0.9710144927536232,
  0.9710144927536232],
 'mean_train_balanced_accuracy': [0.9661260639521508,
  0.9661260639521508,
  0.6666666666666666,
  0.9661260639521508,
  0.9661260639521508,
  0.9661260639521508,
  0.9661260639521508],
 'std_train_balanced_accuracy': [0.007782909373174586,
  0.007782909373174586,
  0.0,
  0.007782909373174586,
  0.007782909373174586,
  0.007782909373174586,
  0.007782909373174586],
 'rank_train_balanced_accuracy': array([1, 1, 7, 1, 1, 1, 1]),
 'mean_fit_time': [0.001999060312906901,
  0.0016531944274902344,
  0.0016682147979736328,
  0.0019936561584472656,
  0.0016682942708333333,
  0.0023442904154459634,
  0.0016681353251139324],
 'std_fit_time': [8.104673248279548e-07,
  0.00048135126754460846,
  0.0004735620798937051,
  8.939901952387178e-06,
  0.00047260810461652655,
  0.0004931964528559586,
  0.0004699654688748367],
 'mean_score_time': [0.0019881725311279297,
  0.0026591618855794272,
  0.0026796658833821616,
  0.001337607701619466,
  0.0013335545857747395,
  0.0023202896118164062,
  0.002334038416544596],
 'std_score_time': [1.2906940492414977e-05,
  0.0009508785819826155,
  0.0009365762649996311,
  0.00047234976131361057,
  0.00047080875797289405,
  0.0004528267864869186,
  0.00047109380912835715],
 'params': [{'min_weight_fraction_leaf': 0.22963955365985156,
   'criterion': 'gini',
   'max_depth': 2,
   'max_leaf_nodes': 13},
  {'min_weight_fraction_leaf': 0.11807874354582698,
   'criterion': 'entropy',
   'max_depth': 9,
   'max_leaf_nodes': 7},
  {'min_weight_fraction_leaf': 0.4566955700628974,
   'criterion': 'entropy',
   'max_depth': 10,
   'max_leaf_nodes': 3},
  {'min_weight_fraction_leaf': 0.11807874354582698,
   'criterion': 'gini',
   'max_depth': 2,
   'max_leaf_nodes': 7},
  {'min_weight_fraction_leaf': 0.22963955365985156,
   'criterion': 'entropy',
   'max_depth': 9,
   'max_leaf_nodes': 13},
  {'min_weight_fraction_leaf': 0.22963955365985156,
   'criterion': 'entropy',
   'max_depth': 2,
   'max_leaf_nodes': 13},
  {'min_weight_fraction_leaf': 0.22963955365985156,
   'criterion': 'gini',
   'max_depth': 9,
   'max_leaf_nodes': 13}]}
[7]:
evolved_estimator.logbook.chapters["parameters"]


[7]:
[{'index': 0,
  'min_weight_fraction_leaf': 0.22963955365985156,
  'criterion': 'gini',
  'max_depth': 2,
  'max_leaf_nodes': 13,
  'score': 0.9402852049910874,
  'cv_scores': array([0.91176471, 0.96969697, 0.93939394]),
  'fit_time': array([0.00199986, 0.00199795, 0.00199938]),
  'score_time': array([0.00197005, 0.00199533, 0.00199914]),
  'test_accuracy': array([0.91176471, 0.96969697, 0.93939394]),
  'train_accuracy': array([0.96969697, 0.95522388, 0.97014925]),
  'test_balanced_accuracy': array([0.90909091, 0.97222222, 0.93333333]),
  'train_balanced_accuracy': array([0.97222222, 0.95514148, 0.97101449])},
 {'index': 1,
  'min_weight_fraction_leaf': 0.11807874354582698,
  'criterion': 'entropy',
  'max_depth': 9,
  'max_leaf_nodes': 7,
  'score': 0.9402852049910874,
  'cv_scores': array([0.91176471, 0.96969697, 0.93939394]),
  'fit_time': array([0.00200057, 0.0019865 , 0.00097251]),
  'score_time': array([0.00200391, 0.00196981, 0.00400376]),
  'test_accuracy': array([0.91176471, 0.96969697, 0.93939394]),
  'train_accuracy': array([0.96969697, 0.95522388, 0.97014925]),
  'test_balanced_accuracy': array([0.90909091, 0.97222222, 0.93888889]),
  'train_balanced_accuracy': array([0.97222222, 0.95514148, 0.97101449])},
 {'index': 2,
  'min_weight_fraction_leaf': 0.4566955700628974,
  'criterion': 'entropy',
  'max_depth': 10,
  'max_leaf_nodes': 3,
  'score': 0.690136660724896,
  'cv_scores': array([0.67647059, 0.6969697 , 0.6969697 ]),
  'fit_time': array([0.00200272, 0.00200343, 0.0009985 ]),
  'score_time': array([0.00203657, 0.00199842, 0.004004  ]),
  'test_accuracy': array([0.67647059, 0.6969697 , 0.6969697 ]),
  'train_accuracy': array([0.6969697 , 0.68656716, 0.68656716]),
  'test_balanced_accuracy': array([0.66666667, 0.66666667, 0.66666667]),
  'train_balanced_accuracy': array([0.66666667, 0.66666667, 0.66666667])},
 {'index': 3,
  'min_weight_fraction_leaf': 0.11807874354582698,
  'criterion': 'gini',
  'max_depth': 2,
  'max_leaf_nodes': 7,
  'score': 0.9402852049910874,
  'cv_scores': array([0.91176471, 0.96969697, 0.93939394]),
  'fit_time': array([0.00200033, 0.00199962, 0.00198102]),
  'score_time': array([0.00200558, 0.00099778, 0.00100946]),
  'test_accuracy': array([0.91176471, 0.96969697, 0.93939394]),
  'train_accuracy': array([0.96969697, 0.95522388, 0.97014925]),
  'test_balanced_accuracy': array([0.90909091, 0.97222222, 0.93333333]),
  'train_balanced_accuracy': array([0.97222222, 0.95514148, 0.97101449])},
 {'index': 4,
  'min_weight_fraction_leaf': 0.22963955365985156,
  'criterion': 'entropy',
  'max_depth': 9,
  'max_leaf_nodes': 13,
  'score': 0.9402852049910874,
  'cv_scores': array([0.91176471, 0.96969697, 0.93939394]),
  'fit_time': array([0.00200105, 0.00200391, 0.00099993]),
  'score_time': array([0.00100136, 0.00099993, 0.00199938]),
  'test_accuracy': array([0.91176471, 0.96969697, 0.93939394]),
  'train_accuracy': array([0.96969697, 0.95522388, 0.97014925]),
  'test_balanced_accuracy': array([0.90909091, 0.97222222, 0.93888889]),
  'train_balanced_accuracy': array([0.97222222, 0.95514148, 0.97101449])},
 {'index': 5,
  'min_weight_fraction_leaf': 0.22963955365985156,
  'criterion': 'entropy',
  'max_depth': 2,
  'max_leaf_nodes': 13,
  'score': 0.9402852049910874,
  'cv_scores': array([0.91176471, 0.96969697, 0.93939394]),
  'fit_time': array([0.00304174, 0.00198984, 0.00200129]),
  'score_time': array([0.00296068, 0.00200129, 0.0019989 ]),
  'test_accuracy': array([0.91176471, 0.96969697, 0.93939394]),
  'train_accuracy': array([0.96969697, 0.95522388, 0.97014925]),
  'test_balanced_accuracy': array([0.90909091, 0.97222222, 0.93333333]),
  'train_balanced_accuracy': array([0.97222222, 0.95514148, 0.97101449])},
 {'index': 6,
  'min_weight_fraction_leaf': 0.22963955365985156,
  'criterion': 'gini',
  'max_depth': 9,
  'max_leaf_nodes': 13,
  'score': 0.9402852049910874,
  'cv_scores': array([0.91176471, 0.96969697, 0.93939394]),
  'fit_time': array([0.00200057, 0.0010035 , 0.00200033]),
  'score_time': array([0.00200343, 0.00300026, 0.00199842]),
  'test_accuracy': array([0.91176471, 0.96969697, 0.93939394]),
  'train_accuracy': array([0.96969697, 0.95522388, 0.97014925]),
  'test_balanced_accuracy': array([0.90909091, 0.97222222, 0.93333333]),
  'train_balanced_accuracy': array([0.97222222, 0.95514148, 0.97101449])}]