Module deck
Trees, ensembles and classical models flashcards
Reviewing needs JavaScript. You can read every card in the list below.
Loading your cards...
Back to the module25 cards in this deck
Read all 25 cards as a list
Decision trees, random forests and gradient boosting
- Front
- Define Gini impurity and Gini gain, and compute the gain for (5+, 5-) split into (4+, 1-) and (1+, 4-).
- Back
- Gini = 1 - sum of p_k squared; gain = parent impurity minus the size-weighted child impurity. Parent 0.5, each child 1 - (0.8^2 + 0.2^2) = 0.32, so gain = 0.5 - 0.32 = 0.18. Entropy gain for the same split is 0.2781 bits.
- Front
- What does 'greedy' mean for a decision tree, and what does it miss?
- Back
- Each split is the best one now, with no look-ahead, because finding an optimal tree is NP-complete. On XOR no single split has any gain, yet two levels of splits are perfect, so a tree that only accepts positive gain stops at the root.
- Front
- A tree has 100% training accuracy and the data has 15% label noise. What does that tell you?
- Back
- The best possible accuracy on fresh data is about 85%, so a training score above that means the tree memorised wrong labels. Limit max_depth or min_samples_leaf, or prune with cost-complexity pruning, and choose the setting by validation accuracy.
- Front
- Write the variance of the average of B predictors with variance sigma^2 and pairwise correlation rho. What does each term teach?
- Back
- rho sigma^2 + (1 - rho) sigma^2 / B. The second term shrinks with more trees. The first is a floor that no number of trees removes, so lowering the correlation (bootstrap samples, random feature subsets) is what moves the floor.
- Front
- What does feature subsampling in a random forest buy, and what does it cost?
- Back
- Fewer candidate features per split makes trees less alike (lower rho), which lowers the variance floor. Each tree gets worse (higher variance and bias). In the lesson's six-feature example rho fell from 0.127 to 0.056 but squared bias rose from 0.273 to 0.467, so max_features is tuned.
- Front
- What is out-of-bag error, and why are about 36.8% of rows out of bag?
- Back
- A row is missed by one bootstrap sample with probability (1 - 1/n)^n, which tends to 1/e = 0.368. Predicting each row only with the trees that never saw it gives a validation estimate with no held-out set. It is noisy on small data.
- Front
- Describe gradient boosting in one pass, naming what each tree is fitted to.
- Back
- Start from a constant. Each round fits a small tree to the negative gradient of the loss at the current predictions (the residual y - F for squared error, y - p for log loss) and adds it scaled by the learning rate: F_m = F_(m-1) + eta h_m.
- Front
- How do learning rate, number of rounds and depth interact in boosting?
- Back
- A smaller learning rate takes smaller steps and needs more rounds; given enough rounds it often reaches a lower validation error (in the lesson, rate 0.01 had not caught up with 0.1 by the 800-round cap). Rounds act as regularisation, so choose them by early stopping on validation data, not training loss. Depth sets how many features interact in one tree; shallow trees are the norm.
- Front
- Bagging versus boosting: what is each tree trained on, and what is the failure mode of each?
- Back
- Bagging trains deep trees independently on bootstrap samples and averages them; it cuts variance, and more trees only stop helping. Boosting trains shallow trees in sequence on the remaining errors; it builds up the fit, and too many rounds or too large a step overfits.
- Front
- Why not trust a forest's default feature importance, and what do you use instead?
- Back
- Impurity importance comes from training statistics and favours features with many distinct values; in the lesson, noise columns took about 75% of it. Use permutation importance on held-out data, and permute correlated features together because they dilute each other.
- Front
- How do tree models treat missing values, and why can that beat mean filling?
- Back
- Scikit-learn trees, histogram boosting and XGBoost learn which child missing values go to, so missingness can be a signal. Mean filling puts missing rows among real values and hides it: in the lesson, 0.693 against 0.656 validation accuracy.
- Front
- How should a 50,000-level categorical feature be encoded for a tree ensemble?
- Back
- Prefer native categorical support or cross-fitted target encoding; plain one-hot makes 50,000 sparse columns and naive target encoding leaks the label. Scikit-learn's TargetEncoder cross-fits, and fit_transform should be used on training data.
k-NN, k-means, PCA, SVM and naive Bayes: the classical toolkit
- Front
- How does k-NN classify a point, and what does choosing k trade off?
- Back
- It stores the training data, finds the k closest rows and takes a majority vote. Small k follows noise (high variance; 1-NN scores 100% on its own training set, barring duplicate rows with different labels); large k blurs the boundary (high bias). Choose k on validation data.
- Front
- What is the curse of dimensionality for distance-based methods?
- Back
- As dimension grows, the nearest point's distance approaches the farthest point's (Beyer et al.: seen from about 10 to 15 dimensions in their experiments). In the lesson's random-data run, nearest/farthest was 0.04 at d = 2 and 0.91 at d = 1000. Neighbours then carry little local information, unless the data has low-dimensional structure.
- Front
- Why must columns be scaled before k-NN, and what happens if you do not?
- Back
- Euclidean distance adds squared differences, so a column in large units decides the neighbours. In the lesson, one noise column at 100 times the scale took 5-NN from 0.950 to 0.530, below the 0.607 majority baseline. Fit the scaler on training data only.
- Front
- State the two steps of Lloyd's k-means loop and what it minimises.
- Back
- Assign each row to its nearest centre; move each centre to the mean of its rows; repeat until assignments stop changing. It minimises the inertia J, the sum of squared distances to the assigned centre. J never rises, so the loop converges, but only to a local optimum.
- Front
- What does k-means++ change, and what does it not guarantee?
- Back
- After a random first centre, each next centre is drawn with probability proportional to its squared distance from the nearest chosen centre. It is O(log k)-competitive in expectation. In the lesson it raised good runs from 72% to 87% but its worst run was still 2.4 times the best J, so also restart and keep the lowest J.
- Front
- How do you choose k in k-means, and why not just minimise inertia?
- Back
- Inertia falls as k grows and is zero at k = n. Look for the elbow where the drop slows, and the silhouette s = (b - a) / max(a, b) (between -1 and 1; higher is better separated), then check stability across restarts and usefulness for the decision. There is no rule that always works.
- Front
- Name three ways k-means fails and the fix for each.
- Back
- Non-convex shapes such as rings (purity 0.52 in the lesson): use DBSCAN or single linkage. Unscaled columns: standardise. Outliers (one point at distance 100 took a whole cluster): remove or cap them, or use a method that labels noise. In all three the objective, a sum of squared distances to a mean, is the cause.
- Front
- List the PCA steps and what the explained variance ratio is.
- Back
- Centre the columns, form the covariance matrix (divide by n - 1), take its eigenvectors sorted by eigenvalue, project onto the top k. Each component's explained variance is its eigenvalue; the ratio is that eigenvalue over the sum of all eigenvalues. Example: covariance rows (2, 1) and (1, 2) has eigenvalues 3 and 1, ratios 3/4 and 1/4.
- Front
- When does PCA mislead?
- Back
- Units: an unscaled big-unit column takes PC1 (0.999998 of the variance in the lesson). Quiet signal: PCA never sees labels, so a class signal in a low-variance direction is dropped (PC1 only 0.508, PC2 only 1.000). Curved structure: PCA is linear. Readability: components mix all columns.
- Front
- State the linear SVM objective and what C does.
- Back
- Minimise 1/2 ||w||^2 + C times the sum of hinge losses max(0, 1 - y f(x)). The first term widens the margin (width 2/||w||); the hinge charges points inside it. Low C gives a wide, smooth margin; high C a narrow one that fits training points. Points beyond margin 1 have zero loss. SVMs are not scale invariant.
- Front
- What is the kernel trick, in one example?
- Back
- A kernel computes the inner product of lifted features without building them. For two columns, (x . z)^2 = x1^2 z1^2 + 2 x1 x2 z1 z2 + x2^2 z2^2, the dot product of the lifted vectors (x1^2, sqrt2 x1 x2, x2^2). It lets a linear SVM separate a disc (0.604 linear, 0.978 with x1^2 + x2^2 added).
- Front
- State the naive Bayes assumption and why it still classifies well.
- Back
- Features are independent given the class, so P(y | x) is proportional to P(y) times the product of P(x_j | y). Classification needs only which class scores higher. Duplicating every feature 3 times left accuracy at 0.800 but raised confidence from 0.810 to 0.928: decisions survive, probabilities do not. One dominant duplicate can break it (0.650).
- Front
- What do a Gaussian mixture, Isolation Forest and t-SNE add to k-means and PCA, and what is the catch of each?
- Back
- GMM fits Gaussians with their own covariance by EM (soft assignments, local optimum, BIC for the number of components). Isolation Forest scores points by how few random splits isolate them (shorter path means more anomalous). t-SNE draws 2-3 D maps only: seed-dependent, no transform for new rows, so its islands are not features.