![]() |
Support Vector Machines (SVMs) are powerful supervised learning models used for classification and regression tasks. A key factor behind their popularity is their ability to handle both linear and non-linear data effectively. In this article, we will explore visualizing SVMs using Python and popular libraries like scikit-learn and Matplotlib. Support Vector Machine (SVMs)Support Vector Machines work by finding the optimal hyperplane that best separates the classes in the feature space. The hyperplane is chosen to maximize the margin, which is the distance between the hyperplane and the nearest data points from each class, known as support vectors. This hyperplane is determined by solving an optimization problem, which aims to minimize the classification error while maximizing the margin. SVMs can be used for both linear and non-linear classification tasks through the use of different kernel functions like linear, polynomial, radial basis function (RBF), and sigmoid kernels. These kernels allow SVMs to handle non-linear decision boundaries by mapping the original feature space into a higher-dimensional space where the classes become separable. Visualizing Linear SVMsLet’s start by visualizing a simple linear SVM using Iris dataset. We will generate the data and train the SVM model using Scikit-Learn. Then, we’ll plot the decision boundary and support vectors to understand how the model separates the classes. Importing Necessary Libraries and load the DatasetThis dataset contains measurements of sepal and petal dimensions for three species of iris flowers. Here, only the first two features (sepal length and sepal width) are retained for visualization purposes.
Training SVM with Linear KernelAn SVM model with a linear kernel is trained on the Iris dataset. A linear kernel is suitable for linearly separable data, aiming to find the best hyperplane that separates different classes.
Creating Meshgrid for Decision BoundaryA meshgrid is created to cover the feature space. This allows for the generation of points for visualization purposes. The decision boundaries of the SVM models will be plotted on this meshgrid.
Plotting Decision Boundary of Linear SVMThe decision boundary of the SVM with a linear kernel is plotted. This is achieved by predicting the class labels for all points on the meshgrid using the predict method. The decision boundary is then visualized using filled contour plots (plt.contourf) and original data points are overlaid on the plot for reference.
Output: ![]() linear SVM visualisation In the above visualization, linear SVM has classified the data points in a linear way. Even though the accuracy is not that great, we can clearly see that the red section has a ton of misclassified datapoints, but that’s where non-linear svm will come into picture. Visualizing Non-linear SVMsSVMs can also handle non-linear decision boundaries by using kernel functions. Let’s visualize a non-linear SVM using the same Iris dataset with a polynomial kernel. Understanding the Impact of Gamma in RBF KernelsBefore visualizing non-linear SVMs, let’s explore the influence of the gamma parameter in the RBF kernel. The gamma parameter significantly impacts the RBF kernel’s behavior in SVMs. It essentially determines the influence of individual data points on the decision boundary.
Defining Gamma ValuesA list named gamma_values is defined, containing different values of the gamma hyperparameters, controlling the influence of a single training example.
Plotting Decision Boundaries for Each Gamma Value
Output:![]() Non linear SVM visualisation with different gamma values In the above visualization, clearly gamma values impacts a lot on the accuracy and complexity of the model.
ConclusionIn conclusion, Support Vector Machines (SVMs) are powerful models for both linear and non-linear classification tasks. Visualizing SVMs, especially with different kernels and hyperparameters, provides valuable insights into their behavior and performance. |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |