Beispiel #1
0
    plt.grid(1)
    plt.show()


if __name__ == '__main__':
    X, Y = generate_random_points(class_count=[
        500,
        500,
    ])
    print("Random 2D Normally distributed Points Generated...\n")

    # split the data to train test with 80:20 ratio
    X_tr, X_ts, Y_tr, Y_ts = train_test_split(X, Y, test_size=0.2)

    # perceptron model
    model = Perceptron(shuffle=1, verbose=0)

    # converting to  list as the perceptron model doesn't recognize np arrays.
    mat = model.fit(X_tr, Y_tr)

    print("\nThe Weight and Bias :")
    model.display_weight()
    print("\nAccuracy : ")
    print("Train Accuracy : ", model.accuracy(Y_tr, model.predict(X_tr)))
    print("Test Accuracy : ", model.accuracy(Y_ts, model.predict(X_ts)))
    print("No of iterations : ", model.n_iter)
    if len(set(Y)) == 2:
        plot_2d(X, Y, model.w, model.b)
    else:
        print("Not plotting as the data is not 2D")
for dot in dataset.dots:
    perceptron.SetInputs([dot.x, dot.y, bias])
    if (dot.y > line.f(dot.x)):
        answer = 1
    else:
        answer = -1
    guess = perceptron.Estimate()
    if (guess == answer):
        goodGuessCount += 1
    color = ''
    if (guess == 1):
        color = 'ro'
    else:
        color = 'bo'
    plt.plot(dot.x, dot.y, color)
    perceptron.accuracy = goodGuessCount / len(dataset.dots)

print("Loss = " + "{:.2f}".format(1 - perceptron.GetAccuracy()))

# Drawing an estimated separation line from the model weights
w0 = perceptron.weights[0]
w1 = perceptron.weights[1]
w2 = perceptron.weights[2]
a = -(w0 / w1)
b = -(w2 / w1)
guessed_line = Function(a, b)
y2 = guessed_line.a * x + guessed_line.b
plt.plot(x, y2, 'g--')  # Green dashes

# Showing the plot
plt.axis([-1, 1, -1, 1])
Beispiel #3
0
from perceptron import Perceptron
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

if __name__ == '__main__':
    X = load_iris().data
    Y = load_iris().target

    # split the data to train test with 80:20 ratio
    X_tr, X_ts, Y_tr, Y_ts = train_test_split(X, Y, test_size=0.2)

    # perceptron model
    model = Perceptron(shuffle=1, verbose=0)

    # converting to  list as the perceptron model doesn't recognize np arrays.
    mat = model.fit(list(X_tr), list(Y_tr))

    print("The Weight and Bias :")
    model.display_weight()
    print("\nAccuracy : ")
    print("Train Accuracy : ",
          model.accuracy(list(Y_tr), model.predict(list(X_tr))))
    print("Test Accuracy : ",
          model.accuracy(list(Y_ts), model.predict(list(X_ts))))
    print("No of iterations : ", model.n_iter)