SVM支持向量机sklearn-wine红酒数据集代码V1
生活随笔
收集整理的這篇文章主要介紹了
SVM支持向量机sklearn-wine红酒数据集代码V1
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.datasets import make_blobs
from sklearn.datasets import load_wine# 定義函數(shù)作圖
def make_meshgrid(x, y, h=.02):x_min, x_max = x.min() - 1, x.max() + 1y_min, y_max = y.min() - 1, y.max() + 1# arange()-開始值、終值和步長創(chuàng)建表示等差數(shù)列的一維數(shù)組# linspace()-通過指定開始值、終值和元素個數(shù)xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h))return xx, yy# 定義繪制等高線
def plot_contours(ax, clf, xx, yy, **params):Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])Z = Z.reshape(xx.shape)out = ax.contourf(xx, yy, Z, **params)return outwine = load_wine()
X = wine.data[:, :2]
y = wine.targetC = 1.0
models = (svm.SVC(kernel='linear',C=C),svm.LinearSVC(C=C),svm.SVC(kernel='rbf', gamma=0.7, C=C),svm.SVC(kernel='poly', degree=3, C=C))
models = (clf.fit(X, y) for clf in models)titles = ('SVC with linear kernel','LinearSVC','SVC with RBF kernel','SVC with polt(degree=3) kernel')# 設(shè)定子圖排列
fig, sub = plt.subplots(2, 2)
plt.subplots_adjust(wspace=0.4, hspace=0.4)
# 作圖
X0, X1 = X[:, 0], X[:, 1]
xx, yy = make_meshgrid(X0, X1)for clf, title, ax in zip(models, titles, sub.flatten()):plot_contours(ax, clf, xx, yy, cmap=plt.cm.plasma, alpha=0.8)ax.scatter(X0, X1, c=y, cmap=plt.cm.plasma, s=20, edgecolors='k')ax.set_xlim(xx.min(), xx.max())ax.set_ylim(yy.min(), yy.max())ax.set_xlabel('Feature 0')ax.set_ylabel('Feature 1')ax.set_xticks(())ax.set_yticks(())ax.set_title(title)plt.show()
總結(jié)
以上是生活随笔為你收集整理的SVM支持向量机sklearn-wine红酒数据集代码V1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 实现桌面壁纸自动更换
- 下一篇: 计算机主机组成成分,手机电脑芯片主要由什