Simple test of support vector machine, R language can be realized by e1071 package, it is a basic algorithm for both R and python
Python is performed through SVM in the sklearn module
# Import the iris data set from sklearn
# Yes, iris is my favorite
from sklearn.datasets import load_iris
import sklearn
import numpy as np
from sklearn import model_selection
iris=load_iris()
iris.keys()
# Simple view of data
# Number of cases and features
n_samples,n_features=iris.data.shape
print("Number of sample:", n_samples)print("Number of feature",n_features)
Number of sample:150
Number of feature 4
# Split test and training sets
# Ratio is 0.6
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(iris.data, iris.target, random_state=1, train_size=0.6)
# Build the classifier
# kernel='rbf'The kernel function is Gaussian
# The larger the gamma, the easier to overfit
# decision_function_shape:one vs one,Multi-category, ovr: one vs rest,One category and the other
clf = svm.SVC(C=0.8, kernel='rbf', gamma=20, decision_function_shape='ovo')
# Fitting the classifier
clf.fit(x_train, y_train)
SVC(C=0.8, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovo', degree=3, gamma=20, kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
# Evaluation model
# Training accuracy
print("SVM-The accuracy of the output training set is:",clf.score(x_train, y_train))
# Test set accuracy
print("SVM-The accuracy of the output test set is:",clf.score(x_test,y_test))
SVM-The accuracy of the output training set is: 1.0
SVM-The accuracy of the output test set is: 0.85
Simple test, follow-up supplement
love&peace
Recommended Posts