Home > Backend Development > Python Tutorial > How to use SVM for classification in Python?

How to use SVM for classification in Python?

WBOY
Release: 2023-06-03 15:51:18
Original
2087 people have browsed it

SVM is a commonly used classification algorithm, which is widely used in the fields of machine learning and data mining. In Python, the implementation of SVM is very convenient and can be completed by using relevant libraries.

This article will introduce how to use SVM for classification in Python, including data preprocessing, model training and parameter tuning.

1. Data preprocessing

Before using SVM for classification, we need to preprocess the data to ensure that the data meets the requirements of the SVM algorithm. Usually, data preprocessing includes the following aspects:

  1. Data cleaning: exclude some useless or abnormal data to avoid interference with SVM classification.
  2. Data normalization: Scale the data according to a certain ratio to ensure that the numerical range of the data is the same.
  3. Feature selection: When there is too much data, select the most informative features to improve the classification effect.

2. Model training

After data preprocessing, we can start model training. In Python, we can use SVM-related libraries for model training.

  1. Import library

Before training the model, we need to import the relevant libraries:

import numpy as np
from sklearn. svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

  1. Data loading

Next, we need to load the data and proceed Division of training set and test set:

data = np.loadtxt('data.txt', delimiter=',')
X = data[:, :-1]
y = data[:, -1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

where data.txt is the data file, we can use loadtxt function to load. The train_test_split function is used to randomly divide the data into a training set and a test set, and the test_size parameter specifies the proportion of the test set.

  1. Model training

Next, we can start model training:

clf = SVC(C=1.0, kernel='rbf' , gamma='auto')
clf.fit(X_train, y_train)

Among them, the C parameter is the regularization coefficient, the kernel parameter specifies which kernel function to use, and the gamma parameter is used to control the kernel function. influence level. In this example, we use the RBF kernel function.

  1. Model evaluation

After training is completed, we need to perform model evaluation:

y_pred = clf.predict(X_test)
acc = accuracy_score (y_test, y_pred)
print('Accuracy:', acc)

Among them, the accuracy_score function is used to calculate the accuracy of the model.

3. Parameter tuning

After model training, we can perform parameter tuning to further improve the classification effect of the model. In SVM, commonly used parameter tuning methods include grid search and cross-validation.

  1. Grid search

Grid search is a brute force search method that searches for the optimal parameter combination by traversing all possible parameter combinations. In Python, we can use the GridSearchCV function to implement grid search.

from sklearn.model_selection import GridSearchCV

Define parameter range

param_grid = {'C': [0.1, 1.0, 10.0],

          'kernel': ['linear', 'rbf'],
          'gamma': ['auto', 0.1, 0.01]}
Copy after login

Carry out Grid search

gs = GridSearchCV(SVC(), param_grid, cv=5)
gs.fit(X_train, y_train)

Output optimal parameters

print('Best:', gs.best_params_)

Among them, param_grid specifies the range of parameters, and the cv parameter specifies the number of cross-validation. After the execution is completed, we can output the optimal parameter combination.

  1. Cross-validation

Cross-validation is a method of validating model performance through repeated sampling. In Python, we can use the cross_val_score function to implement cross validation.

from sklearn.model_selection import cross_val_score

Perform cross validation

scores = cross_val_score(clf, X_train, y_train, cv=5)

Output cross validation Result

print('CV scores:', scores)

Among them, the cv parameter specifies the number of cross-validation. After the execution is completed, we can output the results of the cross-validation.

4. Summary

This article introduces how to use SVM for classification in Python, including data preprocessing, model training and parameter tuning. Classification problems can be effectively solved using SVM, and related libraries in Python also provide convenient tools for implementing SVM. I hope this article can be helpful to readers when using SVM for classification.

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template