Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S23L04 -SVM implementation using python

Machine Leaning Articles
1 views

S23L04 -SVM implementation using python


May 15, 2025

Implementing Support Vector Machines (SVM) in Python: A Comprehensive Guide

Welcome to our in-depth guide on implementing Support Vector Machines (SVM) using Python's scikit-learn library. Whether you're a data science enthusiast or a seasoned professional, this article will walk you through the entire process—from understanding the foundational concepts of SVM to executing a complete implementation using a Jupyter Notebook. Let's dive in!

Table of Contents

  1. Introduction to Support Vector Machines (SVM)
  2. Setting Up the Environment
  3. Data Exploration and Preprocessing
    • Handling Missing Data
    • Encoding Categorical Variables
    • Feature Selection
  4. Splitting the Dataset
  5. Feature Scaling
  6. Building and Evaluating Models
    • K-Nearest Neighbors (KNN)
    • Logistic Regression
    • Gaussian Naive Bayes
    • Support Vector Machine (SVM)
  7. Visualizing Decision Regions
  8. Conclusion
  9. References

1. Introduction to Support Vector Machines (SVM)

Support Vector Machines (SVM) are powerful supervised learning models used for classification and regression tasks. They are particularly effective in high-dimensional spaces and are versatile, thanks to the use of different kernel functions. SVMs aim to find the optimal hyperplane that best separates data points of different classes with the maximum margin.

Key Features of SVM:

  • Margin Optimization: SVMs maximize the margin between classes to ensure better generalization.
  • Kernel Trick: Allows SVMs to perform well in non-linear classification by transforming data into higher dimensions.
  • Robustness: Effective in cases with clear margin of separation and even in high-dimensional spaces.

2. Setting Up the Environment

Before we begin, ensure you have the necessary libraries installed. You can install them using pip:

pip install pandas numpy scikit-learn seaborn matplotlib mlxtend

Note: mlxtend is used for visualizing decision regions.

3. Data Exploration and Preprocessing

Data preprocessing is a crucial step in any machine learning pipeline. It involves cleaning the data, handling missing values, encoding categorical variables, and selecting relevant features.

3.1 Handling Missing Data

Missing data can adversely affect the performance of machine learning models. We'll handle missing values by:

  • Numeric Features: Imputing missing values with the mean.
  • Categorical Features: Imputing missing values with the most frequent value.
import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer

# Load the dataset
data = pd.read_csv('weatherAUS.csv')

# Separate features and target
X = data.iloc[:, :-1]
y = data.iloc[:, -1]

# Handle numeric missing values
numeric_cols = X.select_dtypes(include=['int64', 'float64']).columns
imputer_numeric = SimpleImputer(strategy='mean')
X[numeric_cols] = imputer_numeric.fit_transform(X[numeric_cols])

# Handle categorical missing values
categorical_cols = X.select_dtypes(include=['object']).columns
imputer_categorical = SimpleImputer(strategy='most_frequent')
X[categorical_cols] = imputer_categorical.fit_transform(X[categorical_cols])

3.2 Encoding Categorical Variables

Machine learning models require numerical input. We'll convert categorical variables using:

  • Label Encoding: For binary or high-cardinality categories.
  • One-Hot Encoding: For categories with a limited number of unique values.
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer

# Label Encoding function
def label_encode(series):
    le = LabelEncoder()
    return le.fit_transform(series)

# Apply Label Encoding to target variable
y = label_encode(y)

# Identify columns for encoding
def encoding_selection(X, threshold=10):
    string_cols = X.select_dtypes(include=['object']).columns
    one_hot_cols = [col for col in string_cols if X[col].nunique() <= threshold]
    label_encode_cols = [col for col in string_cols if X[col].nunique() > threshold]
    
    # Label Encode
    for col in label_encode_cols:
        X[col] = label_encode(X[col])
    
    # One-Hot Encode
    ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), one_hot_cols)], remainder='passthrough')
    X = ct.fit_transform(X)
    return X

X = encoding_selection(X)

3.3 Feature Selection

Selecting relevant features can improve model performance and reduce computational complexity. We'll use SelectKBest with the Chi-Squared statistic.

from sklearn.feature_selection import SelectKBest, chi2
from sklearn.preprocessing import MinMaxScaler

# Scale features
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)

# Select top 2 features
selector = SelectKBest(score_func=chi2, k=2)
X_selected = selector.fit_transform(X_scaled, y)

4. Splitting the Dataset

We'll split the dataset into training and testing sets to evaluate the model's performance on unseen data.

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X_selected, y, test_size=0.20, random_state=1)

5. Feature Scaling

Feature scaling ensures that all features contribute equally to the model's performance.

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler(with_mean=False)
scaler.fit(X_train)

