Beispiel #1
0
    def test_accuracy_stream(self):

        stream = SEAGenerator(random_state=42)
        stream.prepare_for_use()

        clf = ARSLVQ(sigma=0.5,
                     prototypes_per_class=2,
                     batch_size=5,
                     decay_rate=0.999)

        evaluator = EvaluatePrequential(show_plot=False,
                                        max_samples=20000,
                                        batch_size=5)

        evaluator.evaluate(stream, clf, model_names=['ARSLVQ'])

        measurements = np.asarray(evaluator.get_measurements()[0])[0]

        self.assertTrue(
            measurements.get_accuracy() >= 0.84,
            msg='Accuracy was {} but has to be greater than 0.84'.format(
                measurements.get_accuracy()))
        self.assertTrue(
            measurements.get_kappa() >= 0.68,
            msg='Kappa was {} but has to be greater than 0.68'.format(
                measurements.get_kappa()))
Beispiel #2
0
    def test_accuracy_stream(self):
        stream = ConceptDriftStream(stream=SEAGenerator(random_state=112,
                                                        noise_percentage=0.1),
                                    drift_stream=SEAGenerator(
                                        random_state=112,
                                        classification_function=1,
                                        noise_percentage=0.1),
                                    random_state=None,
                                    position=20000,
                                    width=50000)
        stream.prepare_for_use()

        clf = GLVQ(prototypes_per_class=6,
                   beta=2,
                   C=None,
                   decay_rate=0.9,
                   gradient_descent="SGD")

        evaluator = EvaluatePrequential(pretrain_size=1,
                                        show_plot=False,
                                        max_samples=20000,
                                        batch_size=1)

        evaluator.evaluate(stream, clf, model_names=['GLVQ'])

        measurements = np.asarray(evaluator.get_measurements()[0])[0]

        self.assertTrue(
            measurements.get_accuracy() >= 0.93,
            msg='Accuracy was {} but has to be greater than 0.93'.format(
                measurements.get_accuracy()))
        self.assertTrue(
            measurements.get_kappa() >= 0.84,
            msg='Kappa was {} but has to be greater than 0.84'.format(
                measurements.get_kappa()))
Beispiel #3
0
    def test_stream(self):
        stream = SEAGenerator(classification_function = 2, random_state = 112, balance_classes = False, noise_percentage = 0.28)     
        stream.prepare_for_use()

        evaluator = EvaluatePrequential(show_plot=False,max_samples=5000, 
                restart_stream=True,batch_size=10,metrics=['kappa', 'kappa_m', 'accuracy']) 

        evaluator.evaluate(stream=stream, model=RRSLVQ(prototypes_per_class=4,sigma=10))

        measurements = np.asarray(evaluator.get_measurements()[0])[0]
        self.assertIsNotNone(eval)
        self.assertTrue(measurements.get_accuracy() >= 0.5,
                        msg='Accuracy was {} but has to be greater than 0.5'.
                        format(measurements.get_accuracy()))
Beispiel #4
0
    def test_anwin(self):
        anwin = ANWIN(alpha=0.001)
        stream = SEAGenerator(classification_function=2,
                              random_state=112,
                              balance_classes=False,
                              noise_percentage=0.28)
        stream.prepare_for_use()

        stream.restart()
        detections, mean = [], []

        print("\n--------------------\n")
        for i in range(10000):
            data = stream.next_sample(10)
            batch = data[0][0][0]
            mean.append(batch)
            anwin.add_element(batch)
            if anwin.detected_change():
                print("\rIteration {}".format(i))
                print("\r ANWINReject Null Hypotheses")
                print(np.mean(mean))
                mean = []
                detections.append(i)

        print("----- Number of detections: " + str(len(detections)) + " -----")
        self.assertGreaterEqual(len(detections), 5)
Beispiel #5
0
def stream_example():
    """Create stream"""
    stream = SEAGenerator(noise_percentage=0.1)

    stream.prepare_for_use()
    """Init BRSLVQ"""
    clf = [
        RSLVQ(sigma=5.0, batch_size=1, n_epochs=1),
        RSLVQ(sigma=5.0, batch_size=5, n_epochs=1),
        RSLVQ(sigma=5.0, batch_size=10, n_epochs=1)
    ]
    """Evaluate"""
    evaluator = EvaluatePrequential(max_samples=10000,
                                    batch_size=100,
                                    show_plot=True)
    """Start evaluation"""
    evaluator.evaluate(stream=stream,
                       model=clf,
                       model_names=['bs=1', 'bs=5', 'bs=10'])
