Exemplo n.º 1
0
def test_gradient_descent():

    t1 = np.array([-5.21e-16,  -7.86e-02,   1.02e+00])
    ada = Adaline(epochs=30, eta=0.01, learning='gd', random_seed=1)
    ada.fit(X_std, y1)
    np.testing.assert_almost_equal(ada.w_, t1, 2)
    assert((y1 == ada.predict(X_std)).all())
Exemplo n.º 2
0
def test_0_1_class():

    t1 = np.array([0.51, -0.04,  0.51])
    ada = Adaline(epochs=30, eta=0.01, learning='sgd', random_seed=1)
    ada.fit(X_std, y0)
    np.testing.assert_almost_equal(ada.w_, t1, 2)
    assert((y0 == ada.predict(X_std)).all())
Exemplo n.º 3
0
def test_stochastic_gradient_descent():

    t1 = np.array([0.03, -0.09, 1.02])
    ada = Adaline(epochs=30, eta=0.01, learning='sgd', random_seed=1)
    ada.fit(X_std, y1)
    np.testing.assert_almost_equal(ada.w_, t1, 2)
    assert((y1 == ada.predict(X_std)).all())
Exemplo n.º 4
0
def test_print_progress_2():
    ada = Adaline(epochs=30,
                  eta=0.01,
                  minibatches=1,
                  print_progress=2,
                  random_seed=1)
    ada.fit(X_std, y1)
Exemplo n.º 5
0
def test_score_function():
    ada = Adaline(epochs=30,
                  eta=0.01,
                  minibatches=1,
                  random_seed=1)
    ada.fit(X_std, y1)
    acc = ada.score(X_std, y1)
    assert acc == 1.0, acc
Exemplo n.º 6
0
def test_ary_persistency_in_shuffling():
    orig = X_std.copy()
    ada = Adaline(epochs=30,
                  eta=0.01,
                  minibatches=len(y),
                  random_seed=1)
    ada.fit(X_std, y1)
    np.testing.assert_almost_equal(orig, X_std, 6)
Exemplo n.º 7
0
def test_invalid_class():

    ada = Adaline(epochs=40, eta=0.01, random_seed=1)
    try:
        ada.fit(X, y2)  # 0, 1 class
        assert(1==2)
    except ValueError:
        pass
Exemplo n.º 8
0
def test_score_function():
    t1 = np.array([-5.21e-16, -7.86e-02, 1.02e+00])
    ada = Adaline(epochs=30,
                  eta=0.01,
                  minibatches=1,
                  random_seed=1)
    ada.fit(X_std, y1)
    acc = ada.score(X_std, y1)
    assert acc == 1.0, acc
Exemplo n.º 9
0
def test_normal_equation():
    t1 = np.array([-5.21e-16,  -7.86e-02,   1.02e+00])
    ada = Adaline(epochs=30,
                  eta=0.01,
                  minibatches=None,
                  random_seed=1)
    ada.fit(X_std, y1)
    np.testing.assert_almost_equal(ada.w_, t1, 2)
    assert((y1 == ada.predict(X_std)).all())
Exemplo n.º 10
0
def test_stochastic_gradient_descent():
    t1 = np.array([[-0.08], [1.02]])
    ada = Adaline(epochs=30,
                  eta=0.01,
                  minibatches=len(y),
                  random_seed=1)
    ada.fit(X_std, y1)
    np.testing.assert_almost_equal(ada.w_, t1, 2)
    assert((y1 == ada.predict(X_std)).all())
Exemplo n.º 11
0
def test_standardized_iris_data_with_shuffle():
    t1 = np.array([-5.21e-16,  -7.86e-02,   1.02e+00])
    ada = Adaline(epochs=30,
                  eta=0.01,
                  solver='gd',
                  random_seed=1,
                  shuffle=True)
    ada.fit(X_std, y1)
    np.testing.assert_almost_equal(ada.w_, t1, 2)
    assert((y1 == ada.predict(X_std)).all())
Exemplo n.º 12
0
def test_standardized_iris_data_with_zero_weights():
    t1 = np.array([-5.21e-16,  -7.86e-02,   1.02e+00])
    ada = Adaline(epochs=30,
                  eta=0.01,
                  minibatches=1,
                  random_seed=1,
                  zero_init_weight=True)
    ada.fit(X_std, y1)
    np.testing.assert_almost_equal(ada.w_, t1, 2)
    assert((y1 == ada.predict(X_std)).all())
Exemplo n.º 13
0
def test_gradient_descent():
    t1 = np.array([[-0.08], [1.02]])
    b1 = np.array([0.00])
    ada = Adaline(epochs=30,
                  eta=0.01,
                  minibatches=1,
                  random_seed=1)
    ada.fit(X_std, y1)
    np.testing.assert_almost_equal(ada.w_, t1, decimal=2)
    np.testing.assert_almost_equal(ada.b_, b1, decimal=2)
    assert((y1 == ada.predict(X_std)).all())
Exemplo n.º 14
0
def test_normal_equation():
    t1 = np.array([[-0.08], [1.02]])
    b1 = np.array([0.00])
    ada = Adaline(epochs=30,
                  eta=0.01,
                  minibatches=None,
                  random_seed=None)
    ada.fit(X_std, y1)
    np.testing.assert_almost_equal(ada.w_, t1, decimal=2)
    np.testing.assert_almost_equal(ada.b_, b1, decimal=2)
    assert (y1 == ada.predict(X_std)).all(), ada.predict(X_std)
