【问题标题】:Random Forest evaluation for array with floats and integers - numpy带有浮点数和整数的数组的随机森林评估 - numpy
【发布时间】: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


【解决方案1】:

您需要将 y_labels 转换为整数,以便 RandomForestClassifier 可以对其进行训练。

test_set = data_features[:,9].astype(int)

【讨论】:

    猜你喜欢
    • 2015-10-19
    • 2021-10-28
    • 2018-04-21
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2014-04-30
    • 2015-05-22
    相关资源
    最近更新 更多