You need to enable JavaScript to run this app. 如何使用GridSearchCV对SVM自定义Kernel的gamma参数进行调优
优惠活动
大模型
产品
解决方案
定价
更多

如何使用GridSearchCV对SVM自定义Kernel的gamma参数进行调优

可行实现方案

核心思路是将带gamma参数的核函数封装为可参数化的可调用类,让gamma成为可以被GridSearchCV识别的嵌套超参数,无需手动切换参数计算准确率。

完整可运行代码

import numpy as np
from sklearn import svm
from sklearn import datasets
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import accuracy_score

# 封装可参数化的TanimotoLambda核
class TanimotoLambdaKernel:
    def __init__(self, gamma=0.01):
        self.gamma = gamma
    
    # 基础Tanimoto相似度计算,用numpy向量化优化速度
    def _tanimoto_base(self, xs, ys):
        a = np.sum(np.minimum(xs, ys))
        b = np.sum(np.maximum(xs, ys))
        return a / b
    
    # 实现__call__方法,符合SVC对自定义核函数的输入输出要求,直接返回Gram矩阵
    def __call__(self, X1, X2):
        gram_matrix = np.zeros((X1.shape[0], X2.shape[0]))
        for i, x1 in enumerate(X1):
            for j, x2 in enumerate(X2):
                tanimoto_sim = self._tanimoto_base(x1, x2)
                gram_matrix[i, j] = np.exp(self.gamma * tanimoto_sim) / (np.exp(self.gamma) - 1)
        return gram_matrix

# 加载数据
X, y = datasets.load_iris(return_X_y=True)
x_train, x_test, y_train, y_test = train_test_split(X, y, random_state=42)

# 初始化SVC,核函数为自定义的类实例
clf = svm.SVC(kernel=TanimotoLambdaKernel())

# 定义参数搜索网格,用双下划线访问嵌套对象的参数
param_grid = {
    'kernel__gamma': [0.001, 0.01, 0.1, 1, 10, 100] # 可根据需求调整搜索范围
}

# 初始化GridSearchCV
grid_search = GridSearchCV(clf, param_grid=param_grid, cv=5, scoring='accuracy', n_jobs=-1)
grid_search.fit(x_train, y_train)

# 输出最优参数和验证集准确率
print("最优gamma参数:", grid_search.best_params_['kernel__gamma'])
print("交叉验证最优准确率:", grid_search.best_score_)

# 用最优模型在测试集评估
best_clf = grid_search.best_estimator_
y_pred = best_clf.predict(x_test)
print("测试集准确率:", accuracy_score(y_test, y_pred))

补充说明

  • 原有代码的顺序错误(clf.fit写在clf定义之前)已经修正
  • 用numpy向量化替换了原有的循环计算Tanimoto相似度,运行速度更快
  • 数据量较大时可以进一步优化Gram矩阵的计算,比如用numba装饰器加速循环,或者改为完全向量化的计算逻辑
  • GridSearchCV会自动遍历所有gamma取值,做交叉验证选最优参数,无需手动测试

内容的提问来源于stack exchange,提问作者Norhther

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 20:15:03