概述
from sklearn import svm import numpy as np import time from sklearn.model_selection import ShuffleSplit from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split from sklearn.model_selection import GridSearchCV from matplotlib import pyplot as plt from ROC import plot_learning_curve cancer = load_breast_cancer() X = cancer.data y = cancer.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # C表示错误可接受的范围,kernel 表示所选择的核函数,gamma时对应选择高斯核的参数 # clf_rbf = svm.SVC(C=1.0, kernel='rbf', gamma=0.1) # clf_rbf.fit(X_train, y_train) # # train_score = clf_rbf.score(X_train, y_train) # test_score = clf_rbf.score(X_test, y_test) # # print("train score:{0}; test score:{1}".format(train_score, test_score)) # # 计算出最佳的gamma值 # gammas = np.linspace(0, 0.0003, 30) # param_grid = {'gamma': gammas} # clf = GridSearchCV(svm.SVC(), param_grid, cv=5) # clf.fit(X, y) # # print("best param: {0}nbest score: {1}".format(clf.best_params_, clf.best_score_)) # # cv = ShuffleSplit(n_splits=10, test_size=0.2, random_state=0) # title = 'Learning Curves for Gaussian Kernel' # # # plt.figure(figsize=(10, 4), dpi=144) # plot_learning_curve(svm.SVC(C=1.0, kernel='rbf', gamma=0.01), title, X, y, ylim=(0.5, 1.01), cv=cv) # plt.show() # # 多项式核函数 degree表示多项式的最高阶 # clf = svm.SVC(C=1.0, kernel='poly', degree=2) # clf.fit(X_train, y_train) # train_score = clf.score(X_train, y_train) # test_score = clf.score(X_test, y_test) # print("train score:{0}; test score:{1}".format(train_score, test_score)) cv = ShuffleSplit(n_splits=5, test_size=0.2, random_state=0) title = 'Learning Curves with degree={0}' degree = [1, 2, 3] plt.figure(figsize=(12, 4), dpi=144) for i in range(len(degree)): plt.subplot(1, len(degree), i+1) plot_learning_curve(svm.SVC(C=1, kernel='poly', degree=degree[i]), title.format(degree[i]), X, y, ylim=(0.8, 1.01), cv=cv, n_jobs=4) plt.show()
最后
以上就是风中小馒头为你收集整理的sklearn----SVM高斯核与多项式核函数的全部内容,希望文章能够帮你解决sklearn----SVM高斯核与多项式核函数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复