An interviewer asks: "How does a decision tree choose its first question? Write the function that finds the best threshold for one feature." The idea in one sentence: try a threshold in every gap between the feature's sorted values and keep the one whose split has the lowest weighted Gini. A tree with just this one question is a decision stump.
Which thresholds: np.unique(x) gives back each different value of x once, sorted from the smallest. Any cut between two neighbor values splits the rows the same way, so you try one threshold per gap: the middle. For ages 20, 35 and 50 the thresholds are 27.5 and 42.5.
The two groups: x <= threshold is a mask, an array of True and False, and y[mask] keeps the labels where it is True:
age = np.array([20, 50, 35])
label = np.array([0, 1, 1])
print(label[age <= 27.5]) # [0]
print(label[age > 27.5]) # [1 1]
The starting code gives you gini and split_gini from the last lesson, the visits and bought_again of 10 customers, and the frame of best_split(x, y): best_threshold starts as None, best_score as 1.0, and it returns both. Your code must print:
Best threshold: visits <= 8.0
Weighted Gini: 0.167
Left labels: [0 0 0 0 1 0]
Right labels: [1 1 1 1]
Your task: fill in best_split: loop over the gaps between the sorted different values of x, score the middle of each gap with split_gini, and keep the threshold with the lowest score. When two thresholds tie, keep the first one.
Assignment
Follow the Coding Tutorial to fill in best_split one line at a time and find the best question about visits.
Hint
Loop over range(len(values) - 1) so values[i + 1] always exists. Compare with <, not <=, so an equal score doesn't replace the threshold found first.
Free plan: 30 tutor messages. PRO is unlimited.
