Complete decision tree and gradient descent functions
Task A) Decision tree split function (classification)
You are building a binary decision tree classifier for tabular numeric features.
Implement:
gini(y)that computes the Gini impurity of labelsy(binary labels 0/1).best_split(X, y)that finds the best(feature_index, threshold)to split on.
Definitions/requirements:
-
Xis ann x dmatrix of floats. -
yis lengthn, values in{0,1}. -
A split is defined by
(j, t)meaning:- left child: samples with
X[i][j] <= t - right child: samples with
X[i][j] > t
- left child: samples with
-
Choose the split that minimizes weighted Gini impurity:
G_split = (n_L / n) * G(y_L) + (n_R / n) * G(y_R) -
If a split would produce an empty side, it is invalid.
-
Return
Noneif no valid split exists.
Constraints:
1 <= n <= 2e4,1 <= d <= 50- Aim for
~ O(nd log n)or better.
Task B) Single-step gradient descent update (linear regression)
You are fitting linear regression with mean squared error:
L(w,b) = (1/n) * sum_{i=1 to n} (w^T x_i + b - y_i)^2
Implement:
gd_step(X, y, w, b, lr)that performs one gradient descent update and returns(new_w, new_b).
Requirements:
Xisn x d,ylengthn(real-valued).wlengthd,bscalar.- Use the correct gradients for MSE (include the averaging over
n). - Must work for
n=1andd=1.
General Notes
- You may assume Python-like pseudocode and basic numeric operations (no need for full training loop).
- Handle edge cases cleanly (constant labels, identical feature values, etc.).
AI interview prep
Core dilemma
The central engineering conflict is balancing the mathematical correctness and numerical stability of fundamental machine learning algorithms (Gini impurity, gradient descent) with the computational efficiency required for processing potentially large datasets, while robustly handling various edge cases.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Complete decision tree and gradient descent functions
Task A) Decision tree split function (classification)
You are building a binary decision tree classifier for tabular numeric features.
Implement:
gini(y)that computes the Gini impurity of labelsy(binary labels 0/1).best_split(X, y)that finds the best(feature_index, threshold)to split on.
Definitions/requirements:
-
Xis ann x dmatrix of floats. -
yis lengthn, values in{0,1}. -
A split is defined by
(j, t)meaning:- left child: samples with
X[i][j] <= t - right child: samples with
X[i][j] > t
- left child: samples with
-
Choose the split that minimizes weighted Gini impurity:
G_split = (n_L / n) * G(y_L) + (n_R / n) * G(y_R) -
If a split would produce an empty side, it is invalid.
-
Return
Noneif no valid split exists.
Constraints:
1 <= n <= 2e4,1 <= d <= 50- Aim for
~ O(nd log n)or better.
Task B) Single-step gradient descent update (linear regression)
You are fitting linear regression with mean squared error:
L(w,b) = (1/n) * sum_{i=1 to n} (w^T x_i + b - y_i)^2
Implement:
gd_step(X, y, w, b, lr)that performs one gradient descent update and returns(new_w, new_b).
Requirements:
Xisn x d,ylengthn(real-valued).wlengthd,bscalar.- Use the correct gradients for MSE (include the averaging over
n). - Must work for
n=1andd=1.
General Notes
- You may assume Python-like pseudocode and basic numeric operations (no need for full training loop).
- Handle edge cases cleanly (constant labels, identical feature values, etc.).