【发布时间】:2017-07-26 16:30:44
【问题描述】:
我有一个数组,其中包含作为浮点数的特征值,我有一个标签数组,它们是整数 - 1 和 0。
示例: 特征值:
[[ 17.99 10.38 122.8 ..., 0.147 0.242 0.079]
[ 20.57 17.77 132.9 ..., 0.07 0.181 0.057]]
当我将标签附加到特征值数组时,标签变为浮动。 示例 - 附加 0 的特征值:
[[ 17.99 10.38 122.8 ..., 0.242 0.079 0. ]]
当我运行以下代码时:
training_set = data_features[:,0:9]
test_set = data_features[:,9]
seed = 7
num_trees = 100
max_features = 3
kfold = model_selection.KFold(n_splits=10, random_state=seed)
model = RandomForestClassifier(n_estimators=num_trees, max_features=max_features)
results = model_selection.cross_val_score(model, training_set, test_set, cv=kfold)
print(results.mean())
我收到一个错误:
raise ValueError("Unknown label type: %r" % y_type)
ValueError: Unknown label type: 'continuous'
根据我的阅读,我看到这是因为标签是浮动的。
如果我将特征值的 dtype 更改为“int”,代码确实可以工作,但我需要保留浮点数。
有没有办法将标签作为整数,将特征值作为浮点数,以便代码正常工作?
【问题讨论】:
-
test_set = data_features[:,9].astype(int)这应该可以解决问题。 -
但是我的测试集是我训练集的 10%,这也是浮动的。如果我这样做 .astype(int) 它会使测试集归零。
-
你只需要将一列转换为int。知道了。让我检查一下。如果它是一个标准示例,您可以分享更多代码或链接到它。
-
这实际上是我的错误,我将标签放入一个单独的数组中并且您的解决方案有效。谢谢!
标签: python arrays numpy random-forest