def test_get_best_estimator(self) -> None: """ Test that the 'get_best_estimator' method returns a value """ model = GSCV(self.valid_clf, self.x, self.y) best_model = model.get_best_estimator() self.assertTrue(best_model is not None)
def test_get_fit_details(self) -> None: """ Test that the 'get_fit_details' method returns a value """ model = GSCV(self.valid_clf, self.x, self.y) fit_details = model.get_fit_details() self.assertTrue(fit_details is not None)
def test_create(self) -> None: """ Test that the create method returns a value """ model = GSCV(self.valid_clf, self.x, self.y) result = model.create() self.assertTrue(result is not None)
def test_score(self) -> None: """ Test that the create method returns a value """ model = GSCV(self.valid_clf, self.x, self.y) score = model.score() self.assertTrue(score is not None) self.assertTrue(0 <= score <= 1)
def test_sklearn_classifier(self) -> None: """ Test that object accepts a sklearn classifier """ clf = KNeighborsClassifier() model = GSCV(clf, self.x, self.y) self.assertEqual(type(model._get_model(clf)), type(KNeighborsClassifier()))
def test_string(self) -> None: """ Test that object accepts a string """ clf = 'KNeighborsClassifier' model = GSCV(clf, self.x, self.y) self.assertEqual(type(model._get_model(clf)), type(KNeighborsClassifier()))
def test_get_fit_details(self) -> None: """ Test that the 'get_fit_details' method returns a value """ model = GSCV(self.valid_clf, self.x, self.y, params={'alpha': [0.0001, 0.01, 0.1, 1]}) fit_details = model.get_fit_details() self.assertTrue(fit_details is not None)
def test_get_best_estimator(self) -> None: """ Test that the 'get_best_estimator' method returns a value """ model = GSCV(self.valid_clf, self.x, self.y, params={'alpha': [0.0001, 0.01, 0.1, 1]}) best_model = model.get_best_estimator() self.assertTrue(best_model is not None)
def test_create(self) -> None: """ Test that the create method returns a value """ model = GSCV(self.valid_clf, self.x, self.y, params={'alpha': [0.0001, 0.01, 0.1, 1]}) result = model.create() self.assertTrue(result is not None)
def test_score(self) -> None: """ Test that the create method returns a value """ model = GSCV(self.valid_clf, self.x, self.y, params={'alpha': [0.0001, 0.01, 0.1, 1]}) score = model.score() self.assertTrue(score is not None) self.assertTrue(0 <= score <= 1)
def test_create(self) -> None: """Test that the create method returns a value""" model = GSCV(self.valid_clf, self.x, self.y, n_jobs=-1, params={ 'n_estimators': [100], 'learning_rate': [0.1], 'max_depth': [1], 'max_features': ['sqrt'], }) result = model.create() self.assertTrue(result is not None)
def test_get_fit_details(self) -> None: """Test that the 'get_fit_details' method returns a value""" model = GSCV(self.valid_clf, self.x, self.y, n_jobs=-1, params={ 'n_estimators': [100], 'learning_rate': [0.1], 'max_depth': [1], 'max_features': ['sqrt'], }) fit_details = model.get_fit_details() self.assertTrue(fit_details is not None)
def test_get_best_estimator(self) -> None: """ Test that the 'get_best_estimator' method returns a value """ model = GSCV(self.valid_clf, self.x, self.y, n_jobs=-1, params={ 'n_estimators': [100], 'max_features': ['sqrt'], 'max_depth': [3], }) best_model = model.get_best_estimator() self.assertTrue(best_model is not None)
def test_check_wrong_scikit_model(self) -> None: """ Make sure that using a non-valid sklearn classifier raises the correct error message """ with self.assertRaises(ValueError): GSCV(self.wrong_clf, self.x, self.y)
def test_custom_params(self) -> None: """Test that custom params override the default ones""" model = GSCV(self.valid_clf, self.x, self.y, params={'alpha': [0.0001, 0.01, 0.1, 1]}) self.assertEqual(model.params, {'alpha': [0.0001, 0.01, 0.1, 1]})
def test_check_model_not_a_model(self) -> None: """ Make sure that using a type that is not a model raises the correct error message. """ with self.assertRaises(ValueError): GSCV('doesnotexist', self.x, self.y)
def test_score(self) -> None: """ Test that the create method returns a value """ model = GSCV(self.valid_clf, self.x, self.y, n_jobs=-1, params={ 'n_estimators': [100], 'max_features': ['sqrt'], 'max_depth': [3], }) score = model.score() self.assertTrue(score is not None) self.assertTrue(0 <= score <= 1)
def test_check_model_not_a_sklearn_model(self) -> None: """ Make sure that using a non-sklearn model raises the correct error message. """ with self.assertRaises(TypeError): GSCV(self.wrong_object, self.x, self.y)
def test_default_params(self) -> None: """ Make sure that the correct default parameters are selected for the model """ model = GSCV(self.valid_clf, self.x, self.y) self.assertEqual(model.params, { 'n_neighbors': [3, 5, 8, 10, 15], 'weights': ['uniform', 'distance'], })
def test_default_params(self) -> None: """ Make sure that the correct default parameters are selected for the model """ model = GSCV(self.valid_clf, self.x, self.y) self.assertEqual(model.params, { 'C': [0.01, 1, 100], 'penalty': ['l1', 'l2'] })
def test_custom_params(self) -> None: """Test that custom params override the default ones""" model = GSCV(self.valid_clf, self.x, self.y, params={ 'C': [1, 100], 'penalty': ['l2'] }) self.assertEqual(model.params, {'C': [1, 100], 'penalty': ['l2']})
def test_default_params(self) -> None: """ Make sure that the correct default parameters are selected for the model """ model = GSCV(self.valid_clf, self.x, self.y) self.assertEqual( model.params, { 'hidden_layer_sizes': [(10, ), (10, 10), (10, 10, 10)], 'alpha': [0.0001, 0.01, 0.1, 1], 'solver': ['lbfgs'], })
def test_custom_params(self) -> None: """Test that custom params override the default ones""" model = GSCV(self.valid_clf, self.x, self.y, params={ 'n_neighbors': [3, 15], 'weights': ['uniform'], }) self.assertEqual(model.params, { 'n_neighbors': [3, 15], 'weights': ['uniform'], })
def test_default_params(self) -> None: model = GSCV(self.valid_clf, self.x, self.y, n_jobs=-1) self.assertEqual(model.params, [ { 'kernel': ['rbf'], 'C': [0.1, 1, 100, 1000], 'gamma': [0.01, 0.1, 1, 10, 'auto'] }, { 'kernel': ['poly'], 'degree': [1, 2, 3, 4], 'coef0': [0.0, 1], 'C': [0.1, 1, 100, 1000], 'gamma': [0.01, 0.1, 1, 10, 'auto'] }, ])
def setUp(self) -> None: # Get data from sklearn import datasets iris = datasets.load_iris() self.x = iris['data'] self.y = iris['target'] self.clf = KNeighborsClassifier() self.model = GSCV(self.clf, self.x, self.y) self.model_dict = { 'KNeighborsClassifier': KNeighborsClassifier(), 'RandomForestClassifier': RandomForestClassifier(), 'GradientBoostingClassifier': GradientBoostingClassifier(), 'MLPClassifier': MLPClassifier(), 'LogisticRegression': LogisticRegression(), 'SVC': SVC() }
def test_custom_params(self) -> None: """Test that custom params override the default ones""" model = GSCV(self.valid_clf, self.x, self.y, n_jobs=-1, params={ 'n_estimators': [100], 'max_features': ['sqrt'], 'max_depth': [3], }) self.assertEqual(model.params, { 'n_estimators': [100], 'max_features': ['sqrt'], 'max_depth': [3], })
def test_custom_params(self) -> None: """Test that custom params override the default ones""" model = GSCV(self.valid_clf, self.x, self.y, n_jobs=-1, params=[ { 'kernel': ['rbf'], 'C': [100, 1000], 'gamma': [0.01, 'auto'] }, ]) self.assertEqual(model.params, [ { 'kernel': ['rbf'], 'C': [100, 1000], 'gamma': [0.01, 'auto'] }, ])