如何使用sklearn中的SVM
SVM分类算法我们前面已经讲过了,那么我们平时要用到SVM的时候,除了在MATLAB中调用libsvm之外,我们的python中的sklean也已经集成了SVM算法。这篇博客就讲一下sklearn中的SVM如何调用。
我们先说个例子,看看简单的使用sklean中的SVC(support vectors classification)怎么使用。
from sklearn import svm
X = [[0, 0], [1, 1], [1, 0]] # training samples
y = [0, 1, 1] # training target
clf = svm.SVC() # class
clf.fit(X, y) # training the svc model
result = clf.predict([2, 2]) # predict the target of testing samples
print result # target
print clf.support_vectors_ #support vectors
print clf.support_ # indeices of support vectors
print clf.n_support_ # number of support vectors for each class
其中有一些注释帮助理解,其实sklearn中调用机器学习的方法都是一个道理,算法就是一个类,其中包含fit(), predict()等等许多方法,我们只要输入训练样本和标记,以及模型的一些可能的参数,自然就直接出分类的结果。