def test_update_n_basis_modes_refit(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) assert model.basis_matrix_inverse_.shape[0] == n_basis_modes model.update_n_basis_modes(n_basis_modes + 1, (x, y), quiet=True) assert model.basis_matrix_inverse_.shape[0] == n_basis_modes + 1
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_)
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
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))
#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])) print(accuracy)
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)