Beispiel #1
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
Beispiel #2
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)
Beispiel #3
0
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 plot_sensor_locations(sensors, ax=None):
    img = np.zeros(64)
    img[sensors] = 16

    if ax is None:
        plt.imshow(img.reshape(8, 8), cmap=plt.cm.binary)
        plt.xticks([])
        plt.yticks([])
        plt.title('Learned sensor locations')
    else:
        ax.imshow(img.reshape(8, 8), cmap=plt.cm.binary)
Beispiel #4
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)