RandomForest权重错误没有属性副本

时间:2014-06-08 15:08:55

标签: python scikit-learn

我正在尝试随机森林分类器算法,当在拟合树时使用权重时,我收到此错误:

rf = RandomForestClassifier(n_estimators=10, n_jobs=1)
rf.fit(train, target, my_weights)

CONSOLE:

line 86, in _parallel_build_trees

curr_sample_weight = sample_weight.copy()
AttributeError: 'list' object has no attribute 'copy'

我做错了什么?

    dataset = genfromtxt(open('data/training_edited.csv','r'), delimiter=',',dtype=float)[1:]
    print("Reading training.csv")
    target = [x[32] for x in dataset]
    my_weights = [x[31] for x in dataset]
    train = [x[1:31] for x in dataset]

2 个答案:

答案 0 :(得分:4)

您需要提供np.ndarray作为输入,至少为sample_weight,但理想情况下是所有输入:

改变你拥有的东西

import numpy as np
target = np.array([x[32] for x in dataset])  # dataset[:, 32]
my_weights = np.array([x[31] for x in dataset])  # dataset[:, 31]
train = np.array([x[1:31] for x in dataset])  # dataset[:, 1:31]

这甚至可以更优雅地完成,因为dataset本身(可能)已经是一个数组(如果文件是同类的),请将注释代码视为建议。

答案 1 :(得分:-2)

而不是curr_sample_weight = sample_weight.copy()

curr_sample_weight = sample_weight[:]

复制列表尝试上面的行

使用copy.copy():

import copy
curr_sample_weight = copy.copy(sample_weight)