Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S14L02 - SVR under Python

Machine Leaning Articles
1 views

S14L02 - SVR under Python


May 15, 2025

Unlocking the Power of Support Vector Regression (SVR) in Python: A Comprehensive Guide

Table of Contents

  1. Introduction
  2. What is Support Vector Regression (SVR)?
  3. Why Choose SVR?
  4. Dataset Overview: Insurance Data Analysis
    1. Dataset Features:
  5. Data Preprocessing
    1. 1. Importing Libraries
    2. 2. Loading the Dataset
    3. 3. Separating Features and Target Variable
    4. 4. Label Encoding
    5. 5. One-Hot Encoding
    6. 6. Splitting the Data
  6. Building and Training the SVR Model
    1. 1. Importing SVR
    2. 2. Initializing and Training the Model
  7. Making Predictions and Evaluating the Model
    1. 1. Predictions
    2. 2. Comparing Actual vs. Predicted Values
    3. 3. Model Evaluation
  8. Interpreting the Results
    1. Why Did SVR Underperform?
  9. Enhancing SVR Performance
    1. 1. Feature Scaling:
    2. 2. Hyperparameter Tuning:
    3. 3. Alternative Models:
  10. Conclusion
  11. Additional Resources
  12. FAQs

Introduction

In the vast landscape of machine learning, regression models play a pivotal role in predicting continuous outcomes. Among these models, Support Vector Regression (SVR) stands out as a powerful yet often underutilized tool. While Support Vector Machines (SVMs) are predominantly favored for classification tasks, SVR offers a unique approach to tackling regression problems. This comprehensive guide delves into the intricacies of SVR, its implementation in Python, and its performance in real-world scenarios, particularly using an insurance dataset.

What is Support Vector Regression (SVR)?

Support Vector Regression is an extension of the Support Vector Machine (SVM) algorithm tailored for regression tasks. Unlike traditional regression models that aim to minimize the error between predicted and actual values, SVR focuses on the epsilon-insensitive loss function. This approach allows SVR to create a margin of tolerance (epsilon) within which errors are disregarded, leading to a more robust model against outliers.

Why Choose SVR?

While SVR is a robust tool for regression, it's essential to understand its positioning in the realm of machine learning:

  • Strengths:
    • Effective in high-dimensional spaces.
    • Robust against overfitting, especially in cases with limited data points.
    • Utilizes kernel functions to model non-linear relationships.
  • Weaknesses:
    • Computationally intensive, making it less suitable for large datasets.
    • Hyperparameter tuning can be complex.
    • Often outperformed by ensemble methods like Random Forests or Gradient Boosting in regression tasks.

Given these characteristics, SVR is best suited for specific scenarios where its strengths can be fully leveraged.

Dataset Overview: Insurance Data Analysis

To illustrate the implementation of SVR, we'll use the Insurance Dataset from Kaggle. This dataset provides information on individuals' demographics and health-related attributes, aiming to predict insurance charges.

Dataset Features:

  • age: Age of the primary beneficiary.
  • sex: Gender of the individual.
  • bmi: Body mass index.
  • children: Number of children covered by health insurance.
  • smoker: Indicator if the individual smokes.
  • region: Residential area in the US.
  • charges: Medical costs billed by health insurance.

Data Preprocessing

Effective data preprocessing is paramount to the success of any machine learning model. Here's a step-by-step breakdown of the preprocessing steps using Python's pandas and sklearn libraries.

1. Importing Libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

2. Loading the Dataset

# Load the insurance dataset
data = pd.read_csv('S07_datasets_13720_18513_insurance.csv')
print(data.head())

Sample Output:

age sex bmi children smoker region charges
19 female 27.900 0 yes southwest 16884.92400
18 male 33.770 1 no southeast 1725.55230
28 male 33.000 3 no southeast 4449.46200
33 male 22.705 0 no northwest 21984.47061
32 male 28.880 0 no northwest 3866.85520

3. Separating Features and Target Variable

X = data.iloc[:,:-1]  # Features
Y = data.iloc[:,-1]   # Target variable (charges)

4. Label Encoding

Categorical variables need to be converted into numerical formats. We use Label Encoding for binary categories like 'sex' and 'smoker'.

from sklearn import preprocessing
le = preprocessing.LabelEncoder()

# Encode 'sex' and 'smoker' columns
X['sex'] = le.fit_transform(X['sex'])
X['smoker'] = le.fit_transform(X['smoker'])
print(X.head())

Sample Output:

...
age sex bmi children smoker region
19 0 27.9 0 1 southwest
18 1 33.77 1 0 southeast
28 1 33.0 3 0 southeast
33 1 22.705 0 0 northwest
32 1 28.88 0 0 northwest

5. One-Hot Encoding

For categorical variables with more than two categories, One-Hot Encoding is preferred. Here, the 'region' column is one such categorical variable.

from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer

# Apply One-Hot Encoding to the 'region' column
columnTransformer = ColumnTransformer([('encoder', OneHotEncoder(), [5])], remainder='passthrough')
X = columnTransformer.fit_transform(X)
print(X)

Sample Output:

[[0.    0.    0.   ... 27.9   0.    1.  ]
 [0.    0.    1.   ... 33.77  1.    0.  ]
 [0.    0.    1.   ... 33.    3.    0.  ]
 ...
 [0.    0.    1.   ... 36.85  0.    0.  ]
 [0.    0.    0.   ... 25.8   0.    0.  ]
 [0.    1.    0.   ... 29.07  0.    1.  ]]

6. Splitting the Data

We divide the dataset into training and testing sets to evaluate the model's performance.

from sklearn.model_selection import train_test_split

# Split the data: 80% training and 20% testing
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.20, random_state=1)

Building and Training the SVR Model

