Python GridSearchCV索引xxxxx超出大小xxxxxx的范围

2024-09-30 20:20:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用GridSerach搜索分类器的最佳超参数,如下所述:http://scikit-learn.org/stable/auto_examples/model_selection/plot_nested_cross_validation_iris.html

下面是一段代码的外观:

  X = X.values   # convert from pandas Dataframe to numpy array
  y = np.array(y)
  n_samples, n_features = X.shape 
  n_outputs = y.shape[0]

  inner_cv = cross_validation.StratifiedKFold(y, n_folds=4, shuffle=True, random_state=rnd)
  outer_cv = cross_validation.StratifiedKFold(y, n_folds=kFold, shuffle=True, random_state=rnd)

  # Non_nested parameter search and scoring
  clf = GridSearchCV(estimator=pipeline, param_grid=param_dict, scoring= scores, cv=inner_cv)

  # Nested CV with parameter optimization
  nested_score = cross_validation.cross_val_score(clf, X=X, y=y, cv=outer_cv)
  nested_score.fit(X,y)
  nested_scores = nested_score.mean()

但是由于某些原因,我得到了这个错误:

^{pr2}$

X和y具有以下尺寸:

   X: (6066, 5)
   y: (6066,)

一切看起来都很正常。问题从何而来?在

谢谢分享你的意见。在


Tags: truerandomarraycvvalidationnestedinnerscore
1条回答
网友
1楼 · 发布于 2024-09-30 20:20:41

不知道你想在这里做什么,但是GridsearchCV不是一个分类器,因此你不能将它传递给cross-val_score。在

GridsearchCV使用不同的参数多次运行交叉验证。因此它表示多个分类器。一旦它被安装,它确实有一个最佳的分类器属性。在

相关问题 更多 >