Support Vector Machine (Support Vector Machine, SVM) extends from linear classification to kernel function


Support Vector Machine (SVM for short) is a powerful and widely used supervised learning algorithm for classification and regression tasks. This article will deeply analyze the principle of SVM, from linear classification to kernel function expansion

1. Linear Classification with Maximum Margin

The core idea of ​​SVM is to find an optimal hyperplane in the feature space to separate samples of different categories. For linearly separable cases, SVM achieves classification by maximizing the separation between the classification boundary (hyperplane) and the samples of the two classes. This interval is called the maximum interval, which makes SVM have better robustness and generalization ability

2. Support Vectors

Support vectors are those sample points closest to the maximum margin hyperplane. These sample points play an important role in defining the hyperplane and decision boundary. The support vector determines the structure and performance of the SVM model

3. Soft interval and penalty factor

In practical applications, there are very few linearly separable datasets. In order to deal with the case of linear inseparability, the concept of soft margin is introduced. Soft margins allow some sample points to be on the wrong side of the hyperplane. In order to balance the robustness and generalization ability of the classification boundary, a penalty factor C is introduced. The value of C determines the tolerance for misclassified samples. A smaller C will produce a looser decision boundary, while a larger C will produce a stricter decision boundary.

4. Kernel function extension

When the data set is not linearly separable, linear SVM cannot effectively classify. To solve this problem, SVM introduces the concept of kernel function. The kernel function can map the samples in the low-dimensional feature space to the high-dimensional feature space, so that the original linearly inseparable problem becomes linearly separable. Commonly used kernel functions include linear kernel, polynomial kernel, Gaussian kernel, etc.

5. Advantages and disadvantages of SVM

Advantages of SVMs:

  • Can handle high-dimensional feature space and data sets with a large number of samples
  • It has good robustness and generalization ability when dealing with linearly separable problems
  • Supports the extension of different kernel functions and can handle nonlinear problems

Disadvantages of SVMs:

  • For large-scale datasets and high-dimensional datasets, the training time is longer
  • Sensitive to choosing an appropriate kernel function and parameter tuning
  • When dealing with noisy data sets, it is easy to overfit

6. SVM code example

from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# 加载数据集
iris = load_iris()
X = iris.data
y = iris.target

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 创建SVM模型
model = SVC()

# 训练模型
model.fit(X_train, y_train)

# 预测结果
y_pred = model.predict(X_test)

# 评估模型
accuracy = accuracy_score(y_test, y_pred)
print("准确率:", accuracy)

In the code, a classic iris data set (Iris) is first loaded, and the data set is divided into a training set and a test set. Then we create a SVM classification model and use the training set for training. Use the test set to make predictions and calculate the accuracy to evaluate the performance of the model

Guess you like

Origin blog.csdn.net/weixin_43749805/article/details/131312548
Recommended