Exemplo n.º 1
0
def gridsearch_lgmlvq(X, y):
    params = {'prototypes_per_class': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
    model = LgmlvqModel()

    scaler = StandardScaler()
    X = scaler.fit_transform(X)

    grid = GridSearchCV(model, params, scoring=make_scorer(scoring_function), cv=3, n_jobs=-1)
    grid.fit(X, y)

    return grid.best_params_
Exemplo n.º 2
0
def test_lgmlvq_classwise():
    # Load data
    X, y = load_iris(True)

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.33,
                                                        random_state=4242)

    # Create and fit model
    model = LgmlvqModel(prototypes_per_class=3,
                        classwise=True,
                        max_iter=100,
                        random_state=4242)
    model.fit(X_train, y_train)

    # Select data point for explaining its prediction
    x_orig = X_test[1:4][0, :]
    assert model.predict([x_orig]) == 2

    # Compute counterfactual
    features_whitelist = None

    x_cf, y_cf, delta = generate_counterfactual(
        model,
        x_orig,
        0,
        features_whitelist=features_whitelist,
        regularization="l1",
        C=1.0,
        optimizer="bfgs",
        return_as_dict=False)
    assert y_cf == 0
    assert model.predict(np.array([x_cf])) == 0

    x_cf, y_cf, delta = generate_counterfactual(
        model,
        x_orig,
        0,
        features_whitelist=features_whitelist,
        regularization="l1",
        C=1.0,
        optimizer="nelder-mead",
        return_as_dict=False)
    assert y_cf == 0
    assert model.predict(np.array([x_cf])) == 0

    features_whitelist = [0, 1, 2, 3]

    x_cf, y_cf, delta = generate_counterfactual(
        model,
        x_orig,
        0,
        features_whitelist=features_whitelist,
        regularization="l1",
        C=1.0,
        optimizer="bfgs",
        return_as_dict=False)
    assert y_cf == 0
    assert model.predict(np.array([x_cf])) == 0
    assert all([
        True if i in features_whitelist else delta[i] == 0.
        for i in range(x_orig.shape[0])
    ])

    x_cf, y_cf, delta = generate_counterfactual(
        model,
        x_orig,
        0,
        features_whitelist=features_whitelist,
        regularization="l1",
        C=1.0,
        optimizer="nelder-mead",
        return_as_dict=False)
    assert y_cf == 0
    assert model.predict(np.array([x_cf])) == 0
    assert all([
        True if i in features_whitelist else delta[i] == 0.
        for i in range(x_orig.shape[0])
    ])
p3.set_title('GRLVQ')
plot(grlvq.project(x, 2),
     y, grlvq.predict(x), grlvq.project(grlvq.w_, 2),
     grlvq.c_w_, p3)

# GMLVQ
gmlvq = GmlvqModel()
gmlvq.fit(x, y)
p4 = plt.subplot(234)
p4.set_title('GMLVQ')
plot(gmlvq.project(x, 2),
     y, gmlvq.predict(x), gmlvq.project(gmlvq.w_, 2),
     gmlvq.c_w_, p4)

# LGMLVQ
lgmlvq = LgmlvqModel()
lgmlvq.fit(x, y)
p5 = plt.subplot(235)
elem_set = list(set(lgmlvq.c_w_))
p5.set_title('LGMLVQ 1')
plot(lgmlvq.project(x, 1, 2, True),
     y, lgmlvq.predict(x), lgmlvq.project(np.array([lgmlvq.w_[1]]), 1, 2),
     elem_set.index(lgmlvq.c_w_[1]), p5)
p6 = plt.subplot(236)
p6.set_title('LGMLVQ 2')
plot(lgmlvq.project(x, 6, 2, True),
     y, lgmlvq.predict(x), lgmlvq.project(np.array([lgmlvq.w_[6]]), 6, 2),
     elem_set.index(lgmlvq.c_w_[6]), p6)

plt.show()
shows the prototypes (black diamond) and their labels (small point inside the diamond).
The projected data is shown in the right plot.

"""
import matplotlib.pyplot as plt
import numpy as np

from sklearn_lvq import LgmlvqModel
from sklearn_lvq.utils import plot2d

print(__doc__)

nb_ppc = 100
toy_label = np.append(np.zeros(nb_ppc), np.ones(nb_ppc), axis=0)

print('LGMLVQ:')
toy_data = np.append(np.random.multivariate_normal([0, 1],
                                                   np.array([[5, -4], [-4,
                                                                       6]]),
                                                   size=nb_ppc),
                     np.random.multivariate_normal([0, 0],
                                                   np.array([[5, 4], [4, 6]]),
                                                   size=nb_ppc),
                     axis=0)
lgmlvq = LgmlvqModel()
lgmlvq.fit(toy_data, toy_label)
plot2d(lgmlvq, toy_data, toy_label, 1, 'lgmlvq')

print('classification accuracy:', lgmlvq.score(toy_data, toy_label))
plt.show()