perceptron algorithm in matlab

main.m
function [weights, bias] = perceptron(X, y, learning_rate, epochs)
    [m, n] = size(X);
    
    weights = zeros(1, n);
    bias = 0;
    
    for epoch = 1:epochs
        for i = 1:m
            prediction = dot(weights, X(i,:)) + bias;
            if y(i) * prediction <= 0
                weights = weights + learning_rate * y(i) * X(i,:);
                bias = bias + learning_rate * y(i);
            end
        end
    end
end
436 chars
17 lines

This is a simple implementation of the perceptron algorithm in MATLAB. Make sure to pass the input features X, target labels y, learning rate, and number of epochs as arguments to the function. The function will return the learned weights and bias.

related categories

gistlibby LogSnag