コード例 #1
0
def test_regression():
    """NOTE: subsequent calls may flip the direction of eigenvectors
        (mulitply by -1), so we can only compare absolute values.

        This was not a problem for svds.. investigate if we can get
        deterministic behavior back.
    """
    X, y = datasets.make_cubic(random_state=123)

    for n_dir in range(1, X.shape[1]):
        sir = SlicedInverseRegression(n_directions=n_dir)

        # take shape is correct
        X_sir = sir.fit(X, y).transform(X)
        np.testing.assert_equal(X_sir.shape[1], n_dir)

        # should match fit_transform
        X_sir2 = sir.fit_transform(X, y)
        np.testing.assert_allclose(np.abs(X_sir), np.abs(X_sir2))

        # call transform again and check if things are okay
        X_sir = sir.transform(X)
        X_sir2 = sir.fit_transform(X, y)
        np.testing.assert_allclose(np.abs(X_sir), np.abs(X_sir2))

        # there is one true angle it should fine
        true_beta = (1 / np.sqrt(2)) * np.hstack((np.ones(2), np.zeros(8)))
        angle = np.dot(true_beta, sir.directions_[0, :])
        np.testing.assert_allclose(np.abs(angle), 1, rtol=1e-1)
コード例 #2
0
def test_errors_alpha_out_of_bounds():
    X, y = datasets.make_cubic(random_state=123)

    sir = SlicedInverseRegression(alpha=10)

    with pytest.raises(ValueError):
        sir.fit(X, y)
コード例 #3
0
ファイル: test_save.py プロジェクト: vishalbelsare/sliced
def test_cubic():
    X, y = datasets.make_cubic(random_state=123)

    save = SlicedAverageVarianceEstimation().fit(X, y)

    true_beta = (1 / np.sqrt(2)) * np.hstack((np.ones(2), np.zeros(8)))
    angle = np.dot(true_beta, save.directions_[0, :])
    np.testing.assert_allclose(np.abs(angle), 1, rtol=1e-1)
コード例 #4
0
ファイル: test_save.py プロジェクト: vishalbelsare/sliced
def test_sparse_not_supported():
    X, y = datasets.make_cubic(random_state=123)
    X = sparse.csr_matrix(X)

    with pytest.raises(TypeError):
        SlicedAverageVarianceEstimation().fit(X, y)
コード例 #5
0
ファイル: test_save.py プロジェクト: vishalbelsare/sliced
def test_n_directions_none():
    X, y = datasets.make_cubic(random_state=123)
    sir = SlicedAverageVarianceEstimation(n_directions=None).fit(X, y)
    np.testing.assert_equal(sir.n_directions_, X.shape[1])
コード例 #6
0
"""
=========================
Sliced Inverse Regression
=========================

An example plot of :class:`sliced.sir.SlicedInverseRegression`
"""
import numpy as np
import matplotlib.pyplot as plt

from sliced import SlicedInverseRegression
from sliced import datasets

X, y = datasets.make_cubic(random_state=123)

sir = SlicedInverseRegression()
X_sir = sir.fit_transform(X, y)

# estimate of the first dimension reducing directions
beta1_hat = sir.directions_[0, :]

# plot data projected onto the first direction
plt.scatter(X_sir[:, 0], y, c=y, cmap='viridis', linewidth=0.5, edgecolor='k')
plt.xlabel("$X\hat{\\beta_1}$")
plt.ylabel("y")

# annotation showing the direction found
beta_text = "$\\beta_1$ = " + "{0}".format([0.707, 0.707])
plt.annotate(beta_text, xy=(-2, 6.5))
beta1_hat_text = "$\hat{\\beta_1}$ = " + "{0}".format(
    np.round(beta1_hat, 3).tolist()[:2])
コード例 #7
0
def test_sparse_not_supported():
    X, y = datasets.make_cubic(random_state=123)
    X = sparse.csr_matrix(X)
    with pytest.raises(TypeError):
        SlicedInverseRegression().fit(X, y)
コード例 #8
0
def test_n_directions_none():
    X, y = datasets.make_cubic(random_state=123)
    sir = SlicedInverseRegression(n_directions=None).fit(X, y)
    np.testing.assert_equal(sir.n_directions_, X.shape[1])