With the data preprocessed, we can now build the SVR model using sklearn.

1. Importing SVR

from sklearn.svm import SVR

2. Initializing and Training the Model

# Initialize the SVR model with default parameters
model = SVR()

# Train the model on the training data
model.fit(X_train, y_train)

Model Output:

SVR()

Making Predictions and Evaluating the Model

After training, we use the model to make predictions on the test set and evaluate its performance using the R² score.

1. Predictions

# Predict on the test data
y_pred = model.predict(X_test)

2. Comparing Actual vs. Predicted Values

# Create a DataFrame to compare actual and predicted charges
comparison = pd.DataFrame()
comparison['Actual'] = y_test
comparison['Predicted'] = y_pred
print(comparison.head())

Sample Output:

Actual Predicted
1646.43 9111.903501
11353.23 9307.009935
8798.59 9277.155786
10381.48 9265.538282
2103.08 9114.774006

3. Model Evaluation

The R² score indicates how well the model's predictions match the actual data. An R² score closer to 1 signifies a better fit.

from sklearn.metrics import r2_score

# Calculate the R² score
r2 = r2_score(y_test, y_pred)
print(f'R² Score: {r2}')

Output:

R² Score: -0.1157396589643176

Interpreting the Results

An R² score of -0.1157 signifies that the SVR model performs poorly on the given dataset. In regression analysis, negative R² values indicate that the model fits the data worse than a horizontal line (i.e., worse than simply predicting the mean of the target variable).

Why Did SVR Underperform?

Several factors can contribute to the underperformance of SVR in this scenario:

  1. Default Hyperparameters: SVR's performance is highly sensitive to its hyperparameters (e.g., kernel type, C, epsilon). Using default settings may not capture the underlying patterns in the data effectively.
  2. Dataset Size: SVR can be computationally intensive, especially with larger datasets. The insurance dataset, with 1,338 records, may still pose challenges for SVR to generalize effectively.
  3. Feature Scaling: SVR requires input features to be scaled appropriately. Lack of feature scaling can lead to suboptimal performance.
  4. Non-linear Relationships: While SVR can handle non-linear relationships using kernel functions, the choice of kernel and its parameters greatly influence performance.

Enhancing SVR Performance

To improve the performance of the SVR model, consider the following steps:

1. Feature Scaling:

from sklearn.preprocessing import StandardScaler

# Initialize scalers
sc_X = StandardScaler()
sc_y = StandardScaler()

# Fit and transform the training data
X_train = sc_X.fit_transform(X_train)
y_train = sc_y.fit_transform(y_train.values.reshape(-1, 1)).ravel()

# Transform the test data
X_test = sc_X.transform(X_test)
y_test = sc_y.transform(y_test.values.reshape(-1, 1)).ravel()

2. Hyperparameter Tuning:

Utilize techniques like Grid Search with Cross-Validation to find the optimal hyperparameters.

from sklearn.model_selection import GridSearchCV

# Define the parameter grid
param_grid = {
    'C': [0.1, 1, 10, 100],
    'epsilon': [0.01, 0.1, 0.2, 0.5],
    'kernel': ['linear', 'rbf', 'poly']
}

# Initialize Grid Search
grid_search = GridSearchCV(SVR(), param_grid, cv=5, scoring='r2', n_jobs=-1)

# Fit Grid Search
grid_search.fit(X_train, y_train)

# Best parameters
print(grid_search.best_params_)

3. Alternative Models:

Given the limitations observed, exploring other regression models like Random Forests or XGBoost might yield better results.

Conclusion

Support Vector Regression is a potent tool in the machine learning arsenal, especially for scenarios demanding robustness against outliers and handling high-dimensional data. However, its efficacy is contingent upon meticulous preprocessing and hyperparameter tuning. In practical applications, as demonstrated with the insurance dataset, SVR may underperform compared to ensemble methods like Random Forests or Gradient Boosting, which often provide superior accuracy in regression tasks.

For practitioners aiming to leverage SVR, it's imperative to:

  • Scale Features Appropriately: Ensuring all features contribute equally to the model.
  • Optimize Hyperparameters: Employing techniques like Grid Search to fine-tune model settings.
  • Evaluate Alternative Models: Sometimes, other algorithms might be inherently better suited for the task at hand.

By understanding the strengths and limitations of SVR, data scientists can make informed decisions, ensuring the deployment of the most effective regression models for their specific use cases.

Additional Resources

  • Scikit-learn SVR Documentation
  • Understanding Support Vector Machines
  • Kaggle Insurance Dataset

FAQs

1. When should I use Support Vector Regression over other regression models?

SVR is particularly useful when dealing with high-dimensional datasets and when the relationship between features and the target variable is non-linear. It's also beneficial when your dataset contains outliers, as SVR is robust against them.

2. Can SVR handle large datasets efficiently?

SVR can be computationally intensive with large datasets, leading to longer training times. For sizable datasets, ensemble methods like Random Forests or Gradient Boosting might be more efficient and provide better performance.

3. How does kernel choice affect SVR performance?

The kernel function determines the transformation of data into a higher-dimensional space, enabling the model to capture non-linear relationships. Common kernels include linear, polynomial (poly), and radial basis function (rbf). The choice of kernel and its parameters (like gamma in rbf) significantly influence SVR's performance.

4. Is feature scaling mandatory for SVR?

Yes, feature scaling is crucial for SVR. Without scaling, features with larger magnitudes can dominate the objective function, leading to suboptimal performance. Scaling ensures that all features contribute equally to the model.

5. What are the alternatives to SVR for regression tasks?

Popular alternatives include Linear Regression, Decision Trees, Random Forests, Gradient Boosting Machines (e.g., XGBoost), and Neural Networks. Each has its strengths and is suited to different types of regression problems.

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.