Python is short _SVM test

Python bitter short _SVM algorithm#

Overview##

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

Code##

# 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

Concluding remarks##

Simple test, follow-up supplement

love&peace

Recommended Posts

Python is short _SVM test
Python 3.9 is here!
Why python is popular
Python is short-crawling music
Python is slowly fading
Is python an interpreted language?
Is python an interpreted language
Python is short-world epidemic map
Is python code case sensitive
What is introspection in python
What is object-oriented in python
What is Python variable scope
Python Data Science: Chi-Square Test
What is list comprehension in python
Is there function overloading in python
Python judges that it is empty
What is the use of Python
Is python language free or paid?
Everything in Python is an object
Is python suitable for data mining
Is python crawler easy to learn