| Total Complexity | 2 | 
| Total Lines | 47 | 
| Duplicated Lines | 21.28 % | 
| 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 | |||
| 10 | data = load_iris()  | 
            ||
| 11 | X = data.data  | 
            ||
| 12 | y = data.target  | 
            ||
| 13 | |||
| 14 | n_iter = 1  | 
            ||
| 15 | |||
| 16 | |||
| 17 | View Code Duplication | def model(para, X_train, y_train):  | 
            |
| 
                                                                                                    
                        
                         | 
                |||
| 18 | model = DecisionTreeClassifier(  | 
            ||
| 19 | criterion=para["criterion"],  | 
            ||
| 20 | max_depth=para["max_depth"],  | 
            ||
| 21 | min_samples_split=para["min_samples_split"],  | 
            ||
| 22 | min_samples_leaf=para["min_samples_leaf"],  | 
            ||
| 23 | )  | 
            ||
| 24 | scores = cross_val_score(model, X_train, y_train, cv=2)  | 
            ||
| 25 | |||
| 26 | return scores.mean()  | 
            ||
| 27 | |||
| 28 | |||
| 29 | search_config = { | 
            ||
| 30 |     model: { | 
            ||
| 31 | "criterion": ["gini", "entropy"],  | 
            ||
| 32 | "max_depth": range(1, 11),  | 
            ||
| 33 | "min_samples_split": range(2, 11),  | 
            ||
| 34 | "min_samples_leaf": range(1, 11),  | 
            ||
| 35 | }  | 
            ||
| 36 | }  | 
            ||
| 37 | |||
| 38 | |||
| 39 | def test_get_results():  | 
            ||
| 40 | opt = Hyperactive(search_config, n_iter=n_iter, optimizer="HillClimbing")  | 
            ||
| 41 | opt.search(X, y)  | 
            ||
| 42 | opt.get_results()  | 
            ||
| 43 | |||
| 44 | opt = Hyperactive(search_config, n_iter=n_iter, n_jobs=2, optimizer="HillClimbing")  | 
            ||
| 45 | opt.search(X, y)  | 
            ||
| 46 | opt.get_results()  | 
            ||
| 47 |