| 188 | } |
| 189 | |
| 190 | TVector<TFeaturePairInteractionInfo> CalcMostInteractingFeatures(const TVector<TMxTree>& trees, |
| 191 | int topPairsCount) { |
| 192 | int featureCount = GetMaxSrcFeature(trees) + 1; |
| 193 | THashMap<std::pair<int, int>, double> sumInteractions; |
| 194 | |
| 195 | for (int i = 0; i < trees.ysize(); ++i) { |
| 196 | const TMxTree& tree = trees[i]; |
| 197 | for (int firstIdx = 0; firstIdx < tree.SrcFeatures.ysize() - 1; ++firstIdx) { |
| 198 | for (int secondIdx = firstIdx + 1; secondIdx < tree.SrcFeatures.ysize(); ++secondIdx) { |
| 199 | int n1 = 1 << firstIdx; |
| 200 | int n2 = 1 << secondIdx; |
| 201 | double delta = 0; |
| 202 | for (int leafIdx = 0; leafIdx < tree.Leaves.ysize(); ++leafIdx) { |
| 203 | int var1 = (leafIdx & n1) != 0; |
| 204 | int var2 = (leafIdx & n2) != 0; |
| 205 | int sign = (var1 ^ var2) ? 1 : -1; |
| 206 | for (int valInLeafIdx = 0; valInLeafIdx < tree.Leaves[leafIdx].Vals.ysize(); ++valInLeafIdx) { |
| 207 | delta += sign * tree.Leaves[leafIdx].Vals[valInLeafIdx]; |
| 208 | } |
| 209 | } |
| 210 | int srcFeature1 = tree.SrcFeatures[firstIdx]; |
| 211 | int srcFeature2 = tree.SrcFeatures[secondIdx]; |
| 212 | if (srcFeature2 < srcFeature1) { |
| 213 | DoSwap(srcFeature1, srcFeature2); |
| 214 | } |
| 215 | if (srcFeature1 == srcFeature2) { |
| 216 | continue; |
| 217 | } |
| 218 | sumInteractions[std::make_pair(srcFeature1, srcFeature2)] += fabs(delta); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | return PostProcessSumInteractions(sumInteractions, featureCount, topPairsCount); |
| 223 | } |
| 224 | |
| 225 | static void DFS(const TFullModel& model, const THashMap<TFeature, int, TFeatureHash>& featureToIdx, ui32 nodeIdx, TVector<std::pair<int, int>>* pathPtr, THashMap<std::pair<int, int>, double>* sumInteractionsPtr) { |
| 226 | const int split = model.ModelTrees->GetModelTreeData()->GetTreeSplits()[nodeIdx]; |
no test coverage detected