SVM的学习记录
转载于:https://blog.csdn.net/made_in_china_too/article/details/82287805
示例实验部分
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.datasets import make_blobs
def plot_hyperplane(clf, X, y,
h=0.02,
draw_sv=True,
title='hyperplane'):
# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
plt.title(title)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
# plt.xticks(())
# plt.yticks(())
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap='hot', alpha=0.5)
markers = ['o', 's', '^']
colors = ['b', 'r', 'c']
labels = np.unique(y)
for label in labels:
plt.scatter(X[y == label][:, 0],
X[y == label][:, 1],
c=colors[label],
marker=markers[label], s=20)
if draw_sv:
sv = clf.support_vectors_
plt.scatter(sv[:, 0], sv[:, 1], c='black', marker='x', s=15)
X, y = make_blobs(n_features=2,n_samples=100, centers=3, random_state=0, cluster_std=[0.3,1.5,0.2])
clf = svm.SVC(C = 1.0, kernel='linear')
clf.fit(X,y)
print(X)
print(y)
print(X[y == 0][:, 0])
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.01), np.arange(y_min, y_max, 0.01))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)
# xx, yy = np.meshgrid(np.arange(1, 4, 1),np.arange(1, 6, 1))
# print(np.c_[xx.ravel(), yy.ravel()])
# print(yy)
plt.figure()
plt.scatter(X[y == 0][:, 0],X[y == 0][:, 1], s=20)
print(clf.score(X, y))
# plt.figure(figsize=(10,3), dpi=100)
plt.contourf(xx, yy, Z, cmap='hot', alpha=0.5)
# plot_hyperplane(clf, X, y, h=0.01, title='Maximiin Margin Hyperplan')
plt.show()
正文部分
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.datasets import make_blobs
def plot_hyperplane(clf, X, y,
h=0.02,
draw_sv=True,
title='hyperplane'):
# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
plt.title(title)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
# plt.xticks(())
# plt.yticks(())
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap='hot', alpha=0.5)
markers = ['o', 's', '^']
colors = ['b', 'r', 'c']
labels = np.unique(y)
for label in labels:
plt.scatter(X[y == label][:, 0],
X[y == label][:, 1],
c=colors[label],
marker=markers[label], s=20)
if draw_sv:
sv = clf.support_vectors_
plt.scatter(sv[:, 0], sv[:, 1], c='black', marker='x', s=15)
X, y = make_blobs(n_features=2,n_samples=100, centers=3, random_state=0, cluster_std=[0.3,1.5,0.2])
clf = svm.SVC(C = 1.0, kernel='linear')
clf.fit(X,y)
print(X)
print(y)
print(X[y == 0][:, 0])
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.01), np.arange(y_min, y_max, 0.01))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)
# xx, yy = np.meshgrid(np.arange(1, 4, 1),np.arange(1, 6, 1))
# print(np.c_[xx.ravel(), yy.ravel()])
# print(yy)
plt.figure()
plt.scatter(X[y == 0][:, 0],X[y == 0][:, 1], s=20)
print(clf.score(X, y))
# plt.figure(figsize=(10,3), dpi=100)
plt.contourf(xx, yy, Z, cmap='hot', alpha=0.5)
# plot_hyperplane(clf, X, y, h=0.01, title='Maximiin Margin Hyperplan')
plt.show()
import numpy as np
from sklearn.model_selection import GridSearchCV
parameters={'kernel':['linear','rbf','sigmoid','poly'],'C':np.linspace(0.1,20,50),'gamma':np.linspace(0.1,20,20)}
svc = svm.SVC()
model = GridSearchCV(svc,parameters,cv=5,scoring='accuracy')
model.fit(X_train,y_train)
model.best_params_
model.score(X_test,y_test)
使用交叉验证以及网格搜索的示例代码
此处代码的原文链接:https://blog.csdn.net/weixin_41990278/article/details/93137009