class SVM:
def __init__(self, learning_rate=0.001, lambda_param=0.01, n_iters=1000):
“””
Initialize the SVM mannequin with hyperparameters.
Parameters:
– learning_rate: The step measurement for updating weights throughout coaching.
– lambda_param: Regularization parameter to stop overfitting.
– n_iters: Variety of iterations for coaching the mannequin.
“””
self.lr = learning_rate # Studying charge
self.lambda_param = lambda_param # Regularization parameter
self.n_iters = n_iters # Variety of coaching iterations
self.w = None # Weights vector
self.b = None # Bias time period
def match(self, X, y):
“””
Practice the SVM mannequin utilizing gradient descent.
Parameters:
– X: Function matrix (n_samples x n_features).
– y: Goal labels (n_samples,). Labels needs to be -1 or 1.
“””
n_samples, n_features = X.form # Get the variety of samples and options
# Guarantee labels are both -1 or 1
y_ = np.the place(y <= 0, -1, 1)
# Initialize weights as zeros and bias as zero
self.w = np.zeros(n_features)
self.b = 0
# Coaching utilizing Stochastic Gradient Descent (SGD)
for _ in vary(self.n_iters):
for idx, x_i in enumerate(X): # Iterate via all coaching samples
# Test if the pattern is accurately categorised
situation = y_[idx] * (np.dot(x_i, self.w) + self.b) >= 1
if situation:
# If accurately categorised, apply solely regularization replace
self.w -= self.lr * (2 * self.lambda_param * self.w)
else:
# If misclassified, replace weights and bias
self.w -= self.lr * (2 * self.lambda_param * self.w – np.dot(x_i, y_[idx]))
self.b -= self.lr * y_[idx]
def predict(self, X):
“””
Predict the category labels for enter knowledge.
Parameters:
– X: Function matrix (n_samples x n_features).
Returns:
– Class labels (-1 or 1) for every enter pattern.
“””
return np.signal(np.dot(X, self.w) + self.b)
# 🔥 Instance Utilization
X_train = np.array([[1, 2], [2, 3], [3, 4], [5, 5], [1, 0]]) # Coaching characteristic matrix
y_train = np.array([1, 1, -1, -1, 1]) # Coaching labels
# Initialize and practice the SVM mannequin
svm = SVM()
svm.match(X_train, y_train)
# Take a look at the mannequin with new knowledge
X_test = np.array([[3, 3], [1, 1]]) # Take a look at characteristic matrix
print(svm.predict(X_test)) # Output: Predicted class labels (both -1 or 1)