【问题标题】:GridSearch with SVM producing IndexError带有 SVM 的 GridSearch 产生 IndexError
【发布时间】:2017-02-15 14:04:34
【问题描述】:

我正在使用 SVM 构建分类器,并希望执行网格搜索以帮助自动找到最佳模型。代码如下:

from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.multiclass import OneVsRestClassifier

X.shape     # (22343, 323)
y.shape     # (22343, 1)

X_train, X_test, y_train, y_test = train_test_split(
  X, Y, test_size=0.4, random_state=0
)

tuned_parameters = [
  {
    'estimator__kernel': ['rbf'],
    'estimator__gamma': [1e-3, 1e-4],
    'estimator__C': [1, 10, 100, 1000]
  },
  {
    'estimator__kernel': ['linear'], 
    'estimator__C': [1, 10, 100, 1000]
  }
]

model_to_set = OneVsRestClassifier(SVC(), n_jobs=-1)
clf = GridSearchCV(model_to_set, tuned_parameters)
clf.fit(X_train, y_train)

我收到以下错误消息(这不是整个堆栈跟踪。只是最后 3 次调用):

----------------------------------------------------
/anaconda/lib/python3.5/site-packages/sklearn/model_selection/_split.py in split(self, X, y, groups)
     88         X, y, groups = indexable(X, y, groups)
     89         indices = np.arange(_num_samples(X))
---> 90         for test_index in self._iter_test_masks(X, y, groups):
     91             train_index = indices[np.logical_not(test_index)]
     92             test_index = indices[test_index]

/anaconda/lib/python3.5/site-packages/sklearn/model_selection/_split.py in _iter_test_masks(self, X, y, groups)
    606 
    607     def _iter_test_masks(self, X, y=None, groups=None):
--> 608         test_folds = self._make_test_folds(X, y)
    609         for i in range(self.n_splits):
    610             yield test_folds == i

/anaconda/lib/python3.5/site-packages/sklearn/model_selection/_split.py in _make_test_folds(self, X, y, groups)
    593         for test_fold_indices, per_cls_splits in enumerate(zip(*per_cls_cvs)):
    594             for cls, (_, test_split) in zip(unique_y, per_cls_splits):
--> 595                 cls_test_folds = test_folds[y == cls]
    596                 # the test split can be too big because we used
    597                 # KFold(...).split(X[:max(c, n_splits)]) when data is not 100%

IndexError: too many indices for array

此外,当我尝试重塑数组以使 y 为 (22343,) 时,我发现 GridSearch 永远不会完成,即使我将 tune_parameters 设置为仅默认值。

如果有帮助,这里是所有软件包的版本:

Python:3.5.2

scikit-learn:0.18

熊猫:0.19.0

【问题讨论】:

  • 您是否尝试减少样本数量并运行它?

标签: python pandas machine-learning scikit-learn svm


【解决方案1】:

您的实现似乎没有错误。

但是,正如 sklearndocumentation 中所述,“拟合时间复杂度与样本数量成二次方,这使得很难扩展到具有多个 10000 样本的数据集”。 See documentation here

在您的情况下,您有 22343 样本,这可能会导致一些计算问题/内存问题。这就是为什么当你做你的默认简历时会花费很多时间。尝试使用10000 或更少的样本减少您的训练集。

【讨论】:

    猜你喜欢
    • 2021-09-16
    • 2019-06-02
    • 1970-01-01
    • 2018-11-30
    • 2021-12-08
    • 2018-03-07
    • 1970-01-01
    • 2016-09-06
    • 2015-06-03
    相关资源
    最近更新 更多