您的位置:首页 > 编程语言 > Python开发

python 数据科学 - 【分类模型】 ☞ 稳健滴 SVM 支持向量机

2017-09-25 11:19 645 查看
from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression

iris = load_iris()

X = iris.data[0:100,[2,3]]
Y = iris.target[0:100]

'''
支持向量机:分界线距离两个类别的边界最远
'''
clf1 = SVC(kernel="linear")
clf1.fit(X, Y)

clf2 = LogisticRegression()
clf2.fit(X, Y)

from itertools import product
import numpy as np
import matplotlib.pyplot as plt

def plot_estimator(estimator, X, Y):
x0_min, x0_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x1_min, x1_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x0_min, x0_max, 0.1),
np.arange(x1_min, x1_max, 0.1))

Z = estimator.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.plot()
plt.contourf(xx, yy, Z, alpha=0.4, cmap = plt.cm.RdYlBu)
plt.scatter(X[:, 0], X[:, 1], c=Y,  cmap = plt.cm.brg)
plt.xlabel('Petal.Length')
plt.ylabel('Petal.Width')
plt.show()


plot_estimator(clf1, X, Y)
plot_estimator(clf2, X, Y)




'''
C, 对错误(边界的某些点)的容忍度,C值越大越不能容忍,宽度越小。C值越小间隔宽度越大。
'''

data = np.array([[-1,2,0],[-2,3,0],[-2,5,0],[-3,-4,0],[-0.1,2,0],[0.2,1,1],[0,1,1],[1,2,1], [1,1,1], [-0.4,0.5,1],[2,5,1]])
X = data[:, :2]
Y = data[:,2]

# Large Margin
clf = SVC(C=1.0, kernel='linear')
clf.fit(X, Y)
plot_estimator(clf,X,Y)

# Narrow Margin
clf = SVC(C=100000, kernel='linear')
clf.fit(X, Y)
plot_estimator(clf,X,Y)




SVM Kernels

from itertools import product
import numpy as np
import matplotlib.pyplot as plt

from sklearn.datasets import load_iris
from sklearn.svm import SVC
'''
rbf,不规则线
poly,弧线
linear,直线(非线性数据不好使用)
'''
iris = load_iris()
X = iris.data[:,[2,3]]
Y = iris.target

clf1 = SVC(kernel="rbf")
clf1.fit(X, Y)

clf2 = SVC(kernel="poly")
clf2.fit(X, Y)

clf3 = SVC(kernel="linear")
clf3.fit(X, Y)

def plot_estimator(estimator, X, Y, idx, title):
x0_min, x0_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x1_min, x1_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x0_min, x0_max, 0.1),
np.arange(x1_min, x1_max, 0.1))

Z = estimator.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

axarr[idx].contourf(xx, yy, Z, alpha=0.4, cmap = plt.cm.RdYlBu)
axarr[idx].scatter(X[:, 0], X[:, 1], c=Y,  cmap = plt.cm.brg)
axarr[idx].set_title(title)

f, axarr = plt.subplots(1, 3, sharex='col', sharey='row', figsize=(20, 5))

for idx, clf, title in zip([0,1,2],[clf1, clf2, clf3], ['rbf', 'poly', 'linear']):
plot_estimator(clf, X, Y, idx, title)
plt.show()




matplotlib.pyplot.subplots

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