简体   繁体   中英

Finding number of misclassified points in perceptron Algorithm - Linear Classifier Machine Learning

From the given script, how do I now find the number of misclassified points. Please Help

    import numpy as np

    DATA = np.loadtxt("C:/Users/abish/OneDrive/Desktop/Machine Learning/data_Perceptron.txt")
    X = DATA[:, 0:2]
    Y = DATA[:, 2]

    import matplotlib.pyplot as plt
    fig, ax = plt.subplots(1, 1, figsize=(5, 5))
    ax.scatter(X[:, 0], X[:, 1], c=Y)
    ax.set_title('ground truth', fontsize=20)
    plt.xlabel('X1')
    plt.ylabel('X2')
    plt.show()


    # Add a bias to the X vector
    X_bias = np.ones([X.shape[0], 3])
    X_bias[:, 1:3] = X
    # Initialize weight vector with zeros
    w = np.zeros([3, 1])


    # Define the activation function  that returns either 1 or 0
    def activation(x):
        return 1 if x >= 1 else 0

    # A function to calculate the unit vector of our weights vector
    def calc_unit_vector(x):
        return x.transpose() / np.sqrt(x.transpose().dot(x))

    # A function that returns values that lay on the hyperplane
    def calc_hyperplane(X, w):
        return np.ravel([-(w[0] + x * w[1]) / w[2] for x in X])

    for _ in range(10):
        for i in range(X_bias.shape[0]):
            y = activation(w.transpose().dot(X_bias[i, :]))
            # Update weights
            w = w + ((Y[i] - y) * X_bias[i, :]).reshape(w.shape[0], 1)

    print('w0 = ', w[0])
    print('w1 = ', w[1])
    print('w2 = ', w[2])

    # Calculate the class of the data points with the weight vector
    result = [w.transpose().dot(x) for x in X_bias]
    result_class = [activation(w.transpose().dot(x)) for x in X_bias]

    # Calculate unit vector
    w = calc_unit_vector(w).transpose()

    fig, ax = plt.subplots(1, 2, figsize=(15, 5))
    ax[0].scatter(X[:, 0], X[:, 1], c=Y)
    ax[0].set_title('ground truth', fontsize=20)

    ax[1].scatter(X[:, 0], X[:, 1], c=result_class)
    ax[1].plot([-20, 20], calc_hyperplane([-20, 20], w), lw=3, c='red')
    ax[1].set_xlim(ax[0].get_xlim())
    ax[1].set_ylim(ax[0].get_ylim())
    ax[1].set_yticks([])
    ax[1].set_title('Perceptron classification with hyperplane', fontsize=20)
    plt.show()

    # calculate misclassified points
    misclassified =
    fig, ax = plt.subplots(1, 1, figsize=(5, 5))
    ax.scatter(X[:, 0], X[:, 1], c=misclassified)
    ax.set_title('misclassified points', fontsize=20)
    plt.xlabel('X1')
    plt.ylabel('X2')
    plt.show()

I need help in calculating the number of misclassified points, I did the other parts but the last part is very confusing.

Please suggest, how do I calculate the number of misclassified points and plot them.

You can check the dissimilar points in the results and the actual class array (Y) using a simple if condition and plot only the dissimilar points with their actual class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM