Decision Tree Classifier from Scratch
Learn this problemProblem statement
Your task is to implement parts of the Decision Tree classification algorithm from scratch (i.e., without importing any libraries or packages). As a reminder, the Decision Tree building comprises four major steps:
- Build tree nodes, where inner nodes contain feature index and value based on which the decision should be made, and leaf nodes contain a class label to predict.
- Find the best split of samples for each tree node.
- Calculate entropy to measure the purity of a split using the following formula.
E = -Σᵢ₌₁ᴺ pᵢ · log₂ pᵢ, where pᵢ = Nᵢ/Nwhere
Nᵢis the number of occurrences of a specific class label in the labeled data, andNis the number of samples. - Calculate the Information Gain of a split using the following formula.
IG = E_parent - (l/n · E_left + r/n · E_right)where
lis the number of samples in the left child,ris the number of samples in the right child, andn = l + ris the total number of samples.
To predict class labels of unlabeled samples, one needs to traverse the tree according to the information in the nodes and assign the leaf node label.
To validate the algorithm implementation, you will need to use it for some classification tasks. Specifically, you will be given a two-dimensional array of float values x_train as training data, where each sub-array x_train[i] represents a unique case, with one-dimensional array y_train where each element represents the true class label of corresponding sub-array in x_train[i].
FastPrep execution adapter
The source image is cropped before the rest of the callable contract and does not show deterministic split or tie rules. FastPrep considers each non-maximum observed feature value as a threshold, sends values less than or equal to it left, chooses equal-gain splits by smaller feature index and then smaller threshold, stops on non-positive gain, and resolves a tied leaf majority with the smaller label. These are grading-adapter rules, not source-image wording.
Function
decisionTreePredictions(xTrain: double[][], yTrain: int[], xTest: double[][]) → int[]Examples
Example 1
xTrain = [[0], [1], [2], [3]]yTrain = [0, 0, 1, 1]xTest = [[0.5], [2.5]]return = [0, 1]FastPrep-authored runnable example (not shown in the source image): Splitting feature 0 at threshold 1 creates two pure children. The first test sample goes left and the second goes right.
Constraints
- FastPrep execution-adapter constraints (not shown in the source image):
1 <= xTrain.length == yTrain.length <= 120.- Each sample has between
1and8features. - Every training and test row has the same number of features.
- All feature values are finite and every class label is an integer.