コード例 #1
0
ファイル: test_decoder.py プロジェクト: zhiye9/nilearn
def test_decoder_split_cv():
    X, y = make_classification(n_samples=200,
                               n_features=125,
                               scale=3.0,
                               n_informative=5,
                               n_classes=4,
                               random_state=42)
    X, mask = to_niimgs(X, [5, 5, 5])
    rand_local = np.random.RandomState(42)
    groups = rand_local.binomial(2, 0.3, size=len(y))

    # Check whether ValueError is raised when cv is not set correctly
    for cv in ['abc', LinearSVC()]:
        model = Decoder(mask=NiftiMasker(), cv=cv)
        pytest.raises(ValueError, model.fit, X, y)

    # Check whether decoder raised warning when groups is set to specific
    # value but CV Splitter is not set
    expected_warning = ('groups parameter is specified but '
                        'cv parameter is not set to custom CV splitter. '
                        'Using default object LeaveOneGroupOut().')
    with pytest.warns(UserWarning, match=expected_warning):
        model = Decoder(mask=NiftiMasker())
        model.fit(X, y, groups=groups)

    # Check that warning is raised when n_features is lower than 50 after
    # screening and clustering for fREM
    with pytest.warns(UserWarning, match=".*screening_percentile parameters"):
        model = fREMClassifier(clustering_percentile=10,
                               screening_percentile=10,
                               mask=NiftiMasker(),
                               cv=1)
        model.fit(X, y)
コード例 #2
0
ファイル: test_decoder.py プロジェクト: nicofarr/nilearn
def test_decoder_binary_classification():
    X, y = make_classification(n_samples=200,
                               n_features=125,
                               scale=3.0,
                               n_informative=5,
                               n_classes=2,
                               random_state=42)
    X, mask = to_niimgs(X, [5, 5, 5])

    # check classification with masker object
    model = Decoder(mask=NiftiMasker())
    model.fit(X, y)
    y_pred = model.predict(X)
    assert accuracy_score(y, y_pred) > 0.95

    # decoder object use predict_proba for scoring with logistic model
    model = Decoder(estimator='logistic_l2', mask=mask)
    model.fit(X, y)
    y_pred = model.predict(X)
    assert accuracy_score(y, y_pred) > 0.95

    # check different screening_percentile value
    for screening_percentile in [100, 20]:
        model = Decoder(mask=mask, screening_percentile=screening_percentile)
        model.fit(X, y)
        y_pred = model.predict(X)
        assert accuracy_score(y, y_pred) > 0.95

    screening_percentile = 90
    for clustering_percentile in [100, 99]:
        model = fREMClassifier(estimator='logistic_l2',
                               mask=mask,
                               clustering_percentile=clustering_percentile,
                               screening_percentile=screening_percentile,
                               cv=5)
        model.fit(X, y)
        y_pred = model.predict(X)
        assert accuracy_score(y, y_pred) > 0.9

    # check cross-validation scheme and fit attribute with groups enabled
    rand_local = np.random.RandomState(42)
    for cv in [KFold(n_splits=5), LeaveOneGroupOut()]:
        model = Decoder(estimator='svc', mask=mask, standardize=True, cv=cv)
        if isinstance(cv, LeaveOneGroupOut):
            groups = rand_local.binomial(2, 0.3, size=len(y))
        else:
            groups = None
        model.fit(X, y, groups=groups)
        assert accuracy_score(y, y_pred) > 0.9