| Total Complexity | 3 |
| Total Lines | 51 |
| Duplicated Lines | 19.61 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | # Author: Simon Blanke |
||
| 2 | # Email: [email protected] |
||
| 3 | # License: MIT License |
||
| 4 | |||
| 5 | from sklearn.datasets import load_iris |
||
| 6 | from sklearn.model_selection import cross_val_score |
||
| 7 | from sklearn.tree import DecisionTreeClassifier |
||
| 8 | from hyperactive import Hyperactive |
||
| 9 | from hyperactive import MetaLearn |
||
| 10 | |||
| 11 | data = load_iris() |
||
| 12 | X = data.data |
||
| 13 | y = data.target |
||
| 14 | |||
| 15 | |||
| 16 | View Code Duplication | def model(para, X_train, y_train): |
|
|
|
|||
| 17 | model = DecisionTreeClassifier( |
||
| 18 | criterion=para["criterion"], |
||
| 19 | max_depth=para["max_depth"], |
||
| 20 | min_samples_split=para["min_samples_split"], |
||
| 21 | min_samples_leaf=para["min_samples_leaf"], |
||
| 22 | ) |
||
| 23 | scores = cross_val_score(model, X_train, y_train, cv=3) |
||
| 24 | |||
| 25 | return scores.mean(), model |
||
| 26 | |||
| 27 | |||
| 28 | search_config = { |
||
| 29 | model: { |
||
| 30 | "criterion": ["gini", "entropy"], |
||
| 31 | "max_depth": range(1, 21), |
||
| 32 | "min_samples_split": range(2, 21), |
||
| 33 | "min_samples_leaf": range(1, 21), |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | |||
| 38 | def test_metalearn(): |
||
| 39 | ml = MetaLearn(search_config) |
||
| 40 | ml.collect(X, y) |
||
| 41 | # ml.train() |
||
| 42 | # ml.search(X, y) |
||
| 43 | |||
| 44 | |||
| 45 | def test_metalearn1(): |
||
| 46 | opt = Hyperactive(search_config, meta_learn=True) |
||
| 47 | opt.fit(X, y) |
||
| 48 | |||
| 49 | |||
| 50 | test_metalearn() |
||
| 51 |