예제 #1
0
    def test_update_label(self):
        gwr = oss_gwr(eps_b=1, eps_n=1, max_age=15)
        gwr.G = nx.Graph()
        gwr.G.add_node(0,
                       attr_dict={
                           'pos': np.array([0, 0]),
                           'fir': 0.1,
                           'lab': 3
                       })
        gwr.G.add_node(1,
                       attr_dict={
                           'pos': np.array([1, 0]),
                           'fir': 0.1,
                           'lab': 2
                       })

        gwr._update_label(0, -1, 1)
        np.testing.assert_equal(gwr.G.node[0]['lab'], 3)

        gwr._update_label(0, 1, 1)
        np.testing.assert_equal(gwr.G.node[0]['lab'], 1)

        gwr._update_label(0, -1, 1)
        np.testing.assert_equal(gwr.G.node[0]['lab'], 1)

        gwr.G.add_edge(0, 1)
        gwr._update_label(0, -1, 1)
        np.testing.assert_equal(gwr.G.node[0]['lab'], 2)
예제 #2
0
    def test_get_best_matching(self):
        gwr = oss_gwr()
        gwr.G = nx.Graph()
        gwr.G.add_node(0, attr_dict={'pos': np.array([1, 1])})
        gwr.G.add_node(3, attr_dict={'pos': np.array([2, 1])})

        b, s = gwr._get_best_matching(np.array([[3, 1]]))
        np.testing.assert_equal(b, 3)
        np.testing.assert_equal(s, 0)

        b, s = gwr._get_best_matching(np.array([[1, 2]]))
        np.testing.assert_equal(b, 0)
        np.testing.assert_equal(s, 3)
예제 #3
0
    def test_remove_old_edges(self):
        gwr = oss_gwr(eps_b=1, eps_n=1, max_age=15)
        gwr.G = nx.Graph()
        gwr.G.add_node(0,
                       attr_dict={
                           'pos': np.array([0, 0]),
                           'fir': 1,
                           'lab': -1
                       })
        gwr.G.add_node(1,
                       attr_dict={
                           'pos': np.array([1, 0]),
                           'fir': 1,
                           'lab': -1
                       })
        gwr.G.add_node(2,
                       attr_dict={
                           'pos': np.array([1, 0]),
                           'fir': 1,
                           'lab': -1
                       })
        gwr.G.add_node(3,
                       attr_dict={
                           'pos': np.array([1, 0]),
                           'fir': 1,
                           'lab': -1
                       })
        gwr.G.add_edge(0, 1, attr_dict={'age': 20})
        gwr.G.add_edge(0, 3, attr_dict={'age': 1})
        gwr.G.add_edge(1, 3, attr_dict={'age': 1})
        gwr.G.add_edge(2, 3, attr_dict={'age': 20})

        gwr._remove_old_edges()
        nodes = gwr.G.nodes()
        edges = gwr.G.edges()
        np.testing.assert_array_equal(nodes, [0, 1, 3])
        np.testing.assert_array_equal(edges, [(0, 3), (1, 3)])

        gwr.G.edge[1][3]['age'] = 30

        gwr._remove_old_edges()
        nodes = gwr.G.nodes()
        edges = gwr.G.edges()
        np.testing.assert_array_equal(nodes, [0, 3])
        np.testing.assert_array_equal(edges, [(0, 3)])
예제 #4
0
 def test_update_network(self):
     gwr = oss_gwr(eps_b=1, eps_n=1)
     gwr.G = nx.Graph()
     gwr.G.add_node(0,
                    attr_dict={
                        'pos': np.array([0, 0]),
                        'fir': 1,
                        'lab': -1
                    })
     gwr.G.add_node(1,
                    attr_dict={
                        'pos': np.array([1, 0]),
                        'fir': 1,
                        'lab': -1
                    })
     gwr._make_link(0, 1)
     gwr._update_network(np.array([[0, 1]]), 0)
     pos_b = gwr.G.node[0]['pos']
     pos_n = gwr.G.node[1]['pos']
     np.testing.assert_array_equal(pos_b, np.array([0, 1]))
     np.testing.assert_array_equal(pos_n, np.array([0, 1]))
예제 #5
0
 def test_add_node(self):
     gwr = oss_gwr(fir_thr=1.1, act_thr=1.1)
     gwr.G = nx.Graph()
     gwr.G.add_node(0,
                    attr_dict={
                        'pos': np.array([0, 0]),
                        'fir': 1,
                        'lab': -1
                    })
     gwr.G.add_node(3,
                    attr_dict={
                        'pos': np.array([1, 0]),
                        'fir': 1,
                        'lab': -1
                    })
     gwr._make_link(0, 3)
     gwr._training_step(np.array([[4, 0]]))
     np.testing.assert_equal(gwr.G.nodes()[-1], 4)
     pos = gwr.G.node[4]['pos']
     np.testing.assert_array_equal(pos, np.array([2.5, 0]))
     np.testing.assert_array_equal(gwr.G.edges(), [(0, 4), (3, 4)])
예제 #6
0
    def test_label_propagation_coeff(self):
        gwr = oss_gwr()
        gwr.G = nx.Graph()
        gwr.G.add_node(0,
                       attr_dict={
                           'pos': np.array([0, 0]),
                           'fir': 1,
                           'lab': -1
                       })
        gwr.G.add_node(1,
                       attr_dict={
                           'pos': np.array([1, 0]),
                           'fir': 1,
                           'lab': -1
                       })
        gwr._make_link(0, 1)
        coeff = gwr._get_label_propagation_coeff(0, 1)
        np.testing.assert_equal(coeff, 1 / 3)

        gwr.G.remove_edge(0, 1)
        coeff = gwr._get_label_propagation_coeff(0, 1)
        np.testing.assert_equal(coeff, 0)
