Support Vector Machine Tutorial for Beginners
Support Vector Machine Tutorial for Beginners, Are you the one who is looking forward to knowing about What is Support Vector Machine?? Or the one who is looking forward to knowing How does SVM work? and implementing svm in python or Are you dreaming to become to certified Pro Machine Learning Engineer or Data Scientist, then stop just dreaming, get your Data Science certification course with Machine Learning from India’s Leading Data Science training institute. Support Vector Machine is another simple algorithm that every machine learning expert uses. It is highly preferred by many experts because it provides accurate results with less computation power and is used for both classification and regression problems. In this blog, we will learn How does SVM work in Machine Learning and implementing svm in python. Do you want to know What is Support Vector Machine, So follow the below mentioned support vector machine tutorial for beginners from Prwatech and take advanced Data Science training with Machine Learning like a pro from today itself under 10+ Years of hands-on experienced Professionals.Introduction to Support Vector Machine
Certainly! Here's your content with improved coherence and the addition of transition words:Support Vector Machine (SVM) Support Vector Machine (SVM) stands as a supervised machine learning algorithm, functioning akin to a discriminative classifier defined by a separating hyperplane. Essentially, for labeled training data, the algorithm constructs the optimal hyperplane, enabling the categorization of new inputs. In two dimensions, this hyperplane manifests as a line that partitions the space into two distinct regions. Support Vectors Support Vectors denote the coordinates of unique observations essential for defining the separating hyperplane.

Criterion 1
The following image shows three hyperplanes trying to separate out two classes.
Criterion 2
Here all hyperplanes are separating two classes, now the question is how to identify the correct one?
Criterion 3

Criterion 4:

Criterion 5:

z = x^2+y^2

f(x) = B(0) + sum(ai * (x,Xi))
The equation calculates the inner products of a new input vector x with all support vectors in training data. B0 and ai coefficients for, each input, must be assessed from the training data by learning algorithm. The polynomial kernel can be written asK(x,xi) = 1 + sum(x * Xi)^d
And exponential can be written asK(x,xi) = exp(-gamma * sum((x — xi²))
Understanding Regularization in SVM
The regularization parameter, often denoted as parameter 'C' in the scikit-learn library, plays a crucial role in Support Vector Machine (SVM) optimization. It determines the extent to which misclassification is tolerated during the SVM training process. When 'C' takes on greater values, the optimizer tends to select a smaller-margin hyperplane if it results in better classification of all training points. Conversely, when 'C' is set to smaller values, the optimizer prioritizes selecting a larger-margin separating hyperplane, even if it leads to misclassification of some points. This balancing act between margin size and misclassification is pivotal in ensuring optimal SVM performance and generalization ability.

Gamma
The gamma parameter defines how far the impact of a single training point reaches, with low values (meaning ‘far’) and high values (meaning ‘close’). In other words, with low gamma, points far away from reasonable separation line are taken in the calculation for the separation line. And in case of higher gamma value, the points close to the plausible line are taken in the calculation.Margin
Understanding Margin in Support Vector Machine (SVM) A margin in Support Vector Machine (SVM) refers to the separation of the decision boundary (hyperplane) from the nearest points of the classes it is intended to separate. In essence, a good margin entails a substantial separation for both classes, allowing data points to reside comfortably within their respective classes without encroaching on the boundaries of other classes. This spacious margin not only facilitates clearer class separation but also enhances the model's robustness and generalization ability. Let's delve into an example to illustrate this concept.

Initializing and importing required libraries
import NumPy as np import matplotlib.pyplot as plt from sklearn import svm, datasetsLet’s take the iris data set and load it. We will check features in iris and we will select two features from them to avoid complex visualization.
iris = datasets.load_iris() print(iris.feature_names) Xin = iris.data[:, :2] # we will take ‘sepal width’ and ‘sepal length’. yin = iris.target We will try to plot support vectors using linear kernel, polynomial kernel, and sigmoid kernel. For this we will keep C and gamma values constant.Let’s apply the linear kernel first:
C = 1.0 # Support Vector Machine regularization parameter svc = svm.SVC(kernel='linear, C=1,gamma='auto').fit(X, y) x_min, x_max = Xin[:, 0].min() - 1, Xin[:, 0].max() + 1 y_min, y_max = Xin[:, 1].min() - 1, Xin[:, 1].max() + 1 h = (x_max / x_min)/100 x1, y1 = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h)) plt.subplot(1, 1, 1) Z = svc.predict(np.c_[x1.ravel(), y1.ravel()]) Z = Z.reshape(x1.shape) plt.contourf(x1, y1, Z, cmap=plt.cm.Paired, alpha=0.8) plt.scatter(Xin[:, 0], Xin[:, 1], c=yin, cmap=plt.cm.Paired) plt.xlabel('Sepal length') plt.ylabel('Sepal width') plt.xlim(x1.min(), x1.max()) plt.title('SVC - linear kernel') plt.show()Output:

Let’s apply poly kernel:
svc = svm.SVC(kernel='poly', C=1,gamma='auto').fit(X, y)Output:

Let’s apply rbf kernel:
svc = svm.SVC(kernel='rbf', C=1,gamma='auto').fit(X, y)Output:

Now Let’s observe the effect of ‘gamma’ on classification. For that, we will keep the SVM kernel and C constant.
If we change values of gamma as 1,10,200 then we get the respective graph as follows:
Now, let's delve into the effect of the parameter 'C' on classification outcomes. In this analysis, we'll keep gamma and the SVM kernel constant to isolate the influence of 'C' on the classification process.
If we change values of C as 5,500,5000 then we get the respective graph as follows: