Example #1
0
def test_build_param_grid_set_estimator():
    clf1 = SVC()
    clf2 = LogisticRegression()
    clf3 = SVC()
    clf4 = SGDClassifier()
    estimator = set_grid(Pipeline([('sel', set_grid(SelectKBest(), k=[2, 3])),
                                   ('clf', None)]),
                         clf=[
                             set_grid(clf1, kernel=['linear']), clf2,
                             set_grid(clf3, kernel=['poly'], degree=[2, 3]),
                             clf4
                         ])
    param_grid = [{
        'clf': [clf1],
        'clf__kernel': ['linear'],
        'sel__k': [2, 3]
    }, {
        'clf': [clf3],
        'clf__kernel': ['poly'],
        'clf__degree': [2, 3],
        'sel__k': [2, 3]
    }, {
        'clf': [clf2, clf4],
        'sel__k': [2, 3]
    }]
    assert build_param_grid(estimator) == param_grid
Example #2
0
def test_rnn(datafnc):
    from sklearn.neural_network import MLPClassifier
    from sklearn.model_selection import train_test_split
    from sklearn.model_selection import GridSearchCV
    from sklearn.pipeline import make_pipeline
    from sklearn.preprocessing import StandardScaler
    from searchgrid import set_grid, build_param_grid
    from noxer.sequences import PadSubsequence

    X, y = datafnc()

    X_train, X_test, y_train, y_test = train_test_split(X, y)

    estimator = make_pipeline(
        set_grid(GRUClassifier(n_neurons=64, n_layers=1, epochs=100),
                 alpha=[1e-4, 1e-3, 1e-2]))

    estimator = make_pipeline(
        set_grid(CNN1DClassifier(epochs=64),
                 alpha=[0.01],
                 n_layers=[1],
                 n_neurons=[32],
                 dropout=[0.2, 0.3, 0.4]))

    model = GridSearchCV(estimator=estimator,
                         param_grid=build_param_grid(estimator),
                         verbose=1000,
                         cv=3,
                         n_jobs=1)

    model.fit(X_train, y_train)
    print(datafnc.__name__)
    print(model.score(X_test, y_test))
Example #3
0
def test_dnn_v_dnn(datafnc):
    from sklearn.neural_network import MLPClassifier
    from sklearn.model_selection import train_test_split
    from sklearn.model_selection import GridSearchCV
    from sklearn.pipeline import make_pipeline
    from sklearn.preprocessing import StandardScaler
    from searchgrid import set_grid, build_param_grid

    X, y = datafnc()

    X_train, X_test, y_train, y_test = train_test_split(X, y)

    estimator = make_pipeline(
        StandardScaler(),
        set_grid(PMLPClassifier(),
                 epochs=[2**i for i in range(1, 8)],
                 n_layers=list(range(1, 4)),
                 n_neurons=[2**i for i in range(1, 8)],
                 alpha=[1e-4, 1e-3, 1e-2]))

    model = GridSearchCV(estimator=estimator,
                         param_grid=build_param_grid(estimator),
                         verbose=1000,
                         cv=3,
                         n_jobs=2)

    mlp = GridSearchCV(
        estimator=make_pipeline(
            StandardScaler(),
            MLPClassifier(),
        ),
        param_grid={'mlpclassifier__max_iter': [2**i for i in range(1, 8)]},
        verbose=1000,
        cv=3)

    model.fit(X_train, y_train)
    mlp.fit(X_train, y_train)
    print(datafnc.__name__)
    print(model.score(X_test, y_test))
    print(mlp.score(X_test, y_test))
Example #4
0
models = [
    set_grid(DecisionTreeClassifier(),
             min_samples_split=[2**-i for i in range(1, 8)],
             max_depth=list(range(1, 21))),
    set_grid(LinearSVC(), C=np.logspace(-6, 6, num=20)),
]

estimator = set_grid(estimator,
                     model=[
                         set_grid(GradientBoostingClassifier(),
                                  n_estimators=[2**i for i in range(1, 11)],
                                  learning_rate=np.logspace(-4, 0, num=10)),
                     ],
                     scaler=[StandardScaler()])

param_grid = build_param_grid(estimator)

gsearch = GridSearchCV(estimator=estimator,
                       param_grid=param_grid,
                       verbose=1,
                       cv=3,
                       n_jobs=-1)

model = gsearch
dummy = DummyClassifier(strategy='most_frequent')

model.fit(X_train, y_train)
dummy.fit(np.zeros((len(y_train), 1)), y_train)
print(model.best_params_)
print(model.score(X_test, y_test))
print(dummy.score(np.zeros((len(y_test), 1)), y_test))
Example #5
0
def test_build_param_grid(estimator, param_grid):
    assert build_param_grid(estimator) == param_grid