---
title: Support Vector Machines
date: 2025-06-05T05:23:34Z
modified: 2026-07-22T05:29:46Z
permalink: "https://www.micheledpierri.com/machine-learning/support-vector-machines/"
type: page
status: publish
excerpt: ""
wpid: 1343
featured_image: "https://www.micheledpierri.com/wp-content/uploads/2026/07/ml_svm_.png"
featured_image_alt: A lone farmer travels along a dirt path dividing rows of purple lavender and golden sunflowers beneath a warm, hazy sky.
timestamp: 2026-07-22T05:29:46Z
tags: []
---

## Introduction

Support Vector Machines are supervised learning algorithms that primarily handle classification problems, though they can also perform regression tasks. They excel at finding boundaries between classes and are especially effective with high-dimensional data.

## Definition and Mathematical Foundations

### Definition

IIn binary classification, data is split into two classes. Support Vector Machines find the best boundary. This boundary is a line in 2D, a plane in 3D, or a hyperplane in higher dimensions.

This boundary is positioned to maximize the distance (called the margin) between points of different classes. The data points that lie exactly on the margin are known as “support vectors.”

![Optimal hyperplane and margin in a linear SVM](https://www.micheledpierri.com/wp-content/uploads/2025/05/svm_graph-1024x768.png)

### Mathematical Foundations

Given a set of points {(xi, yi)}, where x represents the feature vector and y represents the label, the hyperplane is defined as:

![w \cdot x + b = 0](https://www.micheledpierri.com/wp-content/ql-cache/quicklatex.com-01e1fbfa79c4afe8f3c973c616dea208_l3.svg "Rendered by QuickLaTeX.com")

 ![w](https://www.micheledpierri.com/wp-content/ql-cache/quicklatex.com-ce90b63446bb8d078bb10b9b61e2a0a1_l3.svg "Rendered by QuickLaTeX.com") is the weight vector, – ![x](https://www.micheledpierri.com/wp-content/ql-cache/quicklatex.com-9ac9f94eff3ef1c933708cbebc25d094_l3.svg "Rendered by QuickLaTeX.com") is the feature vector, – ![b](https://www.micheledpierri.com/wp-content/ql-cache/quicklatex.com-3571a76aa17060a5447498667881fa61_l3.svg "Rendered by QuickLaTeX.com") is the bias.

The algorithm chooses a hyperplane that maximizes the margin between classes.

To identify the optimal margin, we need to solve:

![\min_{w, b} \frac{1}{2} ||w||^2](https://www.micheledpierri.com/wp-content/ql-cache/quicklatex.com-63593ba8094e6fec063e51d257b52e2e_l3.svg "Rendered by QuickLaTeX.com")

When classes aren’t perfectly separable, we introduce slack variables to handle violations:

  ![\[ \min_{w, b, \xi} \frac{1}{2} ||w||^2 + C \sum_{i=1}^{N} \xi_i \]](https://www.micheledpierri.com/wp-content/ql-cache/quicklatex.com-90f726cda80b5a36b1d18aefd3a9a7cd_l3.svg "Rendered by QuickLaTeX.com")

 where   ![\[ y_i (w \cdot x_i + b) \geq 1 - \xi_i, \quad \forall i, \quad \xi_i \geq 0 \]](https://www.micheledpierri.com/wp-content/ql-cache/quicklatex.com-fefb435477718400b2c9f22d662331b0_l3.svg "Rendered by QuickLaTeX.com")

Here, C is a regularization parameter that we’ll explore further.

For data that cannot be separated linearly, kernel functions offer a solution by mapping the data into a higher-dimensional space where linear separation becomes possible.

Common kernel functions include:

  ![\begin{align*} K_{\text{linear}}(x, x') &= x \cdot x' \\ K_{\text{polynomial}}(x, x') &= (x \cdot x' + c)^d \\ K_{\text{Gaussian}}(x, x') &= e^{-\gamma ||x - x'||^2} \\ K_{\text{sigmoid}}(x, x') &= \tanh(\alpha x \cdot x' + c) \end{align*}](https://www.micheledpierri.com/wp-content/ql-cache/quicklatex.com-246a4d491eb58efd03a951a0a68a03bd_l3.svg "Rendered by QuickLaTeX.com")

### The C hyperparameter

The parameter C is a hyperparameter that balances between accurate classification of training data and effective generalization to new data.

When C is set high, the model aims for perfect classification of all training points, but this increases the risk of overfitting. Conversely, a low C value allows some classification errors while widening the margin, which helps prevent overfitting.

When working with limited data or noisy datasets, it’s better to use a lower C value. However, when dealing with abundant, clearly separable data, a higher C value is more appropriate.

In the graphs, we deliberately set extreme values for C (both very small and very large) to demonstrate their effects on the margins. When C is small, the margins are wide, allowing for some misclassifications. When C is large, the margins become narrow and prevent any misclassifications.

![SVM with large C (narrow margine and fewer errors)](https://www.micheledpierri.com/wp-content/uploads/2025/05/svm_C_effect-1024x731.png)

![SVM with smaller C (large margine and more errors)](https://www.micheledpierri.com/wp-content/uploads/2025/05/svm_C_effect_2-1024x731.png)## Medical Application

The medical literature contains numerous studies demonstrating Support Vector Machines’s successful applications and significant role in healthcare:

- cancer classification and diagnosis (Huang S, Cai N, Pacheco PP, Narrandes S, Wang Y, Xu W. Applications of Support Vector Machine (SVM) Learning in Cancer Genomics. Cancer Genomics Proteomics. 2018 Jan-Feb;15(1):41-51. doi: 10.21873/cgp.20063IF: 2.6 Q2 . PMID: 29275361; PMCID: PMC5822181.)
- classification and medication adherence (Son YJ, Kim HG, Kim EH, Choi S, Lee SK. Application of support vector machine for prediction of medication adherence in heart failure patients. Healthc Inform Res. 2010 Dec;16(4):253-9. doi: 10.4258/hir.2010.16.4.253. Epub 2010 Dec 31. PMID: 21818444; PMCID: PMC3092139.)
- use for diabetes and pre-diabetes diagnosis (Yu, W., Liu, T., Valdez, R. _et al._ Application of support vector machine modeling for prediction of common diseases: the case of diabetes and pre-diabetes. _BMC Med Inform Decis Mak_ **10**, 16 (2010). [https://doi.org/10.1186/1472-6947-10-16](https://doi.org/10.1186/1472-6947-10-16))

## Other Applications

Support Vector Machines have found extensive applications across numerous domains, demonstrating their versatility and effectiveness.

In computer vision, they are key for tasks like image classification, segmentation, and facial recognition.

In natural language processing, SVMs excel at text classification tasks, enabling the categorization of documents, filtering spam, and analyzing sentiment.

The algorithm processes audio for speech recognition and speaker identification.

In the financial sector, SVMs play a crucial role in fraud detection systems by analyzing transaction patterns to identify suspicious banking and credit card activities.

The industrial sector has also embraced SVMs for quality control and maintenance, implementing them in fault detection systems and continuous machine monitoring to predict and prevent equipment failures.

## When to Use Support Vector Machines

SVMs suit cases with limited data that has high dimensionality. They handle both linear and non-linear data effectively (thanks to kernel functions), and they perform well when you need a model that stays robust and resists outliers.

## Limitations of using Support Vector Machines

SVMs perform poorly with very large datasets (over 100,000 samples) and when processing irrelevant or low-quality features. With large datasets in particular, they demand substantial computational resources.

## Application in Python

We will employ Support Vector Machine (SVM) classification for breast tumor prediction by using a comprehensive dataset that is readily available within the scikit-learn library.

This dataset contains a rich collection of medical imaging data, with 30 distinct features carefully extracted from digital images of tumor masses. These features include various physical characteristics such as the radius of the mass, its perimeter measurements, detailed texture analysis, and several other relevant metrics that characterize the tumor’s properties. Each case in the dataset includes a binary classification indicating whether the tumor is benign or malignant, providing a clear target for our predictive modeling efforts.

After loading the dataset into a pandas DataFrame, we split the data into features (X) and target (y), then divided them into training (80%) and test (20%) sets. We normalized the features using StandardScaler and implemented an SVM algorithm with an “rbf” kernel—the most commonly used kernel type, particularly suitable for non-linear data. We set the hyperparameter C to 1.

After training the model, we evaluated its performance using test data metrics and generated a confusion matrix with graphical visualization.

\# import necessary libraries since execution state was reset import numpy as np import matplotlib.pyplot as plt import seaborn as sns import pandas as pd from sklearn.datasets import load\_breast\_cancer from sklearn.model\_selection import train\_test\_split from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC from sklearn.metrics import accuracy\_score, confusion\_matrix, classification\_report # Load the Breast Cancer dataset from scikit-learn cancer = load\_breast\_cancer() # Convert to a pandas DataFrame for better visualization df = pd.DataFrame(cancer.data, columns=cancer.feature\_names) df\[‘target’\] = cancer.target # Step 1: Data Preprocessing (Feature Scaling) X = df.drop(columns=\[‘target’\]) # Features (independent variables) y = df\[‘target’\] # Target variable (Malignant = 0, Benign = 1) # Split dataset into training (80%) and testing (20%) sets X\_train, X\_test, y\_train, y\_test = train\_test\_split(X, y, test\_size=0.2, random\_state=42, stratify=y) # Normalize the features using StandardScaler scaler = StandardScaler() X\_train\_scaled = scaler.fit\_transform(X\_train) X\_test\_scaled = scaler.transform(X\_test) # Step 2: Train the SVM Model svm\_model = SVC(kernel=”rbf”, C=1.0, gamma=”scale”) # Using an RBF Kernel for non-linearity svm\_model.fit(X\_train\_scaled, y\_train) # Step 3: Predictions and Model Evaluation y\_pred = svm\_model.predict(X\_test\_scaled) # Compute accuracy accuracy = accuracy\_score(y\_test, y\_pred) print(f”Model Accuracy: {accuracy:.2f}”) # Generate confusion matrix and classification report conf\_matrix = confusion\_matrix(y\_test, y\_pred) class\_report = classification\_report(y\_test, y\_pred, target\_names=\[“Malignant”, “Benign”\]) # Step 4: Visualizing Confusion Matrix plt.figure(figsize=(6,5)) sns.heatmap(conf\_matrix, annot=True, fmt=’d’, cmap=’Blues’, xticklabels=\[“Malignant”, “Benign”\], yticklabels=\[“Malignant”, “Benign”\]) plt.xlabel(“Predicted Label”) plt.ylabel(“True Label”) plt.title(“Confusion Matrix for SVM Breast Cancer Classification”) plt.show() # Display classification report print(“Classification Report:\\n”, class\_report) ```
<span class="line"><span style="color: #6272A4"># import necessary libraries since execution state was reset</span></span>
<span class="line"><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> numpy </span><span style="color: #FF79C6">as</span><span style="color: #F8F8F2"> np</span></span>
<span class="line"><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> matplotlib.pyplot </span><span style="color: #FF79C6">as</span><span style="color: #F8F8F2"> plt</span></span>
<span class="line"><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> seaborn </span><span style="color: #FF79C6">as</span><span style="color: #F8F8F2"> sns</span></span>
<span class="line"><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> pandas </span><span style="color: #FF79C6">as</span><span style="color: #F8F8F2"> pd</span></span>
<span class="line"><span style="color: #FF79C6">from</span><span style="color: #F8F8F2"> sklearn.datasets </span><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> load_breast_cancer</span></span>
<span class="line"><span style="color: #FF79C6">from</span><span style="color: #F8F8F2"> sklearn.model_selection </span><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> train_test_split</span></span>
<span class="line"><span style="color: #FF79C6">from</span><span style="color: #F8F8F2"> sklearn.preprocessing </span><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> StandardScaler</span></span>
<span class="line"><span style="color: #FF79C6">from</span><span style="color: #F8F8F2"> sklearn.svm </span><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> </span><span style="color: #BD93F9">SVC</span></span>
<span class="line"><span style="color: #FF79C6">from</span><span style="color: #F8F8F2"> sklearn.metrics </span><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> accuracy_score, confusion_matrix, classification_report</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6272A4"># Load the Breast Cancer dataset from scikit-learn</span></span>
<span class="line"><span style="color: #F8F8F2">cancer </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> load_breast_cancer()</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6272A4"># Convert to a pandas DataFrame for better visualization</span></span>
<span class="line"><span style="color: #F8F8F2">df </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> pd.DataFrame(cancer.data, </span><span style="color: #FFB86C; font-style: italic">columns</span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2">cancer.feature_names)</span></span>
<span class="line"><span style="color: #F8F8F2">df[</span><span style="color: #E9F284">'</span><span style="color: #F1FA8C">target</span><span style="color: #E9F284">'</span><span style="color: #F8F8F2">] </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> cancer.target</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6272A4"># Step 1: Data Preprocessing (Feature Scaling)</span></span>
<span class="line"><span style="color: #F8F8F2">X </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> df.drop(</span><span style="color: #FFB86C; font-style: italic">columns</span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2">[</span><span style="color: #E9F284">'</span><span style="color: #F1FA8C">target</span><span style="color: #E9F284">'</span><span style="color: #F8F8F2">])  </span><span style="color: #6272A4"># Features (independent variables)</span></span>
<span class="line"><span style="color: #F8F8F2">y </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> df[</span><span style="color: #E9F284">'</span><span style="color: #F1FA8C">target</span><span style="color: #E9F284">'</span><span style="color: #F8F8F2">]  </span><span style="color: #6272A4"># Target variable (Malignant = 0, Benign = 1)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6272A4"># Split dataset into training (80%) and testing (20%) sets</span></span>
<span class="line"><span style="color: #F8F8F2">X_train, X_test, y_train, y_test </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> train_test_split(X, y, </span><span style="color: #FFB86C; font-style: italic">test_size</span><span style="color: #FF79C6">=</span><span style="color: #BD93F9">0.2</span><span style="color: #F8F8F2">, </span><span style="color: #FFB86C; font-style: italic">random_state</span><span style="color: #FF79C6">=</span><span style="color: #BD93F9">42</span><span style="color: #F8F8F2">, </span><span style="color: #FFB86C; font-style: italic">stratify</span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2">y)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6272A4"># Normalize the features using StandardScaler</span></span>
<span class="line"><span style="color: #F8F8F2">scaler </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> StandardScaler()</span></span>
<span class="line"><span style="color: #F8F8F2">X_train_scaled </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> scaler.fit_transform(X_train)</span></span>
<span class="line"><span style="color: #F8F8F2">X_test_scaled </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> scaler.transform(X_test)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6272A4"># Step 2: Train the SVM Model</span></span>
<span class="line"><span style="color: #F8F8F2">svm_model </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> SVC(</span><span style="color: #FFB86C; font-style: italic">kernel</span><span style="color: #FF79C6">=</span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">rbf</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">, </span><span style="color: #FFB86C; font-style: italic">C</span><span style="color: #FF79C6">=</span><span style="color: #BD93F9">1.0</span><span style="color: #F8F8F2">, </span><span style="color: #FFB86C; font-style: italic">gamma</span><span style="color: #FF79C6">=</span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">scale</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">)  </span><span style="color: #6272A4"># Using an RBF Kernel for non-linearity</span></span>
<span class="line"><span style="color: #F8F8F2">svm_model.fit(X_train_scaled, y_train)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6272A4"># Step 3: Predictions and Model Evaluation</span></span>
<span class="line"><span style="color: #F8F8F2">y_pred </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> svm_model.predict(X_test_scaled)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6272A4"># Compute accuracy</span></span>
<span class="line"><span style="color: #F8F8F2">accuracy </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> accuracy_score(y_test, y_pred)</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(</span><span style="color: #FF79C6">f</span><span style="color: #F1FA8C">"Model Accuracy: </span><span style="color: #BD93F9">{</span><span style="color: #F8F8F2">accuracy</span><span style="color: #FF79C6">:.2f</span><span style="color: #BD93F9">}</span><span style="color: #F1FA8C">"</span><span style="color: #F8F8F2">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6272A4"># Generate confusion matrix and classification report</span></span>
<span class="line"><span style="color: #F8F8F2">conf_matrix </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> confusion_matrix(y_test, y_pred)</span></span>
<span class="line"><span style="color: #F8F8F2">class_report </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> classification_report(y_test, y_pred, </span><span style="color: #FFB86C; font-style: italic">target_names</span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2">[</span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">Malignant</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">, </span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">Benign</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">])</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6272A4"># Step 4: Visualizing Confusion Matrix</span></span>
<span class="line"><span style="color: #F8F8F2">plt.figure(</span><span style="color: #FFB86C; font-style: italic">figsize</span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2">(</span><span style="color: #BD93F9">6</span><span style="color: #F8F8F2">,</span><span style="color: #BD93F9">5</span><span style="color: #F8F8F2">))</span></span>
<span class="line"><span style="color: #F8F8F2">sns.heatmap(conf_matrix, </span><span style="color: #FFB86C; font-style: italic">annot</span><span style="color: #FF79C6">=</span><span style="color: #BD93F9">True</span><span style="color: #F8F8F2">, </span><span style="color: #FFB86C; font-style: italic">fmt</span><span style="color: #FF79C6">=</span><span style="color: #E9F284">'</span><span style="color: #F1FA8C">d</span><span style="color: #E9F284">'</span><span style="color: #F8F8F2">, </span><span style="color: #FFB86C; font-style: italic">cmap</span><span style="color: #FF79C6">=</span><span style="color: #E9F284">'</span><span style="color: #F1FA8C">Blues</span><span style="color: #E9F284">'</span><span style="color: #F8F8F2">, </span><span style="color: #FFB86C; font-style: italic">xticklabels</span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2">[</span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">Malignant</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">, </span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">Benign</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">], </span><span style="color: #FFB86C; font-style: italic">yticklabels</span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2">[</span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">Malignant</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">, </span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">Benign</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">])</span></span>
<span class="line"><span style="color: #F8F8F2">plt.xlabel(</span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">Predicted Label</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">plt.ylabel(</span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">True Label</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">plt.title(</span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">Confusion Matrix for SVM Breast Cancer Classification</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">plt.show()</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6272A4"># Display classification report</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(</span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">Classification Report:</span><span style="color: #FF79C6">\n</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">, class_report)</span></span>
<span class="line"></span>
```

The model achieves excellent performance, separating benign and malignant tumors with 98% accuracy. The visualization of the decision boundary and margins (created using PCA dimensionality reduction) shows that despite some overlap between classes, the model effectively differentiates between the two tumor types.

![Confusion matrix for SVM breast cancer classification](https://www.micheledpierri.com/wp-content/uploads/2025/05/svm_confusion_matrix-1024x853.png)

## Conclusion

Support Vector Machines demonstrate effectiveness in real-world scenarios where limited but feature-rich datasets exist, making them particularly valuable for specialized classification tasks. They handle high-dimensional data while maintaining good generalization, which makes them an excellent choice for many practical applications. However, their computational complexity limits their ability to process large-scale datasets. Despite their kernel capabilities, they may struggle with extremely complex non-linear relationships that require more sophisticated deep learning approaches.