| 1 |  |  | import pytest | 
            
                                                        
            
                                    
            
            
                | 2 |  |  | import numpy as np | 
            
                                                        
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 4 |  |  | from sklearn import svm, datasets | 
            
                                                        
            
                                    
            
            
                | 5 |  |  | from sklearn.naive_bayes import GaussianNB | 
            
                                                        
            
                                    
            
            
                | 6 |  |  | from sklearn.isotonic import IsotonicRegression | 
            
                                                        
            
                                    
            
            
                | 7 |  |  | from sklearn.decomposition import PCA | 
            
                                                        
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 10 |  |  | from sklearn.utils.validation import check_is_fitted | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 12 |  |  | from hyperactive.integrations import HyperactiveSearchCV | 
            
                                                        
            
                                    
            
            
                | 13 |  |  | from hyperactive.optimizers import RandomSearchOptimizer | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 16 |  |  | iris = datasets.load_iris() | 
            
                                                        
            
                                    
            
            
                | 17 |  |  | X, y = iris.data, iris.target | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 20 |  |  | ir = IsotonicRegression() | 
            
                                                        
            
                                    
            
            
                | 21 |  |  | nb = GaussianNB() | 
            
                                                        
            
                                    
            
            
                | 22 |  |  | svc = svm.SVC() | 
            
                                                        
            
                                    
            
            
                | 23 |  |  | pca = PCA(n_components=2) | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 26 |  |  | parameters = {"kernel": ["linear", "rbf"], "C": [1, 10]} | 
            
                                                        
            
                                    
            
            
                | 27 |  |  | opt = RandomSearchOptimizer() | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 30 |  |  | def test_fit(): | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |     search = HyperactiveSearchCV(svc, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |     check_is_fitted(search) | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 37 |  |  | def test_score(): | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |     search = HyperactiveSearchCV(svc, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |     score = search.score(X, y) | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |     assert isinstance(score, float) | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 45 |  |  | def test_classes_(): | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |     search = HyperactiveSearchCV(svc, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |     assert [0, 1, 2] == list(search.classes_) | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 52 |  |  | def test_score_samples(): | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |     search = HyperactiveSearchCV(svc, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |     with pytest.raises(AttributeError): | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         search.score_samples(X) | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 60 |  |  | def test_predict(): | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |     search = HyperactiveSearchCV(svc, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |     result = search.predict(X) | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |     assert isinstance(result, np.ndarray) | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 68 |  |  | def test_predict_proba(): | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |     search = HyperactiveSearchCV(svc, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |     with pytest.raises(AttributeError): | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         search.predict_proba(X) | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |     search = HyperactiveSearchCV(nb, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |     result = search.predict(X) | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |     assert isinstance(result, np.ndarray) | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 82 |  |  | def test_predict_log_proba(): | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |     search = HyperactiveSearchCV(svc, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |     with pytest.raises(AttributeError): | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |         search.predict_log_proba(X) | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     search = HyperactiveSearchCV(nb, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |     result = search.predict_log_proba(X) | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |     assert isinstance(result, np.ndarray) | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 96 |  |  | def test_decision_function(): | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |     search = HyperactiveSearchCV(svc, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |     result = search.decision_function(X) | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |     assert isinstance(result, np.ndarray) | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 104 |  |  | def test_transform(): | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |     search = HyperactiveSearchCV(svc, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |     with pytest.raises(AttributeError): | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |         search.transform(X) | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |     search = HyperactiveSearchCV(pca, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |     result = search.transform(X) | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |     assert isinstance(result, np.ndarray) | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 118 |  |  | def test_inverse_transform(): | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |     search = HyperactiveSearchCV(svc, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |     with pytest.raises(AttributeError): | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |         search.inverse_transform(X) | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |     search = HyperactiveSearchCV(pca, opt, parameters) | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |     search.fit(X, y) | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |     result = search.inverse_transform(search.transform(X)) | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |     assert isinstance(result, np.ndarray) | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |  |