MCPcopy Create free account
hub / github.com/catboost/catboost / UpdateShapByFeaturePath

Function UpdateShapByFeaturePath

catboost/libs/fstr/shap_values.cpp:106–146  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

104}
105
106static void UpdateShapByFeaturePath(
107 const TVector<TFeaturePathElement>& featurePath,
108 const double* leafValuesPtr,
109 size_t leafId,
110 int approxDimension,
111 bool isOblivious,
112 double averageTreeApprox,
113 double conditionFeatureFraction,
114 TVector<TShapValue>* shapValuesInternal
115) {
116 const int approxDimOffset = isOblivious ? approxDimension : 1;
117 for (size_t elementIdx = 1; elementIdx < featurePath.size(); ++elementIdx) {
118 const TVector<TFeaturePathElement> unwoundPath = UnwindFeaturePath(featurePath, elementIdx);
119 double weightSum = 0.0;
120 for (const TFeaturePathElement& unwoundPathElement : unwoundPath) {
121 weightSum += unwoundPathElement.Weight;
122 }
123 const TFeaturePathElement& element = featurePath[elementIdx];
124 const auto sameFeatureShapValue = FindIf(
125 shapValuesInternal->begin(),
126 shapValuesInternal->end(),
127 [element](const TShapValue& shapValue) {
128 return shapValue.Feature == element.Feature;
129 }
130 );
131 const double coefficient =
132 conditionFeatureFraction * weightSum * (element.OnePathsFraction - element.ZeroPathsFraction);
133 if (sameFeatureShapValue == shapValuesInternal->end()) {
134 shapValuesInternal->emplace_back(element.Feature, approxDimension);
135 for (int dimension = 0; dimension < approxDimension; ++dimension) {
136 double value = coefficient * (leafValuesPtr[leafId * approxDimOffset + dimension] - averageTreeApprox);
137 shapValuesInternal->back().Value[dimension] = value;
138 }
139 } else {
140 for (int dimension = 0; dimension < approxDimension; ++dimension) {
141 double addValue = coefficient * (leafValuesPtr[leafId * approxDimOffset + dimension] - averageTreeApprox);
142 sameFeatureShapValue->Value[dimension] += addValue;
143 }
144 }
145 }
146}
147
148TConditionsFeatureFraction::TConditionsFeatureFraction(
149 const TMaybe<TFixedFeatureParams>& fixedFeatureParams,

Calls 7

UnwindFeaturePathFunction · 0.85
FindIfFunction · 0.85
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
emplace_backMethod · 0.45
backMethod · 0.45

Tested by

no test coverage detected