为什么 RandomForestRegressor 的交叉验证在 scikit-learn 中失败



输入训练或测试文件格式如下:

-1 1 11.10115101|u 11.10115101 |s 2 |reason k:0.116|pv pv1000|g 2230444827 |k k3|w k:0
-1 1 11.10115101|u 11.10115101 |s 0 |reason c:0.080|pv pv1000|g 2235873129 |k k0|w c:1
-1 1 11.10115101|u 11.10115101 |s 1 |reason h:0.054 o:0.073|pv pv1000|g 2236879382 |k k10|w h:1 o:21
-1 1 11.10115101|u 11.10115101 |s 0 |reason u:0.133|pv pv1000|g 2237638819 |k k5|w u:26
-1 1 11.10115101|u 11.10115101 |s 0 |reason o:0.086|pv pv1000|g 2237694729 |k k5|w o:11
-1 1 11.10115101|u 11.10115101 |s 2 |reason l:0.111|pv pv1000|g 2237821631 |k k3|w l:0

代码如下,load_data(( 函数将训练数据或测试数据加载到 python dict 列表中,并返回一个元组([dict,...], [0,1,0...](:

parser = argparse.ArgumentParser()
parser.add_argument('-t', '--train', required = True, help='train file')
parser.add_argument('-e', '--test', required = True, help='test file')
ns = parser.parse_args(sys.argv[1:])
f = open(ns.train)
inputs, targets = load_data( f )
print >>sys.stderr, 'load finish'
vec = DictVectorizer()
train = vec.fit_transform( inputs)
print >>sys.stderr, 'dict vectorizer finish'
print >>sys.stderr, 'training'
clf = RandomForestRegressor()
clf.fit(train.toarray(), targets)

print >>sys.stderr, 'testing'
f = open(ns.test)
test_inputs, test_targets = load_data( f )
test = vec.transform(test_inputs)
print cross_validation.cross_val_score(clf, test.toarray(), test_targets, scoring='roc_auc')

训练工作正常,但是在进行交叉验证时,代码的最后一行会抛出异常:

  File "randomforest.py", line 72, in <module>
    print cross_validation.cross_val_score(clf, test.toarray(), test_targets, scoring='roc_auc')
  File "/Users/jerry/pkgs/vpy/lib/python2.7/site-packages/sklearn/cross_validation.py", line 1152, in cross_val_score
    for train, test in cv)
  File "/Users/jerry/pkgs/vpy/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 517, in __call__
    self.dispatch(function, args, kwargs)
  File "/Users/jerry/pkgs/vpy/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 312, in dispatch
    job = ImmediateApply(func, args, kwargs)
  File "/Users/jerry/pkgs/vpy/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 136, in __init__
    self.results = func(*args, **kwargs)
  File "/Users/jerry/pkgs/vpy/lib/python2.7/site-packages/sklearn/cross_validation.py", line 1058, in _cross_val_score
    y_train = y[train]
TypeError: only integer arrays with one element can be converted to an index

我像手动示例一样编写代码,但失败了。

此错误与最近报告的问题 #2508 匹配。

解决方法是调用 add:

test_targets = np.asarray(test_targets)

在呼叫cross_val_score之前。

我使用另一种方法来计算auc,例如:

preds = clf.predict_proba(test)
fpr, tpr, thresholds = roc_curve( test_targets, preds[:, 1])
roc_auc = auc(fpr, tpr)

相关内容

  • 没有找到相关文章

最新更新