6 min read•august 16, 2024
Support Vector Machines (SVMs) are powerful classification tools that find the optimal separating classes in feature space. They maximize the margin between classes, reducing overfitting and improving generalization. SVMs work well for both linear and non-linear data.
SVMs use to handle non-linear data, mapping it to higher dimensions for easier separation. Common kernels include linear, polynomial, and (RBF). SVMs can be evaluated using metrics like , , , and ROC curves, with to prevent overfitting.
from sklearn.svm import SVC from sklearn.preprocessing import StandardScaler # Preprocess data scaler = StandardScaler() X_scaled = scaler.fit_transform(X) # Create and train linear SVM model svm_model = SVC(kernel='linear', C=1.0) svm_model.fit(X_scaled, y) # Make predictions predictions = svm_model.predict(X_test_scaled)
from sklearn.svm import SVC from sklearn.preprocessing import StandardScaler from sklearn.model_selection import GridSearchCV # Preprocess data scaler = StandardScaler() X_scaled = scaler.fit_transform(X) # Define parameter grid for hyperparameter tuning param_grid = {'C': [0.1, 1, 10], 'gamma': [0.1, 1, 10]} # Create and train non-linear SVM model with grid search svm_model = GridSearchCV(SVC(kernel='rbf'), param_grid, cv=5) svm_model.fit(X_scaled, y) # Make predictions using best model predictions = svm_model.predict(X_test_scaled)
from sklearn.model_selection import cross_val_score, learning_curve from sklearn.metrics import confusion_matrix, roc_auc_score import numpy as np import matplotlib.pyplot as plt # Perform cross-validation cv_scores = cross_val_score(svm_model, X_scaled, y, cv=5) print(f"Cross-validation scores: {cv_scores}") print(f"Mean CV score: {np.mean(cv_scores)}") # Generate [confusion matrix](https://www.fiveableKeyTerm:confusion_matrix) cm = confusion_matrix(y_test, predictions) print("Confusion Matrix:") print(cm) # Calculate ROC AUC score roc_auc = roc_auc_score(y_test, svm_model.decision_function(X_test_scaled)) print(f"ROC AUC Score: {roc_auc}") # Plot learning curve train_sizes, train_scores, valid_scores = learning_curve( svm_model, X_scaled, y, train_sizes=np.linspace(0.1, 1.0, 10), cv=5 ) plt.plot(train_sizes, np.mean(train_scores, axis=1), label='Training score') plt.plot(train_sizes, np.mean(valid_scores, axis=1), label='Validation score') plt.xlabel('Training examples') plt.ylabel('Score') plt.legend() plt.show()
from sklearn.decomposition import KernelPCA from sklearn.pipeline import Pipeline # Create pipeline with Kernel PCA and SVM pipeline = Pipeline([ ('kpca', KernelPCA(n_components=50, kernel='rbf')), ('svm', SVC(kernel='linear')) ]) # Fit pipeline to data pipeline.fit(X_train, y_train) # Make predictions predictions = pipeline.predict(X_test)