Skip to content
Snippets Groups Projects
apply_pla.py 1.69 KiB
Newer Older
Simon Adick's avatar
Simon Adick committed
import random

import numpy

from algorithm.pla.perceptron import Perceptron
from algorithm.pla.perceptron_learning_algorithm import train
from aufgaben.p4.binary_classification import binary_classification_feature
from aufgaben.p6.error_rate import ErrorRate

CLASS_JOGGEN = 1
CLASS_KNIEBEUGE = -1


def apply_pla():
    # Hole die aus den Sensordaten berechneten Merkmale
    joggen_feature, kniebeuge_feature = binary_classification_feature(150)

    # Wandel Liste an Merkmalen in einzelne Merkmalsvektoren um
    joggen_vector = ([element] for element in joggen_feature)
    kniebeugen_vector = ([element] for element in kniebeuge_feature)

    # Weise den Trainingsdaten eine Klasse zu
    # 0 = Kniebeuge, 1 = Joggen
    training_data_kniebeuge = zip(kniebeugen_vector, [CLASS_KNIEBEUGE] * len(kniebeuge_feature))
    training_data_joggen = zip(joggen_vector, [CLASS_JOGGEN] * len(joggen_feature))
    training_data = list(training_data_joggen) + list(training_data_kniebeuge)

    # Erstelle ein Perzeptron
    weights = [random.random()]
    threshold = 0.5
    perceptron = Perceptron(weights, threshold, numpy.sign)

    # Trainiere das Perzeptron
Simon Adick's avatar
Simon Adick committed
    train(perceptron, training_data, 300, 0.1)

    # Vergleiche alle Ergebnisse mit der erwarteten Klasse
Simon Adick's avatar
Simon Adick committed
    fehlerrate = ErrorRate()
    for features, correct_class in training_data:
        result = perceptron.classify(features)
        fehlerrate.evaluate(correct_class, result)
    fehlerrate.print_table()
    # Berechne den Grenzwert, ab wann ein Feature anders eingeordnet wird
    grenzwert = - perceptron.weights[0] / perceptron.weights[1]
    return joggen_feature, kniebeuge_feature, grenzwert
Simon Adick's avatar
Simon Adick committed


if __name__ == '__main__':
    apply_pla()
    # apply_pocket()