X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

6. Building and Evaluating Models

We'll build four different models to compare their performance:

  • K-Nearest Neighbors (KNN)
  • Logistic Regression
  • Gaussian Naive Bayes
  • Support Vector Machine (SVM)

6.1 K-Nearest Neighbors (KNN)

from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score

knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
y_pred_knn = knn.predict(X_test)
accuracy_knn = accuracy_score(y_pred_knn, y_test)
print(f'KNN Accuracy: {accuracy_knn:.4f}')

Output:

KNN Accuracy: 0.8003

6.2 Logistic Regression

from sklearn.linear_model import LogisticRegression

log_reg = LogisticRegression(random_state=0, max_iter=200)
log_reg.fit(X_train, y_train)
y_pred_lr = log_reg.predict(X_test)
accuracy_lr = accuracy_score(y_pred_lr, y_test)
print(f'Logistic Regression Accuracy: {accuracy_lr:.4f}')

Output:

Logistic Regression Accuracy: 0.8297

6.3 Gaussian Naive Bayes

from sklearn.naive_bayes import GaussianNB

gnb = GaussianNB()
gnb.fit(X_train, y_train)
y_pred_gnb = gnb.predict(X_test)
accuracy_gnb = accuracy_score(y_pred_gnb, y_test)
print(f'Gaussian Naive Bayes Accuracy: {accuracy_gnb:.4f}')

Output:

Gaussian Naive Bayes Accuracy: 0.7960

6.4 Support Vector Machine (SVM)

from sklearn.svm import SVC

svc = SVC()
svc.fit(X_train, y_train)
y_pred_svc = svc.predict(X_test)
accuracy_svc = accuracy_score(y_pred_svc, y_test)
print(f'SVM Accuracy: {accuracy_svc:.4f}')

Output:

SVM Accuracy: 0.8282

Summary of Model Accuracies:

Model Accuracy
KNN 80.03%
Logistic Regression 82.97%
Gaussian Naive Bayes 79.60%
SVM 82.82%

Among the models evaluated, Logistic Regression slightly outperforms SVM, followed closely by SVM itself.

7. Visualizing Decision Regions

Visualizing decision boundaries helps in understanding how different models classify the data.

from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt
from sklearn import datasets

# Load Iris dataset for visualization
iris = datasets.load_iris()
X_vis = iris.data[:, :2]
y_vis = iris.target

# Initialize models
knn_vis = KNeighborsClassifier(n_neighbors=3)
log_reg_vis = LogisticRegression(random_state=0, max_iter=200)
gnb_vis = GaussianNB()
svc_vis = SVC()

# Fit models
knn_vis.fit(X_vis, y_vis)
log_reg_vis.fit(X_vis, y_vis)
gnb_vis.fit(X_vis, y_vis)
svc_vis.fit(X_vis, y_vis)

# Visualization function
def visualize_decision_regions(X, y, model, title):
    plot_decision_regions(X, y, clf=model, legend=2)
    plt.title(title)
    plt.xlabel('Feature 1')
    plt.ylabel('Feature 2')
    plt.show()

# Plot decision regions for each model
visualize_decision_regions(X_vis, y_vis, knn_vis, 'K-Nearest Neighbors Decision Regions')
visualize_decision_regions(X_vis, y_vis, log_reg_vis, 'Logistic Regression Decision Regions')
visualize_decision_regions(X_vis, y_vis, gnb_vis, 'Gaussian Naive Bayes Decision Regions')
visualize_decision_regions(X_vis, y_vis, svc_vis, 'SVM Decision Regions')

Visualizations:

Each model's decision boundaries will be displayed in separate plots, illustrating how they classify different regions in the feature space.

8. Conclusion

In this guide, we've explored the implementation of Support Vector Machines (SVM) using Python's scikit-learn library. Starting from data preprocessing to building and evaluating various models, including SVM, we've covered essential steps in a typical machine learning pipeline. Additionally, visualizing decision regions provided deeper insights into how different algorithms perform classification tasks.

Key Takeaways:

  • Data Preprocessing: Crucial for cleaning and preparing data for modeling.
  • Feature Selection and Scaling: Enhance model performance and efficiency.
  • Model Comparison: Evaluating multiple algorithms helps in selecting the best performer for your dataset.
  • Visualization: A powerful tool for understanding model behavior and decision-making processes.

By following this comprehensive approach, you can effectively implement SVM and other classification algorithms to solve real-world problems.

9. References

  • Scikit-Learn Documentation
  • Support Vector Machines - Wikipedia
  • MLxtend: Machine Learning Extensions

Thank you for reading! If you have any questions or feedback, feel free to leave a comment below.

Share Article

Back to Blog

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.