Write a Perceptron Classifier for Binary Classification
Created | |
---|---|
Tags | ML Coding |
Implementing a Perceptron classifier from scratch is another instructive task that can help assess a candidate's grasp of machine learning fundamentals. The Perceptron is a type of linear classifier and serves as the foundation for neural networks. Writing a Perceptron involves understanding of linear algebra, gradient-based optimization, and the basics of supervised learning.
Task: Write a Perceptron Classifier for Binary Classification
Prompt: "Using Python, implement a Perceptron classifier from scratch to separate two classes in a binary classification problem. Assume the input features are numeric and pre-processed. Your implementation should include methods for training the model using the Perceptron learning algorithm and for making predictions with the trained model."
Example Implementation
Here's a simplified version of a Perceptron implementation in Python:
import numpy as np
class Perceptron:
def __init__(self, learning_rate=0.01, n_iters=1000):
self.lr = learning_rate
self.n_iters = n_iters
self.activation_func = self._unit_step_func
self.weights = None
self.bias = None
def _unit_step_func(self, x):
return np.where(x>=0, 1, 0)
def fit(self, X, y):
n_samples, n_features = X.shape
# Initialize weights and bias
self.weights = np.zeros(n_features)
self.bias = 0
y_ = np.array([1 if i > 0 else 0 for i in y])
for _ in range(self.n_iters):
for idx, x_i in enumerate(X):
linear_output = np.dot(x_i, self.weights) + self.bias
y_predicted = self.activation_func(linear_output)
# Perceptron update rule
update = self.lr * (y_[idx] - y_predicted)
self.weights += update * x_i
self.bias += update
def predict(self, X):
linear_output = np.dot(X, self.weights) + self.bias
y_predicted = self.activation_func(linear_output)
return y_predicted
# Example usage
X = np.array([[1, 2], [2, 3], [3, 5], [1, 1]])
y = np.array([1, 1, 0, 0]) # Binary labels
perceptron = Perceptron(learning_rate=0.01, n_iters=1000)
perceptron.fit(X, y)
predictions = perceptron.predict(X)
print(predictions) # Outputs the predictions for the input features
Associated Questions
- Model Understanding: "Can you explain the intuition behind the Perceptron learning algorithm and how it updates its weights?"
- Activation Function: "Why did you choose the unit step function as the activation function for the Perceptron? What role does it play?"
- Convergence: "Under what conditions does the Perceptron algorithm converge? What happens if the data is not linearly separable?"
- Feature Scaling: "How does feature scaling affect the Perceptron's performance? Would you recommend scaling the input features?"
- Limitations and Extensions: "What are the limitations of the basic Perceptron model? How can it be extended or modified to overcome some of these limitations?"
These questions aim to delve into the candidate's understanding of the core concepts behind the Perceptron, including its strengths and weaknesses, and its place within the broader context of machine learning algorithms. This exercise tests not only coding skills but also theoretical knowledge and the ability to apply basic principles to solve classification problems.