Home  >  Article  >  Backend Development  >  How to write SVM algorithm in Python?

How to write SVM algorithm in Python?

WBOY
WBOYOriginal
2023-09-21 12:06:11699browse

How to write SVM algorithm in Python?

How to write SVM algorithm in Python?

SVM (Support Vector Machine) is a commonly used classification and regression algorithm based on statistical learning theory and the principle of structural risk minimization. It has high accuracy and generalization ability, and is suitable for various data types. In this article, we will introduce in detail how to write the SVM algorithm using Python and provide specific code examples.

  1. Install Python and related libraries
    Before you start writing the SVM algorithm, you first need to make sure that Python and related machine learning libraries have been installed. It is recommended to use Anaconda as the integrated development environment for Python. It not only comes with a Python interpreter, but also includes many commonly used scientific computing and machine learning libraries. Install the scikit-learn library using the following command:
pip install scikit-learn
  1. Import the required libraries
    Import the required libraries, including scikit-learn, numpy, and matplotlib.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
  1. Loading data set
    To demonstrate the writing of the SVM algorithm, we will use the famous Iris data set. The Iris data set contains 150 iris flower samples, each sample has 4 features. We divided the dataset into two categories: Setosa and Versicolor, two varieties of iris flowers.
iris = datasets.load_iris()
X = iris.data[:, :2]  # 我们只使用前两个特征
y = iris.target
  1. Training model
    Use SVM to train the model, here we use the linear kernel function.
C = 1.0  # SVM正则化参数
svc = svm.SVC(kernel='linear', C=C).fit(X, y)
  1. Draw the decision boundary
    In order to better understand the classification effect of SVM, we can draw the decision boundary. First, we create a grid to sample the entire feature space.
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
h = (x_max / x_min)/100
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

Then, we use this grid as an input feature to predict and get the decision boundary.

Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

Finally, we use the matplotlib library to draw sample points and decision boundaries.

plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.show()
  1. Complete code example
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets

# 加载数据集
iris = datasets.load_iris()
X = iris.data[:, :2]
y = iris.target

# 训练模型
C = 1.0  # SVM正则化参数
svc = svm.SVC(kernel='linear', C=C).fit(X, y)

# 画出决策边界
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
h = (x_max / x_min)/100
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.show()

Summary:
Through the above steps, we successfully wrote the SVM algorithm using Python and demonstrated it through the Iris data set . Of course, this is just a simple application of the SVM algorithm. There are many ways to extend and improve SVM, such as using different kernel functions, adjusting the regularization parameter C, etc. I hope this article will help you learn and understand the SVM algorithm.

The above is the detailed content of How to write SVM algorithm in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn