|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef INCLUDED_ml_maths_CTreeShapFeatureImportance_h |
| 8 | +#define INCLUDED_ml_maths_CTreeShapFeatureImportance_h |
| 9 | + |
| 10 | +#include <maths/CBoostedTree.h> |
| 11 | +#include <maths/ImportExport.h> |
| 12 | + |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +namespace ml { |
| 16 | +namespace maths { |
| 17 | + |
| 18 | +//! \brief Computes SHAP (SHapley Additive exPlanation) values for feature importance estimation for gradient boosting |
| 19 | +//! trees. |
| 20 | +//! |
| 21 | +//! DESCRIPTION:\n |
| 22 | +//! SHAP values is a unique consistent and locally accurate attribution value. This mean that the sum of the SHAP |
| 23 | +//! feature importance values approximates the model prediction up to a constant bias. This implementation follows the |
| 24 | +//! algorithm "Consistent Individualized Feature Attribution for Tree Ensembles" by Lundberg, Erion, and Lee. |
| 25 | +//! The algorithm has the complexity O(TLD^2) where T is the number of trees, L is the maximum number of leaves in the |
| 26 | +//! tree, and D is the maximum depth of a tree in the ensemble. |
| 27 | +class MATHS_EXPORT CTreeShapFeatureImportance { |
| 28 | +public: |
| 29 | + using TTree = std::vector<CBoostedTreeNode>; |
| 30 | + using TTreeVec = std::vector<TTree>; |
| 31 | + using TIntVec = std::vector<int>; |
| 32 | + using TDoubleVec = std::vector<double>; |
| 33 | + using TDoubleVecVec = std::vector<TDoubleVec>; |
| 34 | + |
| 35 | +public: |
| 36 | + explicit CTreeShapFeatureImportance(TTreeVec trees, std::size_t threads = 1); |
| 37 | + |
| 38 | + //! Compute SHAP values for the data in \p frame using the specified \p encoder. |
| 39 | + //! The results are written directly back into the \p frame, the index of the first result column is controller |
| 40 | + //! by \p offset. |
| 41 | + void shap(core::CDataFrame& frame, const CDataFrameCategoryEncoder& encoder, std::size_t offset); |
| 42 | + |
| 43 | + //! Compute number of training samples from \p frame that pass every node in the \p tree. |
| 44 | + static TDoubleVec samplesPerNode(const TTree& tree, |
| 45 | + const core::CDataFrame& frame, |
| 46 | + const CDataFrameCategoryEncoder& encoder, |
| 47 | + std::size_t numThreads); |
| 48 | + |
| 49 | + //! Recursively computes inner node values as weighted average of the children (leaf) values |
| 50 | + //! \returns The maximum depth the the tree. |
| 51 | + static std::size_t updateNodeValues(TTree& tree, |
| 52 | + std::size_t nodeIndex, |
| 53 | + const TDoubleVec& samplesPerNode, |
| 54 | + std::size_t depth); |
| 55 | + |
| 56 | + //! Get the reference to the trees. |
| 57 | + TTreeVec& trees() { return m_Trees; }; |
| 58 | + |
| 59 | +private: |
| 60 | + using TSizeVec = std::vector<std::size_t>; |
| 61 | + |
| 62 | + //! Manages variables for the current path through the tree as the main algorithm proceeds. |
| 63 | + struct SPath { |
| 64 | + explicit SPath(std::size_t length) |
| 65 | + : s_FractionOnes(length), s_FractionZeros(length), |
| 66 | + s_FeatureIndex(length, -1), s_Scale(length), s_NextIndex(0), |
| 67 | + s_MaxLength(length) {} |
| 68 | + |
| 69 | + void extend(int featureIndex, double fractionZero, double fractionOne) { |
| 70 | + if (s_NextIndex < s_MaxLength) { |
| 71 | + s_FeatureIndex[s_NextIndex] = featureIndex; |
| 72 | + s_FractionZeros[s_NextIndex] = fractionZero; |
| 73 | + s_FractionOnes[s_NextIndex] = fractionOne; |
| 74 | + if (s_NextIndex == 0) { |
| 75 | + s_Scale[s_NextIndex] = 1.0; |
| 76 | + } else { |
| 77 | + s_Scale[s_NextIndex] = 0.0; |
| 78 | + } |
| 79 | + ++s_NextIndex; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + void reduce(std::size_t pathIndex) { |
| 84 | + for (std::size_t i = pathIndex; i < this->depth(); ++i) { |
| 85 | + s_FeatureIndex[i] = s_FeatureIndex[i + 1]; |
| 86 | + s_FractionZeros[i] = s_FractionZeros[i + 1]; |
| 87 | + s_FractionOnes[i] = s_FractionOnes[i + 1]; |
| 88 | + } |
| 89 | + --s_NextIndex; |
| 90 | + } |
| 91 | + |
| 92 | + //! Indicator whether or not the feature \p pathIndex is decicive for the path. |
| 93 | + double fractionOnes(std::size_t pathIndex) const { |
| 94 | + return s_FractionOnes[pathIndex]; |
| 95 | + } |
| 96 | + |
| 97 | + //! Fraction of all training data that reached the \pathIndex in the path. |
| 98 | + double fractionZeros(std::size_t pathIndex) const { |
| 99 | + return s_FractionZeros[pathIndex]; |
| 100 | + } |
| 101 | + |
| 102 | + int featureIndex(std::size_t pathIndex) const { |
| 103 | + return s_FeatureIndex[pathIndex]; |
| 104 | + } |
| 105 | + |
| 106 | + //! Scaling coefficients (factorials), see. Equation (2) in the paper by Lundberg et al. |
| 107 | + double scale(std::size_t pathIndex) const { return s_Scale[pathIndex]; } |
| 108 | + |
| 109 | + //! Current depth in the tree |
| 110 | + std::size_t depth() const { return s_NextIndex - 1; }; |
| 111 | + |
| 112 | + TDoubleVec s_FractionOnes; |
| 113 | + TDoubleVec s_FractionZeros; |
| 114 | + TIntVec s_FeatureIndex; |
| 115 | + TDoubleVec s_Scale; |
| 116 | + std::size_t s_NextIndex; |
| 117 | + std::size_t s_MaxLength; |
| 118 | + }; |
| 119 | + |
| 120 | +private: |
| 121 | + //! Recursively traverses all pathes in the \p tree and updated SHAP values once it hits a leaf. |
| 122 | + //! Ref. Algorithm 2 in the paper by Lundberg et al. |
| 123 | + void shapRecursive(const TTree& tree, |
| 124 | + const TDoubleVec& samplesPerNode, |
| 125 | + const CDataFrameCategoryEncoder& encoder, |
| 126 | + const CEncodedDataFrameRowRef& encodedRow, |
| 127 | + SPath splitPath, |
| 128 | + std::size_t nodeIndex, |
| 129 | + double parentFractionZero, |
| 130 | + double parentFractionOne, |
| 131 | + int parentFeatureIndex, |
| 132 | + std::size_t offset, |
| 133 | + core::CDataFrame::TRowItr& row) const; |
| 134 | + //! Extend the \p path object, update the variables and factorial scaling coefficients. |
| 135 | + static void extendPath(SPath& path, double fractionZero, double fractionOne, int featureIndex); |
| 136 | + //! Sum the scaling coefficients for the \p path without the feature defined in \p pathIndex. |
| 137 | + static double sumUnwoundPath(const SPath& path, std::size_t pathIndex); |
| 138 | + //! Updated the scaling coefficients in the \p path if the feature defined in \p pathIndex was seen again. |
| 139 | + static void unwindPath(SPath& path, std::size_t pathIndex); |
| 140 | + |
| 141 | +private: |
| 142 | + TTreeVec m_Trees; |
| 143 | + std::size_t m_NumberThreads; |
| 144 | + TDoubleVecVec m_SamplesPerNode; |
| 145 | +}; |
| 146 | +} |
| 147 | +} |
| 148 | + |
| 149 | +#endif // INCLUDED_ml_maths_CTreeShapFeatureImportance_h |
0 commit comments