kf = sklearn.model_selection.KFold(n_splits=n_splits, shuffle=False)
for train_index, test_index in kf.split(pos):
    pos_train, pos_test = pos[train_index], pos[test_index]
    vel_train, vel_test = vel[train_index], vel[test_index]

    y_train, y_test = y[train_index], y[test_index]
    y_train = sequence_labels(y_train, w=7)
    y_test = sequence_labels(y_test, w=7)

    g1, g2, g3 = train_unsupervised_hierarchy(pos_train,
                                              vel_train,
                                              n_epochs=n_epochs)
    X_train = propagate_through_hierarchy(pos_train, vel_train, g1, g2, g3)
    X_test = propagate_through_hierarchy(pos_test, vel_test, g1, g2, g3)

    gwr_super = oss_gwr(**pars)
    gwr_super.train(X_train, y_train, n_epochs=n_epochs)

    y_pred = gwr_super.predict(X_test)
    a = sklearn.metrics.accuracy_score(y_test, y_pred)
    acc.append(a)

    if compare:
        rf = RandomForestDecoder()
        rf.fit(X_train, y_train)
        y_pred = rf.predict(X_test)
        a = sklearn.metrics.accuracy_score(y_test, y_pred)
        accrf.append(a)

f, ax = plt.subplots(1, 1)
ax.plot(acc, label='GWR')
예제 #8
0
import matplotlib.pyplot as plt
'''
Using the OSS-GWR to classify the Iris dataset.
'''

iris = sklearn.datasets.load_iris()
X = iris.data
y = iris.target

# split data into training and testing
ind = np.random.choice(X.shape[0], size=100, replace=False)
mask = np.zeros_like(y, dtype=bool)
mask[ind] = True
X_train = X[mask, :]
y_train = y[mask]

X_test = X[~mask, :]
y_test = y[~mask]

logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
oss = oss_gwr(act_thr=0.45)
oss.train(X_train, y_train, n_epochs=20)
y_pred = oss.predict(X_test)

cm = sklearn.metrics.confusion_matrix(y_test, y_pred)
acc = cm.diagonal().sum() / cm.sum()
print('Classification accuracy: %s' % str(acc))

f, ax = plt.subplots(1, 1)
sns.heatmap(cm, ax=ax, annot=True)
plt.show()
예제 #9
0
for i in range(5, y.shape[0]):
    c = collections.Counter(y[i - 5:i])
    lab = c.most_common()[0][0]
    y_[i - 5] = lab

acc = []
accrf = []
compare = True
n_splits = 10
kf = sklearn.model_selection.KFold(n_splits=n_splits, shuffle=True)
for train_index, test_index in kf.split(X_):
    X_train, X_test = X_[train_index], X_[test_index]
    y_train, y_test = y_[train_index], y_[test_index]
    # here you can kill the labels of y_train

    gwr = oss_gwr(**pars)
    gwr.train(X_train, y_train, n_epochs=50)
    y_pred = gwr.predict(X_test)
    a = sklearn.metrics.accuracy_score(y_test, y_pred)
    acc.append(a)

    if compare:
        rf = RandomForestDecoder()
        rf.fit(X_train, y_train)
        y_pred = rf.predict(X_test)
        a = sklearn.metrics.accuracy_score(y_test, y_pred)
        accrf.append(a)

f, ax = plt.subplots(1, 1)
ax.plot(acc, label='GWR')
ax.plot(accrf, label='RF')
import numpy as np
import logging
import sklearn.datasets
import sklearn.metrics
import sklearn.model_selection

from neuralgas.oss_gwr import oss_gwr

iris = sklearn.datasets.load_iris()
X = iris.data
y = iris.target

kf = sklearn.model_selection.KFold(n_splits=10)

acc = []
for train_index, test_index in kf.split(X):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]
    # here you can kill the labels of y_train

    gwr = oss_gwr()
    gwr.train(X_train, y_train)
    y_pred = gwr.predict(X_test)
    a = sklearn.metrics.accuracy_score(y_test, y_pred)
    acc.append(a)

print('Mean accuracy: %s' % np.mean(acc))
예제 #11
0
        X_train = XX[mask, :]
        y_train = yy[mask]
        X_test = XX[~mask, :]
        y_test = yy[~mask]

        # obfuscate the data
        i = ran[k]
        s = i * X_train.shape[0] // 100
        ind2 = np.random.choice(X_train.shape[0], size=s, replace=False)
        mask = np.zeros_like(y_train, dtype=bool)
        mask[ind2] = True
        y_train[~mask] = -1
        y_train.setflags(write=False)

        # classify
        gwr = oss_gwr(act_thr=0.75, max_age=500, kappa=1.05)
        gwr.train(X_train, y_train, n_epochs=n_epochs)
        y_pred = gwr.predict(X_test)
        a = sklearn.metrics.accuracy_score(y_test, y_pred)
        acc[j, k] = a

        Gcc = sorted(nx.connected_component_subgraphs(gwr.G),
                     key=len,
                     reverse=True)
        G0 = len(Gcc[0].nodes())
        C = nx.average_clustering(gwr.G)
        graph[0, j, k] = G0
        graph[1, j, k] = C

Acc = acc.mean(axis=0)
std = acc.std(axis=0)