This is a simple example of Support Vector Machines (SVM) using Python and scikit-learn.
Support Vector Machines (SVM) is a powerful supervised learning algorithm used for classification and regression tasks. In the context of classification, SVM aims to find the hyperplane that best separates different classes in the feature space. The hyperplane is chosen to maximize the margin between the classes, and the data points on the margin are called support vectors.
Key concepts of Support Vector Machines:
SVM is effective in high-dimensional spaces and is particularly useful when the number of features is greater than the number of samples. Additionally, SVM can handle non-linear relationships between features through the use of different kernel functions.
Python Source Code:
# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error
# Generate synthetic data
np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
# Split the 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)
# Train the Support Vector Machines (SVM) model
model = SVR(kernel='linear')
model.fit(X_train, y_train.ravel())
# Make predictions on the test set
y_pred = model.predict(X_test)
# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print(f'Mean Squared Error: {mse}')
# Plot the results
plt.scatter(X_test, y_test, color='black')
plt.scatter(X_test, y_pred, color='red', marker='x')
plt.title('Support Vector Machines (SVM) Example')
plt.xlabel('X')
plt.ylabel('y')
plt.show()
Explanation:
train_test_split
function.SVR
.