【问题标题】:SVM Hyperparamter tunning using GridSearchCV使用 GridSearchCV 调整 SVM 超参数
【发布时间】:2021-10-12 17:51:57
【问题描述】:

我正在尝试超调支持向量机分类器以准确预测具有较高重叠程度的类。目标是获得 C 的精确值,例如 7.568787 将类分开

处理这个的部分代码如下:

from sklearn.svm import SVC
from scipy.stats import loguniform
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.calibration import CalibratedClassifierCV

parameters = {"C": loguniform(1e-6, 1e+6)}

grid = GridSearchCV(estimator=CalibratedClassifierCV(SVC(kernel = 'rbf', gamma = 'scale', decision_function_shape='ovr', class_weight=None),method='sigmoid', cv=5), param_grid=parameters, refit = True, verbose = 3)

grid.fit(X_train, Y_train)

但是,当我尝试运行代码时,出现以下错误:

ValueError: Parameter grid for parameter (C) needs to be a list or numpy array, but got (<class 'scipy.stats._distn_infrastructure.rv_frozen'>). Single values need to be wrapped in a list with one element.

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    loguniform 返回一个

    你可以这样使用它:

    loguniform(1e-6, 1e+6).rvs(#number of samples you want)
    

    .rvs(size):从分布中抽取随机样本

    编辑 管道示例

    pipeline = make_pipeline(
        CountVectorizer(stop_words="english"),
        TfidfTransformer(),
        DecisionTreeClassifier(),
    )
    
    params = {
            'decisiontreeclassifier__max_depth': (1,5,10,50,250,500,1000,5000),
    }
    
    gridSearch_decisiontree = GridSearchCV(pipeline, params, cv=3, iid=False, n_jobs=-1)
    gridSearch_decisiontree.fit(X, Y)
    

    【讨论】:

    • parameters = {"C": loguniform(1e-6, 1e+6).rvs(1000000)} 返回:ValueError: Invalid parameter C for estimator CalibratedClassifierCV(base_estimator=SVC(), cv =5)。使用 `estimator.get_params() 检查可用参数列表
    • 你只是传递了一个你称之为 C 的参数(它不知道那是什么)。通常,您还需要在其前面附加名称。检查我的编辑
    猜你喜欢
    • 1970-01-01
    • 2021-08-11
    • 2017-11-28
    • 2021-03-29
    • 2016-05-11
    • 2016-11-20
    • 2023-03-05
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多