Exemplo n.º 15
0
def test_refit_weights():
    t1 = np.array([-5.21e-16,  -7.86e-02,   1.02e+00])
    ada = Adaline(epochs=15, eta=0.01, solver='gd', random_seed=1)
    ada.fit(X_std, y1, init_weights=True)
    ada.fit(X_std, y1, init_weights=False)
    np.testing.assert_almost_equal(ada.w_, t1, 2)
    assert((y1 == ada.predict(X_std)).all())
Exemplo n.º 16
0
def test_refit_weights():
    t1 = np.array([[-0.08], [1.02]])
    ada = Adaline(epochs=15,
                  eta=0.01,
                  minibatches=1,
                  random_seed=1)
    ada.fit(X_std, y1, init_params=True)
    ada.fit(X_std, y1, init_params=False)
    np.testing.assert_almost_equal(ada.w_, t1, 2)
    assert((y1 == ada.predict(X_std)).all())
Exemplo n.º 17
0
def test_ary_persistency_in_shuffling():
    orig = X_std.copy()
    ada = Adaline(epochs=30, eta=0.01, minibatches=len(y), random_seed=1)
    ada.fit(X_std, y1)
    np.testing.assert_almost_equal(orig, X_std, 6)
Exemplo n.º 18
0
from mlxtend.classifier import Perceptron
from mlxtend.classifier import Adaline
from mlxtend.classifier import MultiLayerPerceptron
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_moons
import matplotlib.gridspec as gridspec
import itertools

gs = gridspec.GridSpec(2, 2)  #xw
X, y = make_moons(n_samples=100, random_state=123)
fig = plt.figure(figsize=(10, 8))

ppn = Perceptron(epochs=50, eta=0.05, random_seed=0)
ppn.fit(X, y)
ada = Adaline(epochs=50, eta=0.05, random_seed=0)
ada.fit(X, y)

mlp = MultiLayerPerceptron(n_output=len(np.unique(y)),
                           n_features=X.shape[1],
                           n_hidden=150,
                           l2=0.0,
                           l1=0.0,
                           epochs=500,
                           eta=0.01,
                           alpha=0.0,
                           decrease_const=0.0,
                           minibatches=1,
                           shuffle_init=False,
                           shuffle_epoch=False,
                           random_seed=0)
Exemplo n.º 19
0
def test_invalid_class():
    ada = Adaline(epochs=40, eta=0.01, random_seed=1)
    try:
        ada.fit(X, y2)  # 0, 1 class
    except ValueError:
        pass
Exemplo n.º 20
0
def test_score_function():
    ada = Adaline(epochs=30, eta=0.01, minibatches=1, random_seed=1)
    ada.fit(X_std, y1)
    acc = ada.score(X_std, y1)
    assert acc == 1.0, acc
Exemplo n.º 21
0
def test_gradient_descent():
    t1 = np.array([-5.21e-16, -7.86e-02, 1.02e+00])
    ada = Adaline(epochs=30, eta=0.01, minibatches=1, random_seed=1)
    ada.fit(X_std, y1)
    np.testing.assert_almost_equal(ada.w_, t1, 2)
    assert ((y1 == ada.predict(X_std)).all())
Exemplo n.º 22
0
def test_0_1_class():
    t1 = np.array([0.51, -0.04, 0.51])
    ada = Adaline(epochs=30, eta=0.01, minibatches=1, random_seed=1)
    ada.fit(X_std, y0)
    np.testing.assert_almost_equal(ada.w_, t1, 2)
    assert ((y0 == ada.predict(X_std)).all())
Exemplo n.º 23
0
def test_array_dimensions():
    ada = Adaline(epochs=15, eta=0.01, random_seed=1)
    ada = ada.fit(np.array([1, 2, 3]), [-1])
Exemplo n.º 24
0
def test_array_dimensions():
    ada = Adaline(epochs=15, eta=0.01, random_seed=1)
    ada = ada.fit(np.array([1, 2, 3]), [-1])
Exemplo n.º 25
0
def test_invalid_solver():
    t1 = np.array([-5.21e-16,  -7.86e-02,   1.02e+00])
    ada = Adaline(epochs=30, eta=0.01, solver='bla', random_seed=1)
    ada.fit(X_std, y1)
Exemplo n.º 26
0
def test_stochastic_gradient_descent():
    t1 = np.array([[-0.08], [1.02]])
    ada = Adaline(epochs=30, eta=0.01, minibatches=len(y), random_seed=1)
    ada.fit(X_std, y1)
    np.testing.assert_almost_equal(ada.w_, t1, 2)
    assert ((y1 == ada.predict(X_std)).all())
Exemplo n.º 27
0
from mlxtend.evaluate import plot_decision_regions
from mlxtend.classifier import Adaline
import matplotlib.pyplot as plt

X, y = iris_data()
X = X[:, [0, 3]]
X = X[0:100]
y = y[0:100]

X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()
X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()


# Closed Form Solution
ada = Adaline(epochs=30,
              eta=0.01,
              minibatches=None,
              random_seed=1)

ada.fit(X, y)
plot_decision_regions(X, y, clf=ada)
plt.title('Adaline - Stochastic Gradient Descent')
plt.show()



# (Stochastic) Gradient Descent
ada2 = Adaline(epochs=30,
               eta=0.01,
               minibatches=1, # 1 for GD learning
               #minibatches=len(y), # len(y) for SGD learning
               #minibatches=5, # for SGD learning w. minibatch size 20
Exemplo n.º 28
0
def test_invalid_labels_2():
    y2 = np.where(y1 == 0, -1, 1)
    ada = Adaline(epochs=15, eta=0.01, random_seed=1)
    assert_raises(AttributeError,
                  'y array must not contain negative labels.\nFound [-1  1]',
                  ada.fit, X, y2, {(-1, 1)})