📜  scikit learn svm - Python (1)

📅  最后修改于: 2023-12-03 15:05:05.319000             🧑  作者: Mango

Scikit Learn SVM - Python

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.

Installation

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
Importing the Library

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.

Dataset

For this example, we will use the famous iris flowers dataset. This dataset contains 150 samples, and each sample contains the following features:

  • Sepal length (in cm)
  • Sepal width (in cm)
  • Petal length (in cm)
  • Petal width (in cm)

There are three classes in the target variable:

  • Iris Setosa
  • Iris Versicolour
  • Iris Virginica

We will download the dataset using Scikit Learn's load_iris function.

from sklearn.datasets import load_iris
iris = load_iris()
Preprocessing the Dataset

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)
Training the SVM Model

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.

Predicting

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.

Conclusion

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.