python - 多标签 OneVsRestClassifier 的 GridSearch?

标签 python scikit-learn

我正在对多标签数据进行网格搜索,如下所示:

#imports
from sklearn.svm import SVC as classifier
from sklearn.pipeline import Pipeline
from sklearn.decomposition import RandomizedPCA
from sklearn.cross_validation import StratifiedKFold
from sklearn.grid_search import GridSearchCV

#classifier pipeline
clf_pipeline = clf_pipeline = OneVsRestClassifier(
                Pipeline([('reduce_dim', RandomizedPCA()),
                          ('clf', classifier())
                          ]
                         ))

C_range = 10.0 ** np.arange(-2, 9)
gamma_range = 10.0 ** np.arange(-5, 4)
n_components_range = (10, 100, 200)
degree_range = (1, 2, 3, 4)

param_grid = dict(estimator__clf__gamma=gamma_range,
                  estimator__clf__c=c_range,
                  estimator__clf__degree=degree_range,
                  estimator__reduce_dim__n_components=n_components_range)

grid = GridSearchCV(clf_pipeline, param_grid,
                                cv=StratifiedKFold(y=Y, n_folds=3), n_jobs=1,
                                verbose=2)
grid.fit(X, Y)

我看到了以下回溯:

/Users/andrewwinterman/Documents/sparks-honey/classifier/lib/python2.7/site-packages/sklearn/grid_search.pyc in fit_grid_point(X, y, base_clf, clf_params, train, test, loss_func, score_func, verbose, **fit_params)
    107 
    108     if y is not None:
--> 109         y_test = y[safe_mask(y, test)]
    110         y_train = y[safe_mask(y, train)]
    111         clf.fit(X_train, y_train, **fit_params)

TypeError: only integer arrays with one element can be converted to an index

看起来像 GridSearchCV 对象到多个标签。我应该如何解决这个问题?我是否需要使用 label_binarizer 明确地遍历唯一类,在每个子估计器上运行网格搜索?

最佳答案

我认为 grid_search.py​​ 中存在错误

您是否尝试过将 y 作为 numpy 数组?

import numpy as np
Y = np.asarray(Y)

关于python - 多标签 OneVsRestClassifier 的 GridSearch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14225882/

相关文章:

python - sed 保持匹配过去的换行符

Python 索引错误值不在列表中...on .index(value)

python - 从 scipy 导入 softmax 并在从 sklearn 导入后使用它时出现问题

machine-learning - 在给定连续类标签的情况下使用反向传播神经网络

python-3.x - 值错误 : Found input variables with inconsistent numbers of samples: [2750, 1095]

Python setuptools - 在子文件夹中维护文本文件引用?

python - 显示目录中的所有图像时出错

python - 在 Jupyter Python 中使用 Graphviz 设置节点位置

python - 使用 GridsearchCV 提取管道中最佳模型的 MLPRegressor 属性 (n_iter_ )?

python - 在 Scipy 中,为什么具有统一概率的 custom.rvs() 仅返回开始区域中的值?