Пример #1
0
def test_update_n_basis_modes_unfit_basis(basis, data_binary_classification):
    x, y, _ = data_binary_classification
    n_basis_modes = 5
    model = SSPOC(basis=basis())
    model.update_n_basis_modes(n_basis_modes, (x, y), quiet=True)

    assert model.basis_matrix_inverse_.shape[0] == n_basis_modes
Пример #2
0
def test_sspoc_selector_equivalence(data_multiclass_classification):
    x, y, _ = data_multiclass_classification

    model = SSPOC().fit(x, y)

    np.testing.assert_array_equal(model.get_selected_sensors(),
                                  model.selected_sensors)
Пример #3
0
def test_update_n_sensors(data, n_sensors):
    x, y, l1_penalty = data
    model = SSPOC(l1_penalty=l1_penalty).fit(x, y, quiet=True)

    model.update_sensors(n_sensors=n_sensors, quiet=True)
    assert len(model.selected_sensors) == n_sensors
    assert model.n_sensors == n_sensors
Пример #4
0
def test_large_threshold(data):
    x, y, l1_penalty = data
    model = SSPOC(l1_penalty=l1_penalty).fit(x, y, quiet=True)

    model.update_sensors(threshold=10, quiet=True)
    assert len(model.selected_sensors) == 0
    assert model.n_sensors == 0
Пример #5
0
def test_update_threshold(data):
    x, y, l1_penalty = data

    model = SSPOC(threshold=0.01, l1_penalty=l1_penalty).fit(x, y, quiet=True)
    nnz = len(model.selected_sensors)

    # Larger threshold should result in fewer sensors
    model.update_sensors(threshold=1, quiet=True)
    assert len(model.selected_sensors) < nnz
Пример #6
0
def test_prefit_basis(data_binary_classification):
    x, y, _ = data_binary_classification
    basis = Identity().fit(x)
    model_prefit = SSPOC(basis=basis)
    model_prefit.fit(x, y, prefit_basis=True, quiet=True)

    model_standard = SSPOC().fit(x, y, quiet=True)

    np.testing.assert_allclose(model_prefit.sensor_coef_,
                               model_standard.sensor_coef_)
Пример #7
0
def test_dummy_predict(data):
    x, y, l1_penalty = data
    model = SSPOC(l1_penalty=l1_penalty).fit(x, y, quiet=True)
    model.update_sensors(n_sensors=0, xy=(x, y), quiet=True)

    assert model.n_sensors == 0
    # Test that model can still make predictions, albeit random ones
    # when it has no sensors to work with
    y_pred = model.predict(x)
    assert len(y_pred) == len(y)
Пример #8
0
def test_update_n_basis_modes_errors(basis, data_binary_classification):
    x, y, _ = data_binary_classification
    n_basis_modes = 5
    model = SSPOC(basis=basis(n_basis_modes=n_basis_modes))

    model.fit(x, y, quiet=True)

    with pytest.raises(ValueError):
        model.update_n_basis_modes(0, xy=(x, y))
    with pytest.raises(ValueError):
        model.update_n_basis_modes("5", xy=(x, y))
    with pytest.raises(ValueError):
        model.update_n_basis_modes(x.shape[0] + 1, xy=(x, y))
Пример #9
0
def test_initialize_with_threshold(data):
    x, y, l1_penalty = data
    max_sensors = x.shape[1]
    model = SSPOC(threshold=0, l1_penalty=l1_penalty).fit(x, y, quiet=True)

    assert len(model.selected_sensors) == max_sensors
    assert model.n_sensors == max_sensors
Пример #10
0
def test_initialize_with_n_sensors(data):
    x, y, l1_penalty = data
    n_sensors = 3
    model = SSPOC(n_sensors=n_sensors, l1_penalty=l1_penalty).fit(x,
                                                                  y,
                                                                  quiet=True)

    assert len(model.selected_sensors) == n_sensors
    assert model.n_sensors == n_sensors
Пример #11
0
def test_not_fitted(data_binary_classification):
    x, y, _ = data_binary_classification
    model = SSPOC()

    # Shouldn't be able to call any of these methods before fitting
    with pytest.raises(NotFittedError):
        model.predict(x)
    with pytest.raises(NotFittedError):
        model.update_sensors(n_sensors=5)
    with pytest.raises(NotFittedError):
        model.selected_sensors
Пример #12
0
def test_update_n_basis_modes_shape(basis, data_binary_classification):
    x, y, _ = data_binary_classification
    n_basis_modes_init = 10
    model = SSPOC(basis=basis(n_basis_modes=n_basis_modes_init))
    model.fit(x, y, quiet=True)
    assert model.basis.n_basis_modes == n_basis_modes_init
    assert model.basis_matrix_inverse_.shape[0] == n_basis_modes_init

    n_basis_modes = 5
    model.update_n_basis_modes(n_basis_modes, xy=(x, y), quiet=True)
    assert model.basis.n_basis_modes == n_basis_modes_init
    assert model.basis_matrix_inverse_.shape[0] == n_basis_modes
Пример #13
0
X = np.random.normal(0, 1.0, size=(N, 100)).T

#f_gamma = np.array([-1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0]).reshape(-1,1)
f_gamma = np.zeros(shape=(N, 1))
#f_gamma = np.random.normal(0,2,size=(N,1))
f_gamma[0:10] = np.random.normal(0, 5, size=(10, 1))
f_gamma[np.abs(f_gamma) < 2] = 0
Gamma = lambda x: np.dot(x, f_gamma)

beta = Gamma(X).squeeze()
dz = beta > 0
#y = H(x)

X_tr, X_te, dz_tr, dz_te = train_test_split(X, dz)

model = SSPOC()
model.fit(X_tr, dz_tr)

plt.figure()
plt.plot(np.sort(np.abs(model.sensor_coef_)), 'o')
plt.title('Coefficient magnitudes')

model.update_sensors(n_sensors=2, xy=(X_tr, dz_tr))
print('Portion of sensors used:', len(model.selected_sensors) / 10)
print('Selected sensors:', model.selected_sensors)
plt.figure()
plt.plot(f_gamma)
plt.stem(model.selected_sensors, np.ones_like(model.selected_sensors))

accuracy = metrics.accuracy_score(
    dz_te, model.predict(X_te[:, model.selected_sensors]))
Пример #14
0
def test_bad_update_sensors_input(data_binary_classification):
    x, y, _ = data_binary_classification
    model = SSPOC().fit(x, y, quiet=True)

    with pytest.raises(ValueError):
        model.update_sensors()
Пример #15
0
def test_basis_integration(basis, data):
    x, y, _ = data
    model = SSPOC(basis=basis, n_sensors=5)
    model.fit(x, y, quiet=True)

    check_is_fitted(model)
Пример #16
0
def test_predict_accuracy(data, baseline_accuracy):
    x, y, l1_penalty = data
    model = SSPOC(threshold=0, l1_penalty=l1_penalty).fit(x, y, quiet=True)

    assert (accuracy_score(y, model.predict(x[:, model.selected_sensors])) >
            baseline_accuracy)
Пример #17
0
def test_coefficient_shape(data, shape):
    x, y, _ = data
    model = SSPOC().fit(x, y, quiet=True)

    assert model.sensor_coef_.shape == shape