'SVC' 对象没有属性 'best_params_'

'SVC' object has no attribute 'best_params_'

def ParamSelection(X, Y, nfolds): 
    Cs = [0.0001, 0.001, 0.01, 0.1, 1, 10, 100]
    degrees = [1, 2, 3, 4, 5]
    param_grid = {'C': Cs, 'degree' : degrees}
    grid_search = GridSearchCV(svm.SVC(kernel = 'poly'), param_grid, cv = nfolds)
    grid_search = svm.SVC(gamma='scale')
    grid_search.fit(X, Y)
    grid_search.best_params_
    return grid_search.best_params_
ParamSelection(trainX, trainY, 10)

AttributeError: 'SVC' object has no attribute 'best_params_'

我收到上述错误。我应该怎么办?你能帮忙吗?

您将 GridSearch 重新分配给 svm,因此您的 GridSearch 对象不是 sklearn 对象。只需删除此行:

grid_search = svm.SVC(gamma='scale')

那么应该 运行 没问题。你会收到弃用警告,因此在实例化 SVC():

时设置 gamma 参数
grid_search = GridSearchCV(svm.SVC(kernel = 'poly', gamma='scale'), param_grid, cv = nfolds)