Dev Duniya
Mar 19, 2025
Support Vector Machines (SVM) are a powerful supervised machine learning algorithm used for both classification and regression tasks. SVMs are known for their ability to find the optimal decision boundary that maximizes the margin between different classes, leading to robust and effective models.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# Load the iris dataset
iris = load_iris()
# Select two features and two classes
X = iris.data[:100, [1, 2]] # Sepal Width and Petal Length
y = iris.target[:100]
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create an SVM classifier with an RBF kernel
svm_classifier = SVC(kernel='rbf', C=1.0, gamma='scale')
# Train the model
svm_classifier.fit(X_train, y_train)
# Make predictions on the test set
y_pred = svm_classifier.predict(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
Radial Basis Function (RBF) Kernel: Maps data to an infinite-dimensional space.
Polynomial Kernel: Maps data to a polynomial space.
Assumption: Assumes that the data is perfectly linearly separable.
Goal: Find the hyperplane that perfectly separates the data without any misclassifications.
Limitations:
More realistic: Allows for some misclassification to handle noisy or overlapping data.
Introduces a penalty: Introduces a penalty term in the optimization objective to allow for a small number of misclassifications.
Regularization Parameter (C): Controls the trade-off between maximizing the margin and minimizing the number of misclassifications.
Influence of data points: The gamma parameter in the RBF kernel controls the influence of individual data points on the decision boundary.
Support Vector Machines are a powerful and versatile machine learning algorithm with a strong theoretical foundation. By understanding the key concepts and techniques, you can effectively apply SVM to a wide range of classification and regression problems.