| 1 |  |  | # Author: Simon Blanke | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # Email: [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # License: MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import pytest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from sklearn.datasets import load_iris | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | from sklearn.tree import DecisionTreeClassifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | from sklearn.model_selection import cross_val_score | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | from hyperactive import Hyperactive | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | data = load_iris() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | X = data.data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | y = data.target | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | def model(para, X_train, y_train): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     model = DecisionTreeClassifier( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         criterion=para["criterion"], max_depth=para["max_depth"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     scores = cross_val_score(model, X_train, y_train, cv=2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     return scores.mean() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | search_config = {model: {"criterion": ["gini", "entropy"], "max_depth": range(1, 21)}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | def test_checks_X(): | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     with pytest.raises(ValueError): | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         opt = Hyperactive(1, y) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | def test_checks_y(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         opt = Hyperactive(X, 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | def test_checks_memory(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         opt = Hyperactive(X, y, memory=None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | def test_checks_random_state(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         opt = Hyperactive(X, y, random_state=None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | def test_checks_verbosity(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         opt = Hyperactive(X, y, verbosity=None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 58 |  | View Code Duplication | def test_checks_search_config(): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         opt = Hyperactive(X, y, verbosity=None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         opt.search(1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     search_config = {1} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         opt = Hyperactive(X, y, verbosity=None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         opt.search(search_config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     search_config = {model: 1} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         opt = Hyperactive(X, y, verbosity=None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         opt.search(search_config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | def test_checks_n_iter(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         opt = Hyperactive(X, y) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         opt.search(search_config, n_iter=0.1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | def test_checks_max_time(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         opt = Hyperactive(X, y) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         opt.search(search_config, max_time="1") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | def test_checks_optimizer(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         opt = Hyperactive(X, y) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         opt.search(search_config, optimizer=1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | def test_checks_n_jobs(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         opt = Hyperactive(X, y) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         opt.search(search_config, n_jobs=0.1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | def test_checks_scheduler(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         opt = Hyperactive(X, y) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         opt.search(search_config, scheduler=1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 112 |  | View Code Duplication | def test_checks_init_config(): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         opt = Hyperactive(X, y, verbosity=None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         opt.search(search_config, init_config=1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     init_config = {1} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         opt = Hyperactive(X, y, verbosity=None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         opt.search(search_config, init_config=init_config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     init_config = {model: 1} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     with pytest.raises(ValueError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         opt = Hyperactive(X, y, verbosity=None) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  |         opt.search(search_config, init_config=init_config) | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  |