📅  最后修改于: 2023-12-03 15:05:05.319000             🧑  作者: Mango
SVM (Support Vector Machines) is a powerful supervised learning technique for classification, regression, and outlier detection. Scikit Learn is a popular Python library that offers SVM implementation. This tutorial will cover the basics of Scikit Learn SVM and will guide you through a simple example.
Before getting started with SVM in Scikit Learn, you need to install Scikit Learn library. You can do this by running the following command:
!pip install scikit-learn
After installation, you can import the library in your Python code using the following command:
from sklearn import svm
This command imports the SVM module of Scikit Learn.
For this example, we will use the famous iris flowers dataset. This dataset contains 150 samples, and each sample contains the following features:
There are three classes in the target variable:
We will download the dataset using Scikit Learn's load_iris function.
from sklearn.datasets import load_iris
iris = load_iris()
Before training the SVM model, we need to preprocess the data. We will scale the features using Sklearn's StandardScaler.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaled_features = scaler.fit_transform(iris.data)
After preprocessing the dataset, we will train the SVM model using the SVM module of Scikit Learn.
clf = svm.SVC(kernel='linear', C=1)
clf.fit(scaled_features, iris.target)
In the code above, we are training an SVM model using a linear kernel and a regularization parameter of 1.
We can now use the trained SVM model to predict the class of a new sample. Here is an example of predicting the class of a new sample.
new_sample = [[5.1, 3.5, 1.4, 0.2]]
scaled_new_sample = scaler.transform(new_sample)
clf.predict(scaled_new_sample)
In the code above, we are predicting the class of a new sample with the following features: Sepal length = 5.1 cm, Sepal width = 3.5 cm, Petal length = 1.4 cm, Petal width = 0.2 cm.
In this tutorial, we learned the basics of Scikit Learn SVM and trained a simple SVM model to classify iris flowers. SVM is a powerful and widely used technique in the field of machine learning. Scikit Learn's implementation of SVM makes it easy to build and train SVM models in Python.