def test_simple_example_params(digits, param, expected): X_train, X_test, y_train, y_test = digits mod = BasicSGDClassifier(**{param: expected}) mod.fit(X_train, y_train) preds = mod.predict(X_test) acc = accuracy_score(y_test, preds) if not (param == "max_iter" and expected <= 1): assert acc >= 0.90
def fit_basic_sgd_classifier_with_crossvalidation(X, y): basemod = BasicSGDClassifier() cv = 5 param_grid = {'eta': [0.01, 0.1, 1.0], 'max_iter': [10]} best_mod = utils.fit_classifier_with_crossvalidation( X, y, basemod, cv, param_grid) return best_mod
def fit_basic_sgd_classifier(X, y): """Wrapper for `BasicSGDClassifier`. Parameters ---------- X : 2d np.array The matrix of features, one example per row. y : list The list of labels for rows in `X`. Returns ------- BasicSGDClassifier A trained `BasicSGDClassifier` instance. """ mod = BasicSGDClassifier() mod.fit(X, y) return mod
def test_cross_validation_nlu(digits): X_train, X_test, y_train, y_test = digits param_grid = {'eta': [0.02, 0.03]} mod = BasicSGDClassifier(max_iter=2) best_mod = utils.fit_classifier_with_hyperparameter_search( X_train, y_train, mod, cv=2, param_grid=param_grid)
def test_cross_validation_sklearn(digits): X_train, X_test, y_train, y_test = digits mod = BasicSGDClassifier(max_iter=5) xval = cross_validate(mod, X_train, y_train, cv=2)
def test_hyperparameter_selection(digits): X_train, X_test, y_train, y_test = digits param_grid = {'eta': [0.02, 0.03]} mod = BasicSGDClassifier(max_iter=5) xval = RandomizedSearchCV(mod, param_grid, cv=2) xval.fit(X_train, y_train)
def test_parameter_setting(param, expected): mod = BasicSGDClassifier() mod.set_params(**{param: expected}) result = getattr(mod, param) assert result == expected
def test_params(param, expected): mod = BasicSGDClassifier(**{param: expected}) result = getattr(mod, param) assert result == expected