Beispiel #6
0
    def test_accuracy_stream(self):
        stream = SEAGenerator(random_state=42)
        stream.prepare_for_use()

        clf = GRLVQ(prototypes_per_class=2, regularization=5.0, beta=2, C=None)

        evaluator = EvaluatePrequential(pretrain_size=1,
                                        show_plot=False,
                                        max_samples=20000,
                                        batch_size=1)

        evaluator.evaluate(stream, clf, model_names=['GRLVQ'])

        measurements = np.asarray(evaluator.get_measurements()[0])[0]

        self.assertTrue(
            measurements.get_accuracy() >= 0.7,
            msg='Accuracy was {} but has to be greater than 0.7'.format(
                measurements.get_accuracy()))
        self.assertTrue(
            measurements.get_kappa() >= 0.3,
            msg='Kappa was {} but has to be greater than 0.3'.format(
                measurements.get_kappa()))
Beispiel #7
0
                    random_state=112,
                    balance_classes=False)
mixed_ra = ReoccuringDriftStream(stream=s1,
                                 drift_stream=s2,
                                 random_state=None,
                                 alpha=90.0,
                                 position=2000,
                                 width=100,
                                 pause=1000)
mixed_a = ConceptDriftStream(stream=s1,
                             drift_stream=s2,
                             alpha=90.0,
                             random_state=None,
                             position=int(study_size / 2),
                             width=1)
sea_a = ConceptDriftStream(stream=SEAGenerator(random_state=112,
                                               noise_percentage=0.1),
                           drift_stream=SEAGenerator(random_state=112,
                                                     classification_function=2,
                                                     noise_percentage=0.1),
                           alpha=90.0,
                           random_state=None,
                           position=int(study_size / 2),
                           width=1)

