python svm超参数_sklearn中的超参数调节

进行参数的选择是一个重要的步骤。在机器学习当中需要我们手动输入的参数叫做超参数,其余的参数需要依靠数据来进行训练,不需要我们手动设定。进行超参数选择的过程叫做调参。

进行调参应该有一下准备条件:

一个学习器

一个参数空间

一个从参数空间当中寻找参数的方法

一个交叉验证的规则

一个性能评估的策略

下面我介绍几种调参的方法:

1:穷举式的网格搜索

sklearn当中的GridSearchCV实现了这种穷举是的网格搜索,其实这种方法是很简单的。下面是使用交叉验证来进行网格搜索的一个例子:

from sklearn importdatasetsfrom sklearn.model_selection importtrain_test_split, GridSearchCVfrom sklearn.svm importSVCfrom sklearn.metrics importclassification_report

digits=datasets.load_digits()

n_samples=len(digits.images)

X= digits.images.reshape((n_samples, -1))

y=digits.target

X_train, X_test, y_train, y_test=train_test_split(

X, y, test_size=0.5, random_state=0)#上面的操作是导入数据,并且把数据划分为训练数据和测试数据,这里使用的是手写数字识别数据集

tunned_parameters= [{'kernel':['rbf'],'gamma':[1e-3, 1e-4],'C':[1, 10, 100, 1000]},

{'kernel':['linear'], 'C':[1, 10, 100, 1000]}]

scores= ['precision', 'recall'] #这是我们使用的评分策略,因为是多分类问题,所以最后的评分策略为precision_macro 和 recall_macro 见下面

for score inscores:print("# Tuning hyper-parameters for %s" %score)print()

clf= GridSearchCV(SVC(), tunned_parameters, cv=5

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个SVM超参数对比的Python可视化案例。 首先,我们需要导入必要的库和数据集。在这个案例,我们将使用Iris数据集。 ```python import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.svm import SVC from sklearn.metrics import accuracy_score # Load the iris dataset iris = datasets.load_iris() X = iris.data[:, :2] y = iris.target # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) ``` 接下来,我们定义一个函数来训练SVM,并返回测试集的准确率。 ```python def train_svm(C, gamma): # Create an SVM classifier svm = SVC(kernel='rbf', C=C, gamma=gamma) # Train the classifier on the training data svm.fit(X_train, y_train) # Make predictions on the testing data y_pred = svm.predict(X_test) # Calculate the accuracy of the classifier acc = accuracy_score(y_test, y_pred) return acc ``` 现在,我们可以使用这个函数来比较不同的超参数组合,并将结果可视化。 ```python # Define the range of values for C and gamma C_range = np.logspace(-3, 3, 7) gamma_range = np.logspace(-3, 3, 7) # Create a meshgrid of C and gamma values C, gamma = np.meshgrid(C_range, gamma_range) # Initialize an array to store the accuracy scores acc_scores = np.zeros((len(C_range), len(gamma_range))) # Calculate the accuracy score for each combination of C and gamma for i in range(len(C_range)): for j in range(len(gamma_range)): acc_scores[i, j] = train_svm(C_range[i], gamma_range[j]) # Plot the accuracy scores as a heatmap plt.figure(figsize=(10, 8)) plt.imshow(acc_scores, interpolation='nearest', cmap=plt.cm.hot) plt.xlabel('gamma') plt.ylabel('C') plt.colorbar() plt.xticks(np.arange(len(gamma_range)), gamma_range, rotation=45) plt.yticks(np.arange(len(C_range)), C_range) plt.title('SVM accuracy') plt.show() ``` 这将生成一个热图,显示不同的C和gamma值组合的准确性得分。 ![SVM hyperparameter comparison](https://i.imgur.com/7VdWZbQ.png) 从图可以看出,当C和gamma的值都较小时,准确性得分较低。当C和gamma的值都较大时,准确性得分最高。此外,当C和gamma的值相等时,准确性得分也较高。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值