【问题标题】:training data with GridSearchCV gives me ValueError, Sci-kit learn使用 GridSearchCV 训练数据给了我 ValueError,Sci-kit 学习
【发布时间】:2019-04-05 06:22:15
【问题描述】:
dataset name = faces

faces.data = independent variables

faces.target = dependent variable

from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.pipeline import make_pipeline

pca = PCA(n_components=150, whiten=True, random_state=42)
svc = SVC(kernel="rbf", class_weight="balanced")
model = make_pipeline(pca, svc)

# spliting data from faces dataset. data is x and target is y
from sklearn.model_selection import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(faces.data, faces.target, random_state=42)

我为 PCA 和 SVC 创建了一个管道,然后将数据拆分为训练集和测试集。

# explore combinations of paramters
from sklearn.model_selection import GridSearchCV

param_grid = {'svc_C':[1,5,10,50],
             'svc_gamma':[0.0001, 0.0005, 0.001, 0.005]}
# instantiate grid of GridSearchCV class
# model uses pca to extract meaningful features then svc to find support vector

grid = GridSearchCV(model, param_grid)

grid.fit(Xtrain,ytrain)

当我在通过 PCA 和 SVC 之后尝试使用 GridSearchCV 训练数据时,它给了我一个错误,上面写着 "ValueError: Invalid parameter svc_C for estimator Pipeline"

有什么建议吗?

【问题讨论】:

  • 你在哪里调用 fit 函数到你的管道?
  • @SergeyBushmanov 必须忘记抱歉,谢谢。

标签: python machine-learning scikit-learn


【解决方案1】:

我敢打赌,您的参数应该包括双取消划线,例如:

param_grid = {'svc__C':[1,5,10,50],
             'svc__gamma':[0.0001, 0.0005, 0.001, 0.005]}

【讨论】:

  • 哦...我明白了。它适用于双下划线,但你能解释一下原因吗?
  • 某些参数的名称中带有下划线。通过添加两个下划线,您可以清楚区分管道中步骤的名称和它们的参数名称
猜你喜欢
  • 2013-05-23
  • 2015-12-16
  • 2020-01-27
  • 2019-01-29
  • 2020-10-26
  • 2014-07-16
  • 2013-10-13
  • 2017-06-25
  • 2017-04-06
相关资源
最近更新 更多