Ejemplo n.º 1
0
def test_np_matrix():
    """
    Confirm that input validation code does not return np.matrix
    """
    X = np.arange(12).reshape(3, 4)

    assert_false(isinstance(as_float_array(X), np.matrix))
    assert_false(isinstance(as_float_array(np.matrix(X)), np.matrix))
    assert_false(isinstance(as_float_array(sp.csc_matrix(X)), np.matrix))

    assert_false(isinstance(atleast2d_or_csr(X), np.matrix))
    assert_false(isinstance(atleast2d_or_csr(np.matrix(X)), np.matrix))
    assert_false(isinstance(atleast2d_or_csr(sp.csc_matrix(X)), np.matrix))

    assert_false(isinstance(atleast2d_or_csc(X), np.matrix))
    assert_false(isinstance(atleast2d_or_csc(np.matrix(X)), np.matrix))
    assert_false(isinstance(atleast2d_or_csc(sp.csr_matrix(X)), np.matrix))

    assert_false(isinstance(safe_asarray(X), np.matrix))
    assert_false(isinstance(safe_asarray(np.matrix(X)), np.matrix))
    assert_false(isinstance(safe_asarray(sp.lil_matrix(X)), np.matrix))

    assert_true(atleast2d_or_csr(X, copy=False) is X)
    assert_false(atleast2d_or_csr(X, copy=True) is X)
    assert_true(atleast2d_or_csc(X, copy=False) is X)
    assert_false(atleast2d_or_csc(X, copy=True) is X)
Ejemplo n.º 2
0
def test_np_matrix():
    """
    Confirm that input validation code does not return np.matrix
    """
    X = np.arange(12).reshape(3, 4)

    assert_false(isinstance(as_float_array(X), np.matrix))
    assert_false(isinstance(as_float_array(np.matrix(X)), np.matrix))
    assert_false(isinstance(as_float_array(sp.csc_matrix(X)), np.matrix))

    assert_false(isinstance(atleast2d_or_csr(X), np.matrix))
    assert_false(isinstance(atleast2d_or_csr(np.matrix(X)), np.matrix))
    assert_false(isinstance(atleast2d_or_csr(sp.csc_matrix(X)), np.matrix))

    assert_false(isinstance(atleast2d_or_csc(X), np.matrix))
    assert_false(isinstance(atleast2d_or_csc(np.matrix(X)), np.matrix))
    assert_false(isinstance(atleast2d_or_csc(sp.csr_matrix(X)), np.matrix))

    assert_false(isinstance(safe_asarray(X), np.matrix))
    assert_false(isinstance(safe_asarray(np.matrix(X)), np.matrix))
    assert_false(isinstance(safe_asarray(sp.lil_matrix(X)), np.matrix))

    assert_true(atleast2d_or_csr(X, copy=False) is X)
    assert_false(atleast2d_or_csr(X, copy=True) is X)
    assert_true(atleast2d_or_csc(X, copy=False) is X)
    assert_false(atleast2d_or_csc(X, copy=True) is X)
Ejemplo n.º 3
0
def _transform_selected(X, transform, selected="all", copy=True):
    """Apply a transform function to portion of selected features

    Parameters
    ----------
    X : array-like or sparse matrix, shape=(n_samples, n_features)
        Dense array or sparse matrix.

    transform : callable
        A callable transform(X) -> X_transformed

    copy : boolean, optional
        Copy X even if it could be avoided.

    selected: "all" or array of indices or mask
        Specify which features to apply the transform to.

    Returns
    -------
    X : array or sparse matrix, shape=(n_samples, n_features_new)
    """
    if selected == "all":
        X = safe_asarray(X, copy=copy, force_all_finite=False)
        return transform(X)

    X = atleast2d_or_csc(X, copy=copy, force_all_finite=False)

    if len(selected) == 0:
        return X

    n_features = X.shape[1]
    ind = np.arange(n_features)
    sel = np.zeros(n_features, dtype=bool)
    sel[np.asarray(selected)] = True
    not_sel = np.logical_not(sel)
    n_selected = np.sum(sel)

    if n_selected == 0:
        # No features selected.
        return X
    elif n_selected == n_features:
        # All features selected.
        return transform(X)
    else:
        X_sel = transform(X[:, ind[sel]])
        X_not_sel = X[:, ind[not_sel]]

        if sparse.issparse(X_sel) or sparse.issparse(X_not_sel):
            return sparse.hstack((X_sel, X_not_sel)).tocsr()
        else:
            return np.hstack((X_sel, X_not_sel))
Ejemplo n.º 4
0
def test_atleast2d_or_sparse():
    for typ in [sp.csr_matrix, sp.dok_matrix, sp.lil_matrix, sp.coo_matrix]:
        X = typ(np.arange(9, dtype=float).reshape(3, 3))

        Y = atleast2d_or_csr(X, copy=True)
        assert_true(isinstance(Y, sp.csr_matrix))
        Y.data[:] = 1
        assert_array_equal(X.toarray().ravel(), np.arange(9))

        Y = atleast2d_or_csc(X, copy=False)
        Y.data[:] = 4
        assert_true(np.all(X.data == 4)
                    if isinstance(X, sp.csc_matrix)
                    else np.all(X.toarray().ravel() == np.arange(9)))

        Y = atleast2d_or_csr(X, dtype=np.float32)
        assert_true(Y.dtype == np.float32)
Ejemplo n.º 5
0
def test_atleast2d_or_sparse():
    for typ in [sp.csr_matrix, sp.dok_matrix, sp.lil_matrix, sp.coo_matrix]:
        X = typ(np.arange(9, dtype=float).reshape(3, 3))

        Y = atleast2d_or_csr(X, copy=True)
        assert_true(isinstance(Y, sp.csr_matrix))
        Y.data[:] = 1
        assert_array_equal(X.toarray().ravel(), np.arange(9))

        Y = atleast2d_or_csc(X, copy=False)
        Y.data[:] = 4
        assert_true(
            np.all(X.data == 4) if isinstance(X, sp.csc_matrix) else np.all(
                X.toarray().ravel() == np.arange(9)))

        Y = atleast2d_or_csr(X, dtype=np.float32)
        assert_true(Y.dtype == np.float32)
Ejemplo n.º 6
0
 def fit(self, X, y=None):
     X = atleast2d_or_csc(X)
     self.n_input_feats = X.shape[1]
     return self