【问题标题】:implementation of R random forest feature importance score in scikit-learn在 scikit-learn 中实现 R 随机森林特征重要性得分
【发布时间】:2015-11-14 16:58:24
【问题描述】:

我正在尝试在 sklearn 中为随机森林回归模型实现 R 的特征重要性评分方法;根据 R 的文档:

第一个度量是根据置换 OOB 数据计算得出的:对于每棵树, 记录数据袋外部分的预测误差 (分类的错误率,回归的 MSE)。那么同样是 在排列每个预测变量后完成。和...之间的不同 然后将两者在所有树上平均,并由 差异的标准差。如果标准差为 对于变量,差异等于 0,除法未完成 (但在这种情况下,平均值几乎总是等于 0)。

因此,如果我理解正确,我需要能够为每棵树中的 OOB 样本置换每个预测变量(特征)。

我知道我可以通过这样的方式访问训练有素的森林中的每棵树

numberTrees = 100
clf = RandomForestRegressor(n_estimators=numberTrees)
clf.fit(X,Y)
for tree in clf.estimators_:
    do something

是否有获取每棵树的 OOB 样本列表?也许我可以使用每棵树的random_state 来推导出 OOB 样本列表?

【问题讨论】:

    标签: python r scikit-learn regression random-forest


    【解决方案1】:

    虽然 R 使用 OOB 样本,但我发现通过使用所有训练样本,我在 scikit 中得到了相似的结果。我正在执行以下操作:

    # permute training data and score against its own model  
    epoch = 3
    seeds = range(epoch)
    
    
    scores = defaultdict(list) # {feature: change in R^2}
    
    # repeat process several times and then average and then average the score for each feature
    for j in xrange(epoch):
        clf = RandomForestRegressor(n_jobs = -1, n_estimators = trees, random_state = seeds[j],
                                   max_features = num_features, min_samples_leaf = leaf)
    
        clf = clf.fit(X_train, y_train)
        acc = clf.score(X_train, y_train)    
    
        print 'Epoch', j
        # for each feature, permute its values and check the resulting score
        for i, col in enumerate(X_train.columns):
            if i % 200 == 0: print "- feature %s of %s permuted" %(i, X_train.shape[1])
            X_train_copy = X_train.copy()
            X_train_copy[col] = np.random.permutation(X_train[col])
            shuff_acc = clf.score(X_train_copy, y_train)
            scores[col].append((acc-shuff_acc)/acc)
    
    # get mean across epochs
    scores_mean = {k: np.mean(v) for k, v in scores.iteritems()}
    
    # sort scores (best first)
    scores_sorted = pd.DataFrame.from_dict(scores_mean, orient='index').sort(0, ascending = False)
    

    【讨论】:

      猜你喜欢
      • 2015-09-28
      • 2021-05-09
      • 2015-03-28
      • 2021-08-29
      • 1970-01-01
      • 2015-12-16
      • 2013-06-07
      • 2016-06-02
      • 2015-09-20
      相关资源
      最近更新 更多