GBDT 特征重要度計算


GBDT原理和推導:https://blog.csdn.net/yangxudong/article/details/53872141

 

Pyspark 分類、回歸、聚類示例:

https://blog.csdn.net/littlely_ll/article/details/78151964

https://blog.csdn.net/littlely_ll/article/details/78161574?utm_source=blogxgwz2

https://blog.csdn.net/littlely_ll/article/details/78155192

 

 

 

特征重要度的計算
Friedman在GBM的論文中提出的方法:特征j的全局重要度通過特征j在單顆樹中的重要度的平均值來衡量。

 

實現代碼片段

為了更好的理解特征重要度的計算方法,下面給出scikit-learn工具包中的實現,代碼移除了一些不相關的部分。

下面的代碼來自於GradientBoostingClassifier對象的feature_importances屬性的計算方法:

def feature_importances_(self):
    total_sum = np.zeros((self.n_features, ), dtype=np.float64)
    for tree in self.estimators_:
        total_sum += tree.feature_importances_ 
    importances = total_sum / len(self.estimators_)
    return importances

  

其中,self.estimators_是算法構建出的決策樹的數組,tree.feature_importances_ 是單棵樹的特征重要度向量,其計算方法如下:

cpdef compute_feature_importances(self, normalize=True):
    """Computes the importance of each feature (aka variable)."""

    while node != end_node:
        if node.left_child != _TREE_LEAF:
            # ... and node.right_child != _TREE_LEAF:
            left = &nodes[node.left_child]
            right = &nodes[node.right_child]

            importance_data[node.feature] += (
                node.weighted_n_node_samples * node.impurity -
                left.weighted_n_node_samples * left.impurity -
                right.weighted_n_node_samples * right.impurity)
        node += 1

    importances /= nodes[0].weighted_n_node_samples

    return importances

  

上面的代碼經過了簡化,保留了核心思想。計算所有的非葉子節點在分裂時加權不純度的減少,減少得越多說明特征越重要。

不純度的減少實際上就是該節點此次分裂的收益,因此我們也可以這樣理解,節點分裂時收益越大,該節點對應的特征的重要度越高。




原文鏈接:https://blog.csdn.net/yangxudong/article/details/53899260


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM