SVM简单上手示例

转载自 百度知道 id:风_南(https://zhidao.baidu.com/usercenter?uid=e9904069236f25705e799313)

转载只为方便学习复习,侵删。

在用scikit-learn包训练机器学习模型时候,这里举一个训练SVM的例子:

1. 先要按照scikit-learn包,先安装下面三个依赖包:

Python (>= 2.6 or >= 3.3),

NumPy (>= 1.6.1),

SciPy (>= 0.9).

然后在cmd命令行中输入:

pip install  scikit-learn

import numpy as np
X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) #数据特征
y = np.array([1, 1, 2, 2])  # 数据对应的标签
from sklearn.svm import SVC  # 导入svm的svc类(支持向量分类)
clf = SVC()  # 创建分类器对象
clf.fit(X, y)  # 用训练数据拟合分类器模型
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
clf.predict([[-0.8, -1]])  # 用训练好的分类器去预测[-0.8, -1]数据的标签
#结果 [1]
原文地址:https://www.cnblogs.com/cpg123/p/10940798.html