我有这样的数据:
print training_data
print labels
# prints
[[1, 0, 1, 1], [1, 1, 1, 1], [1, 0, 1, 1], [1, 1, 1, 0], [1, 1, 0, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 0,0], [1, 1, 1, 1], [1, 0, 1, 1]]
['a', 'b', 'a', 'b', 'a', 'b', 'b', 'a', 'a', 'a', 'b']我正试着把它从sklearn python库提供给一个RandomForestClassifier。
classifier = RandomForestClassifier(n_estimators=10)
classifier.fit(training_data, labels)但是会收到这个错误:
Traceback (most recent call last):
File "learn.py", line 52, in <module>
main()
File "learn.py", line 48, in main
classifier = train_classifier()
File "learn.py", line 33, in train_classifier
classifier.fit(training_data, labels)
File "/Library/Python/2.7/site-packages/scikit_learn-0.14_git-py2.7-macosx-10.8-intel.egg/sklearn/ensemble/forest.py", line 348, in fit
y = np.ascontiguousarray(y, dtype=DOUBLE)
File "/Library/Python/2.7/site-packages/numpy-1.8.0.dev_bbcfcf6_20130307-py2.7-macosx-10.8-intel.egg/numpy/core/numeric.py", line 419, in ascontiguousarray
return array(a, dtype, copy=False, order='C', ndmin=1)
ValueError: could not convert string to float: a我的猜测是,我没有正确地格式化此数据以进行拟合。但是我不明白为什么从the documentation
这似乎是一个非常基本、简单的问题。有人知道答案吗?
发布于 2013-04-08 03:44:48
尝试使用LabelEncoder预先转换标签。
发布于 2015-05-28 00:01:31
您可以使用由分类器自动识别的numpy数组,如下所示:
import numpy as np
from sklearn.ensemble import RandomForestClassifier
np_training = np.array(training_data)
np_labels = np.array(labels)
clf = RandomForestClassifier(n_estimators=20, max_depth=5)
clf.fit(np_training, np_labels)这应该是可行的
https://stackoverflow.com/questions/15866843
复制相似问题