- Predict the leaf node for each data point using the predict function.
- Extract the unique leaf nodes from the predictions.
- Construct a logical matrix where each row corresponds to a data point and each column corresponds to a leaf.
Which point is in which leaf in decision trees
3 次查看(过去 30 天)
显示 更早的评论
I have a regression tree created by fitrtree, I want to have a logical matrix z with 'n = number of data points' as rows and 'tl=number of leaves in my decision tree' as columns, and then I want z(i,j)=1 if data point i is in leaf j. I could not find such a property in fitrtree or something related so that I could use it and because I need to run this code fore different datasets, I can not write something specific to the data and the created tree.
Is there any way I can do such a thing?
Thank you
0 个评论
回答(1 个)
Ayush Aniket
2025-6-9
You can achieve this by using the predict function and the node property of the tree. Refer the steps below and the code snippet that uses synthetic data to depict the algorithm:
% Generate synthetic data
rng(42); % Set random seed for reproducibility
n = 10; % Number of data points
numFeatures = 3; % Number of features
X = randn(n, numFeatures); % Random features
Y = X(:,1) * 2 + X(:,2) * -3 + X(:,3) * 1.5 + randn(n,1) * 0.5; % Target values with noise
% Train regression tree
Mdl = fitrtree(X, Y);
% Predict leaf assignments for each data point
[~, node] = predict(Mdl, X);
% Get unique leaf nodes
uniqueLeaves = unique(node);
tl = numel(uniqueLeaves);
n = numel(node);
% Initialize logical matrix
z = false(n, tl);
% Populate logical matrix
for i = 1:n
leafIndex = find(uniqueLeaves == node(i));
z(i, leafIndex) = true;
end
% Display the logical matrix (optional)
disp(z);
This method ensures that z(i,j) = 1 if data point i belongs to leaf j. Since it dynamically adapts to different datasets, it should work across various scenarios.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!