Search code examples
pythonmachine-learningperceptron

How to plot perceptron decision boundary and data set in python


I wrote multilayer-perceptron, using three layers (0,1,2). I want to plot the decision boundary and the data-set(eight features long) that i classified, Using python. How do i plot it on the screen, using one of the python libraries? Weight function -> matrix[3][8] Sample x -> vector[8]

#-- Trains the boundary decision, and test it. --#
def perceptron(x, y):
    m = len(x)
    d = len(x[0])
    eta = 0.1

    w = [[0 for k in range(d)] for j in range(3)]

    T = 2500
    for t in range(0, T):
        i = random.randint(0, m - 1)
        v = [float(j) for j in x[i]]
        y_hat = np.argmax(np.dot(w, v))
        if y_hat != y[i]:
            w[y[i]] = np.add(w[y[i]], np.array(v) * eta)
            w[y_hat] = np.subtract(w[y_hat], np.array(v) * eta)

    w_perceptron = w

    #-- Test the decision boundary that we trained. --#
    #-- Prints the loss weight function. --#
    M_perceptron = 0
    for t in range(0, m):
        y_hat = np.argmax(np.dot(w_perceptron, x[t]))
        if y[t] != y_hat:
            M_perceptron = M_perceptron + 1
    return float(M_perceptron) / m


def main():
    y = []
    x = [[]]
    x = readTrain_X(sys.argv[1], x) # Reads data trainning set.
    readTrain_Y(sys.argv[2], y) # Reads right classified training set.

    print(perceptron(x, y))

Solution

  • You cannot plot 8 features. There is no way you can visualize a 8D space. But what you can do is to perform dimensionality reduction using PCA/t-SNE to 2D for visualization. If you can reduce it to 2D then you can use create a grid of values and use the probabilities returned by the model to visualize the decision boundary.

    Reference: Link