Exemplo n.º 1
0
def test_is_pairwise():
    # simple checks for _is_pairwise
    pca = KernelPCA(kernel='precomputed')
    with pytest.warns(None) as record:
        assert _is_pairwise(pca)
    assert not record

    # pairwise attribute that is not consistent with the pairwise tag
    class IncorrectTagPCA(KernelPCA):
        _pairwise = False

    pca = IncorrectTagPCA(kernel='precomputed')
    msg = "_pairwise was deprecated in 0.24 and will be removed in 1.1"
    with pytest.warns(FutureWarning, match=msg):
        assert not _is_pairwise(pca)

    # the _pairwise attribute is present and set to True while pairwise tag is
    # not present
    class TruePairwise(BaseEstimator):
        _pairwise = True

    true_pairwise = TruePairwise()
    with pytest.warns(FutureWarning, match=msg):
        assert _is_pairwise(true_pairwise)

    # pairwise attribute is not defined thus tag is used
    est = BaseEstimator()
    with pytest.warns(None) as record:
        assert not _is_pairwise(est)
    assert not record
Exemplo n.º 2
0
def test_is_pairwise():
    # simple checks for _is_pairwise
    pca = KernelPCA(kernel='precomputed')
    with pytest.warns(None) as record:
        assert _is_pairwise(pca)
    assert not record

    # pairwise attribute that is not consistent with the pairwise tag
    class IncorrectTagPCA(KernelPCA):
        _pairwise = False

    pca = IncorrectTagPCA(kernel='precomputed')
    msg = ("_pairwise was deprecated in 0.24 and will be removed in 0.26. "
           "Set the estimator tags of your estimator instead")
    with pytest.warns(FutureWarning, match=msg):
        assert not _is_pairwise(pca)

    # the _pairwise attribute is present and set to False while the pairwise
    # tag is not present
    class FalsePairwise(BaseEstimator):
        _pairwise = False

        def _get_tags(self):
            tags = super()._get_tags()
            del tags['pairwise']
            return tags

    false_pairwise = FalsePairwise()
    with pytest.warns(None) as record:
        assert not _is_pairwise(false_pairwise)
    assert not record

    # the _pairwise attribute is present and set to True while pairwise tag is
    # not present
    class TruePairwise(FalsePairwise):
        _pairwise = True

    true_pairwise = TruePairwise()
    with pytest.warns(FutureWarning, match=msg):
        assert _is_pairwise(true_pairwise)

    # pairwise attribute is not defined thus tag is used
    est = BaseEstimator()
    with pytest.warns(None) as record:
        assert not _is_pairwise(est)
    assert not record
Exemplo n.º 3
0
def _safe_split(estimator, X, y, indices, train_indices=None):
    """Create subset of dataset and properly handle kernels.

    Slice X, y according to indices for cross-validation, but take care of
    precomputed kernel-matrices or pairwise affinities / distances.

    If ``estimator._pairwise is True``, X needs to be square and
    we slice rows and columns. If ``train_indices`` is not None,
    we slice rows using ``indices`` (assumed the test set) and columns
    using ``train_indices``, indicating the training set.

    .. deprecated:: 0.24

        The _pairwise attribute is deprecated in 0.24. From 1.1
        (renaming of 0.26) and onward, this function will check for the
        pairwise estimator tag.

    Labels y will always be indexed only along the first axis.

    Parameters
    ----------
    estimator : object
        Estimator to determine whether we should slice only rows or rows and
        columns.

    X : array-like, sparse matrix or iterable
        Data to be indexed. If ``estimator._pairwise is True``,
        this needs to be a square array-like or sparse matrix.

    y : array-like, sparse matrix or iterable
        Targets to be indexed.

    indices : array of int
        Rows to select from X and y.
        If ``estimator._pairwise is True`` and ``train_indices is None``
        then ``indices`` will also be used to slice columns.

    train_indices : array of int or None, default=None
        If ``estimator._pairwise is True`` and ``train_indices is not None``,
        then ``train_indices`` will be use to slice the columns of X.

    Returns
    -------
    X_subset : array-like, sparse matrix or list
        Indexed data.

    y_subset : array-like, sparse matrix or list
        Indexed targets.

    """
    if _is_pairwise(estimator):
        if not hasattr(X, "shape"):
            raise ValueError("Precomputed kernels or affinity matrices have "
                             "to be passed as arrays or sparse matrices.")
        # X is a precomputed square kernel matrix
        if X.shape[0] != X.shape[1]:
            raise ValueError("X should be a square kernel matrix")
        if train_indices is None:
            X_subset = X[np.ix_(indices, indices)]
        else:
            X_subset = X[np.ix_(indices, train_indices)]
    else:
        X_subset = _safe_indexing(X, indices)

    if y is not None:
        y_subset = _safe_indexing(y, indices)
    else:
        y_subset = None

    return X_subset, y_subset