sea_ra = ReoccuringDriftStream(stream=SEAGenerator(random_state=112,
                                                   noise_percentage=0.1),
                               drift_stream=SEAGenerator(
                                   random_state=112,
                                   classification_function=2,
                                   noise_percentage=0.1),
                               alpha=90.0,
Beispiel #8
0
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  9 17:08:11 2018

@author: Moritz Heusinger <*****@*****.**>
"""

from skmultiflow.data.sea_generator import SEAGenerator
from rslvq import RSLVQ
from adaptive_rslvqs_batch import RSLVQ as BARSLVQ
from skmultiflow.evaluation.evaluate_prequential import EvaluatePrequential
from arslvq import RSLVQ as ARSLVQ
import numpy as np
from rslvq_stream import RSLVQ as MasterRSLVQ

stream = SEAGenerator()
stream.prepare_for_use()

clf = [
    RSLVQ(batch_size=5),
    BARSLVQ(gradient_descent='Adadelta',
            batch_size=5,
            decay_rate=0.9,
            sigma=1.0),
    RSLVQ(),
    MasterRSLVQ(gradient_descent='Adadelta', decay_rate=0.999, sigma=1.0)
]

evaluator = EvaluatePrequential(max_samples=100000,
                                batch_size=5,
                                show_plot=True)
Beispiel #9
0
def test_sea_generator(test_path):
    stream = SEAGenerator(classification_function=2,
                          random_state=112,
                          balance_classes=False,
                          noise_percentage=0.28)
    stream.prepare_for_use()

    assert stream.n_remaining_samples() == -1

    expected_names = ['att_num_0', 'att_num_1', 'att_num_2']
    assert stream.feature_names == expected_names

    expected_targets = [0, 1]
    assert stream.target_values == expected_targets

    assert stream.target_names == ['target_0']

    assert stream.n_features == 3

    assert stream.n_cat_features == 0

    assert stream.n_num_features == 3

    assert stream.n_targets == 1

    assert stream.get_data_info(
    ) == 'SEA Generator - 1 target(s), 2 classes, 3 features'

    assert stream.has_more_samples() is True

    assert stream.is_restartable() is True

    # Load test data corresponding to first 10 instances
    test_file = os.path.join(test_path, 'sea_stream.npz')
    data = np.load(test_file)
    X_expected = data['X']
    y_expected = data['y']

    X, y = stream.next_sample()
    assert np.alltrue(X[0] == X_expected[0])
    assert np.alltrue(y[0] == y_expected[0])

    X, y = stream.last_sample()
    assert np.alltrue(X[0] == X_expected[0])
    assert np.alltrue(y[0] == y_expected[0])

    stream.restart()
    X, y = stream.next_sample(10)
    assert np.alltrue(X == X_expected)
    assert np.alltrue(y == y_expected)

    assert stream.n_targets == np.array(y).ndim

    assert stream.n_features == X.shape[1]

    assert 'stream' == stream._estimator_type

    expected_info = "SEAGenerator(balance_classes=False, classification_function=2,\n" \
                    "             noise_percentage=0.28, random_state=112)"
    assert stream.get_info() == expected_info
Beispiel #10
0
 def test_correct_init(self):
     T_sne_stream_visualizer(stream=SEAGenerator(),
                             normalize=True,
                             path=None,
                             drift_handling='KS',
                             confidence=0.0001)
Beispiel #11
0
 def test_wrong_drift_handler(self):
     with self.assertRaises(TypeError):
         T_sne_stream_visualizer(stream=SEAGenerator(),
                                 drift_handling='TSWIN')
Beispiel #12
0
def init_standard_streams_naive_bayes(
):  # RBF Stream beinhaltet negative Werte daher muss dieser beim Naive Bayes Algortihmus weggelassen werden
    """Initialize standard data streams
    
    Standard streams are inspired by the experiment settings of 
    Gomes, Heitor Murilo & Bifet, Albert & Read, Jesse & Barddal, Jean Paul & 
    Enembreck, Fabrício & Pfahringer, Bernhard & Holmes, Geoff & 
    Abdessalem, Talel. (2017). Adaptive random forests for evolving data 
    stream classification. Machine Learning. 1-27. 10.1007/s10994-017-5642-8. 
    """
    agrawal_a = ConceptDriftStream(stream=AGRAWALGenerator(random_state=112,
                                                           perturbation=0.1),
                                   drift_stream=AGRAWALGenerator(
                                       random_state=112,
                                       classification_function=2,
                                       perturbation=0.1),
                                   random_state=None,
                                   alpha=90.0,
                                   position=21000000)
    agrawal_a.name = "agrawal_a"
    agrawal_g = ConceptDriftStream(stream=AGRAWALGenerator(random_state=112,
                                                           perturbation=0.1),
                                   drift_stream=AGRAWALGenerator(
                                       random_state=112,
                                       classification_function=1,
                                       perturbation=0.1),
                                   random_state=None,
                                   position=21000000,
                                   width=1000000)
    agrawal_g.name = "agrawal_g"
    hyper = HyperplaneGenerator(mag_change=0.001, noise_percentage=0.1)

    led_a = ConceptDriftStream(
        stream=LEDGeneratorDrift(has_noise=False,
                                 noise_percentage=0.0,
                                 n_drift_features=3),
        drift_stream=LEDGeneratorDrift(has_noise=False,
                                       noise_percentage=0.0,
                                       n_drift_features=7),
        random_state=None,
        alpha=90.0,  # angle of change grade 0 - 90
        position=21000000,
        width=1)

    led_a.name = "led_a"
    led_g = ConceptDriftStream(stream=LEDGeneratorDrift(has_noise=False,
                                                        noise_percentage=0.0,
                                                        n_drift_features=3),
                               drift_stream=LEDGeneratorDrift(
                                   has_noise=False,
                                   noise_percentage=0.0,
                                   n_drift_features=7),
                               random_state=None,
                               position=21000000,
                               width=1000000)
    led_g.name = "led_g"
    rand_tree = RandomTreeGenerator()
    rand_tree.name = "rand_tree"
    #rbf_if = RandomRBFGeneratorDrift(change_speed=0.001)
    #rbf_if.name = "rbf_if"
    #rbf_im = RandomRBFGeneratorDrift(change_speed=0.0001)
    #rbf_im.name = "rbf_im"
    sea_a = ConceptDriftStream(stream=SEAGenerator(random_state=112,
                                                   noise_percentage=0.1),
                               drift_stream=SEAGenerator(
                                   random_state=112,
                                   classification_function=2,
                                   noise_percentage=0.1),
                               alpha=90.0,
                               random_state=None,
                               position=21000000,
                               width=1)
    sea_a.name = "sea_a"
    sea_g = ConceptDriftStream(stream=SEAGenerator(random_state=112,
                                                   noise_percentage=0.1),
                               drift_stream=SEAGenerator(
                                   random_state=112,
                                   classification_function=1,
                                   noise_percentage=0.1),
                               random_state=None,
                               position=21000000,
                               width=1000000)
    sea_g.name = "sea_g"
    return [agrawal_a, agrawal_g, hyper, led_a, led_g, rand_tree, sea_a, sea_g]