Practical Application of Scikit-learnSVM in Python
Preparation work:
1. Install Python. You can download and install the latest version of Python from the official website.
2. Install Scikit learn. You can use the pip command (pip install scikit learn) or use Anaconda for installation (conda install scikit learn).
3. Download the dataset. This sample uses the Iris dataset, which can be downloaded from the following website: https://archive.ics.uci.edu/ml/datasets/iris
Dependent class libraries:
1. sklearn. svm. SVC: SVM algorithm implementation class in Scikit learn.
2. sklearn.datasets.load_ Iris: Iris dataset loading function in Scikit learn.
Sample data description:
The Iris dataset contains 150 samples, each with 4 features (sepal length, sepal width, petal length, and petal width) and their corresponding category labels (Setosa, Versicolor, Virginia).
The Python code implementation is as follows:
python
from sklearn import svm
from sklearn.datasets import load_iris
#Import Dataset
iris = load_iris()
#Create an SVM classifier object
clf = svm.SVC()
#Training SVM models using datasets
clf.fit(iris.data, iris.target)
#Predict the category of new samples
new_sample = [[5.0, 3.6, 1.3, 0.25]]
predicted_class = clf.predict(new_sample)
#Print prediction results
print("Predicted class:", predicted_class)
Program output result:
Predicted class: [0]
Summary:
This example demonstrates how to use the SVM algorithm in Scikit learn to classify iris datasets. Firstly, we need to install Python and Scikit learn libraries and download the dataset. Then, we import the necessary class libraries and datasets, and create an SVM classifier object. Next, use the dataset to train the SVM model and use the trained model to predict the category of new samples. Finally, print the predicted results.
Through this example, we can see that the SVM algorithm using Scikit learn is very simple and flexible. With the appropriate environment setup and preparation work, we can quickly use the SVM algorithm in Scikit learn to complete practical tasks.