您的位置:首页 > 其它

机器学习 SVM sklearn

2017-04-16 10:36 274 查看

SVM回归

代码:

import numpy as np
from sklearn import svm
import matplotlib.pyplot as plt

N = 50

np.random.seed(0)


x = np.sort(np.random.uniform(0, 6, N), axis=0)
y = 2*np.sin(x) + 0.1*np.random.randn(N)
x = x.reshape(-1, 1)

svr_rbf = svm.SVR(kernel='rbf', gamma=0.2, C=100)

'''C和gamma找到最优方式如下
model = svm.SVR(kernel='rbf')
c_can = np.logspace(-2, 2, 10)
gamma_can = np.logspace(-2, 2, 10)
svr = GridSearchCV(model, param_grid={'C': c_can, 'gamma': gamma_can}, cv=5)
svr.fit(x, y)
print '验证参数:\n', svr.best_params_
'''
svr_rbf.fit(x, y)svr_linear = svm.SVR(kernel='linear', C=100)svr_linear.fit(x, y)svr_poly = svm.SVR(kernel='poly', degree=3, C=100)svr_poly.fit(x, y)
x_test = np.linspace(x.min(), 1.5*x.max(), 100).reshape(-1, 1)
y_rbf = svr_rbf.predict(x_test)
y_linear = svr_linear.predict(x_test)
y_poly = svr_poly.predict(x_test)

matplotlib.rcParams['font.sans-serif'] = [u'SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(9, 8), facecolor='w')#可选 创建绘图对象9宽8高
plt.plot(x_test, y_rbf, 'r-', linewidth=1, label='RBF Kernel')#支持颜色bgrcmykw
plt.plot(x_test, y_linear, 'g-', linewidth=2, label='Linear Kernel')#支持线型-|--|-.|:|.|,|o|v|^|<|>|等
plt.plot(x_test, y_poly, 'b-', linewidth=2, label='Polynomial Kernel')
plt.plot(x, y, 'mo', markersize=6)
#散点图与plot使用类型
plt.scatter(x[svr_rbf.support_], y[svr_rbf.support_], s=130, c='r', marker='*', label='RBF Support Vectors')
plt.legend(loc='lower left')#显示图示
plt.title(u'回归SVR', fontsize=16)
plt.xlabel('X')
plt.ylabel('Y')
plt.grid(True)
plt.show()


SVM分类

代码:

import numpy as np
from sklearn import svm
import matplotlib as mpl
import matplotlib.colors
import matplotlib.pyplot as plt

# 分类器
clfs = [svm.SVC(C=0.3, kernel='linear'),
svm.SVC(C=10, kernel='linear'),
svm.SVC(C=5, kernel='rbf', gamma=1),
svm.SVC(C=5, kernel='rbf', gamma=4)]

for i, clf in enumerate(clfs):
clf.fit(x, y)
y_hat = clf.predict(x)

print '支撑向量的数目:', clf.n_support_

    print '支撑向量的系数:', clf.dual_coef_

    print '支撑向量:', clf.support_
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: