SimonBlanke /
Optimization-Metadata
| 1 | # Author: Simon Blanke |
||
| 2 | # Email: [email protected] |
||
| 3 | # License: MIT License |
||
| 4 | |||
| 5 | import time |
||
| 6 | import numpy as np |
||
| 7 | |||
| 8 | from sklearn.datasets import load_iris |
||
| 9 | from sklearn.model_selection import cross_val_score |
||
| 10 | from sklearn.tree import DecisionTreeClassifier |
||
| 11 | from hyperactive import Hyperactive |
||
| 12 | from hypermemory import delete_model |
||
| 13 | |||
| 14 | data = load_iris() |
||
| 15 | X, y = data.data, data.target |
||
| 16 | |||
| 17 | |||
| 18 | def test_long_term_memory_times(): |
||
| 19 | def _model_(para, X_train, y_train): |
||
| 20 | model = DecisionTreeClassifier(max_depth=para["max_depth"]) |
||
| 21 | scores = cross_val_score(model, X_train, y_train, cv=2) |
||
| 22 | |||
| 23 | return scores.mean() |
||
| 24 | |||
| 25 | search_config = {_model_: {"max_depth": range(2, 500)}} |
||
| 26 | |||
| 27 | delete_model(_model_) |
||
| 28 | |||
| 29 | c_time = time.time() |
||
| 30 | opt = Hyperactive(X, y, memory="long") |
||
| 31 | opt.search(search_config, n_iter=1000) |
||
| 32 | diff_time_0 = time.time() - c_time |
||
| 33 | |||
| 34 | c_time = time.time() |
||
| 35 | opt = Hyperactive(X, y, memory="long") |
||
| 36 | opt.search(search_config, n_iter=1000) |
||
| 37 | diff_time_1 = time.time() - c_time |
||
| 38 | |||
| 39 | assert diff_time_0 / 2 > diff_time_1 |
||
| 40 | |||
| 41 | |||
| 42 | View Code Duplication | def test_long_term_memory_with_data(): |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 43 | def model2(para, X_train, y_train): |
||
| 44 | model = DecisionTreeClassifier( |
||
| 45 | criterion=para["criterion"], max_depth=para["max_depth"] |
||
| 46 | ) |
||
| 47 | scores = cross_val_score(model, X_train, y_train, cv=2) |
||
| 48 | |||
| 49 | return scores.mean() |
||
| 50 | |||
| 51 | search_config = { |
||
| 52 | model2: {"criterion": ["gini", "entropy"], "max_depth": range(1, 11)} |
||
| 53 | } |
||
| 54 | |||
| 55 | opt = Hyperactive(X, y, memory="long") |
||
| 56 | opt.search(search_config) |
||
| 57 | |||
| 58 | opt = Hyperactive(X, y, memory="long") |
||
| 59 | opt.search(search_config) |
||
| 60 | |||
| 61 | |||
| 62 | View Code Duplication | def test_long_term_memory_without_data(): |
|
|
0 ignored issues
–
show
|
|||
| 63 | def model3(para, X_train, y_train): |
||
| 64 | model = DecisionTreeClassifier( |
||
| 65 | criterion=para["criterion"], max_depth=para["max_depth"] |
||
| 66 | ) |
||
| 67 | scores = cross_val_score(model, X_train, y_train, cv=2) |
||
| 68 | |||
| 69 | return scores.mean() |
||
| 70 | |||
| 71 | search_config = { |
||
| 72 | model3: {"criterion": ["gini", "entropy"], "max_depth": range(1, 11)} |
||
| 73 | } |
||
| 74 | |||
| 75 | opt = Hyperactive(X, y, memory="long") |
||
| 76 | opt.search(search_config, n_iter=0) |
||
| 77 | |||
| 78 | opt = Hyperactive(X, y, memory="long") |
||
| 79 | opt.search(search_config) |
||
| 80 | |||
| 81 | |||
| 82 | def test_long_term_memory_best_model(): |
||
| 83 | def model4(para, X_train, y_train): |
||
| 84 | model = DecisionTreeClassifier( |
||
| 85 | criterion=para["criterion"], max_depth=para["max_depth"] |
||
| 86 | ) |
||
| 87 | scores = cross_val_score(model, X_train, y_train, cv=2) |
||
| 88 | |||
| 89 | return scores.mean() |
||
| 90 | |||
| 91 | search_config = { |
||
| 92 | model4: {"criterion": ["gini", "entropy"], "max_depth": range(1, 11)} |
||
| 93 | } |
||
| 94 | |||
| 95 | opt1 = Hyperactive(X, y, memory="long") |
||
| 96 | opt1.search(search_config) |
||
| 97 | |||
| 98 | best_para = opt1.results[model4] |
||
| 99 | |||
| 100 | opt2 = Hyperactive(X, y, memory="long") |
||
| 101 | opt2.search(search_config, n_iter=0, init_config=opt1.results) |
||
| 102 | |||
| 103 | assert best_para == opt2.results[model4] |
||
| 104 | |||
| 105 | |||
| 106 | def test_long_term_memory_obj_storage(): |
||
| 107 | from sklearn.gaussian_process import GaussianProcessClassifier |
||
| 108 | |||
| 109 | from sklearn.gaussian_process.kernels import RBF, Matern, ConstantKernel |
||
| 110 | |||
| 111 | def model(para, X_train, y_train): |
||
| 112 | gpc = GaussianProcessClassifier(kernel=para["kernel"]) |
||
| 113 | scores = cross_val_score(gpc, X_train, y_train, cv=2) |
||
| 114 | |||
| 115 | return scores.mean() |
||
| 116 | |||
| 117 | search_config = {model: {"kernel": [RBF(), Matern(), ConstantKernel()]}} |
||
| 118 | |||
| 119 | opt1 = Hyperactive(X, y, memory="long") |
||
| 120 | opt1.search(search_config) |
||
| 121 | |||
| 122 | best_para = opt1.results[model] |
||
| 123 | |||
| 124 | opt2 = Hyperactive(X, y, memory="long") |
||
| 125 | opt2.search(search_config, n_iter=0, init_config=opt1.results) |
||
| 126 | |||
| 127 | assert best_para == opt2.results[model] |
||
| 128 | |||
| 129 | |||
| 130 | View Code Duplication | def test_long_term_memory_search_space_expansion(): |
|
|
0 ignored issues
–
show
|
|||
| 131 | def model5(para, X_train, y_train): |
||
| 132 | model = DecisionTreeClassifier(criterion=para["criterion"]) |
||
| 133 | scores = cross_val_score(model, X_train, y_train, cv=2) |
||
| 134 | |||
| 135 | return scores.mean() |
||
| 136 | |||
| 137 | search_config = {model5: {"criterion": ["gini", "entropy"]}} |
||
| 138 | |||
| 139 | opt = Hyperactive(X, y, memory="long") |
||
| 140 | opt.search(search_config) |
||
| 141 | |||
| 142 | def model5(para, X_train, y_train): |
||
| 143 | model = DecisionTreeClassifier( |
||
| 144 | criterion=para["criterion"], max_depth=para["max_depth"] |
||
| 145 | ) |
||
| 146 | scores = cross_val_score(model, X_train, y_train, cv=2) |
||
| 147 | |||
| 148 | return scores.mean() |
||
| 149 | |||
| 150 | search_config = { |
||
| 151 | model5: {"criterion": ["gini", "entropy"], "max_depth": range(1, 11)} |
||
| 152 | } |
||
| 153 | |||
| 154 | opt = Hyperactive(X, y, memory="long") |
||
| 155 | opt.search(search_config) |
||
| 156 | |||
| 157 | |||
| 158 | View Code Duplication | def test_long_term_memory_search_space_reduction(): |
|
|
0 ignored issues
–
show
|
|||
| 159 | def model6(para, X_train, y_train): |
||
| 160 | model = DecisionTreeClassifier( |
||
| 161 | criterion=para["criterion"], max_depth=para["max_depth"] |
||
| 162 | ) |
||
| 163 | scores = cross_val_score(model, X_train, y_train, cv=2) |
||
| 164 | |||
| 165 | return scores.mean() |
||
| 166 | |||
| 167 | search_config = { |
||
| 168 | model6: {"criterion": ["gini", "entropy"], "max_depth": range(1, 11)} |
||
| 169 | } |
||
| 170 | |||
| 171 | opt = Hyperactive(X, y, memory="long") |
||
| 172 | opt.search(search_config) |
||
| 173 | |||
| 174 | def model6(para, X_train, y_train): |
||
| 175 | model = DecisionTreeClassifier(criterion=para["criterion"]) |
||
| 176 | scores = cross_val_score(model, X_train, y_train, cv=2) |
||
| 177 | |||
| 178 | return scores.mean() |
||
| 179 | |||
| 180 | search_config = {model6: {"criterion": ["gini", "entropy"]}} |
||
| 181 | |||
| 182 | opt = Hyperactive(X, y, memory="long") |
||
| 183 | opt.search(search_config) |
||
| 184 |