AI-ML interleaved practice
Instructions: Solve each problem showing all work. These problems deliberately mix subtopics — read carefully and choose the correct method. Do not assume consecutive problems share a technique. Total: 62 marks. Use notation for math.
1. (6 marks) A feature column has values . Using the IQR rule, determine whether is an outlier. Show , , IQR, and the upper fence.
2. (5 marks) A dataset predicts house price. It contains columns: zip_code, bedrooms, satisfaction_rating (values: poor/average/good/excellent), and review_text. Classify each column as numerical, categorical, ordinal, or text. Justify the ambiguous one.
3. (7 marks) Given the two-point dataset , derive the OLS slope and intercept using the closed-form formulas. State the fitted line.
4. (6 marks) A color column has values {red, green, blue}. Explain why label encoding is inappropriate here for a linear model, and give the one-hot representation. State how many columns one-hot produces if you drop one to avoid the dummy trap.
5. (7 marks) Standardize (z-score) the value from a feature with mean and standard deviation . Then min-max scale the same value given feature range . Show both results and state which transformation preserves the shape of the distribution.
6. (6 marks) You are asked to prevent data leakage. A colleague fits a StandardScaler on the entire dataset, then splits into train/test. Explain precisely what leaks, and give the correct ordering of operations.
7. (8 marks) For the multiple regression normal equation , with compute . Show , its inverse, and .
8. (5 marks) A binary classification dataset has 950 negatives and 50 positives. Name two resampling strategies to address imbalance, and explain why plain accuracy is a misleading metric here.
9. (6 marks) A model has with samples and predictors. Compute the adjusted . State why adjusted can decrease when adding a useless feature.
Answer keyMark scheme & solutions
1. — Tests 2.1.3 (Outlier detection). Why: presence of one extreme value + "IQR rule" signals boxplot-fence method, not z-score.
Sorted: (). Median = . Lower half → . Upper half → . . Upper fence . Since , 88 is an outlier.
2. — Tests 2.1.1 (Types of data).
zip_code: categorical (numeric-looking but no meaningful arithmetic order).bedrooms: numerical (discrete count).satisfaction_rating: ordinal (ordered categories poor < average < good < excellent).review_text: text. The ambiguous one iszip_code: though stored as digits, differences/means are meaningless, so treat as categorical.
3. — Tests 2.2.3 (OLS derivation). Why: "derive with closed-form" → OLS slope/intercept formulas, not gradient descent.
, . . . Fitted line: .
4. — Tests 2.1.6 (One-hot vs label encoding). Why: nominal unordered categories → one-hot; label encoding imposes a false order.
Label encoding red=0, green=1, blue=2 implies blue > green > red numerically, misleading a linear model. One-hot:
| red | green | blue |
|---|---|---|
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 1 |
| Dropping one column to avoid the dummy-variable trap → 2 columns. |
5. — Tests 2.1.5 (Min-max & z-score). Why: two named scalings must be distinguished.
Z-score: . Min-max: . Standardization (z-score) preserves the distribution shape (linear shift/scale); min-max also linear but bounds to . Both are linear, but z-score is preferred when shape/outlier sensitivity matters.
6. — Tests 2.1.13 (Data leakage). Why: fitting a scaler before splitting leaks test statistics.
What leaks: the scaler's mean and std are computed using test-set values, so information about the test distribution enters training. Correct order: (1) split into train/test, (2) fit scaler on train only, (3) transform train, (4) transform test with the train-fitted scaler.
7. — Tests 2.2.5 (Normal equation). Why: explicit → closed-form matrix solve.
, . . . . So , .
8. — Tests 2.1.11 (Imbalanced data). Strategies: SMOTE (synthetic minority oversampling) and random undersampling of the majority. Accuracy misleads because predicting all-negative yields accuracy while catching zero positives; use precision/recall/F1/ROC-AUC instead.
9. — Tests 2.2.8 (Adjusted ). Adjusted penalizes extra predictors: adding a useless feature raises , increasing , which lowers adjusted unless the feature improves enough.
[
{"claim":"IQR upper fence for problem 1 is 19.5 and 88 is an outlier","code":"data=sorted([12,14,15,13,88,14,11]); Q1=12; Q3=15; iqr=Q3-Q1; fence=Q3+1.5*iqr; result=(fence==19.5) and (88>fence)"},
{"claim":"Normal equation beta = [2/3, 1/2]","code":"import numpy as np; X=np.array([[1,1],[1,2],[1,3]]); y=np.array([1,2,2]); b=np.linalg.inv(X.T@X)@X.T@y; result=bool(np.allclose(b,[2/3,1/2]))"},
{"claim":"Adjusted R2 approx 0.8420","code":"R2=0.85; n=100; p=5; adj=1-(1-R2)*(n-1)/(n-p-1); result=abs(adj-0.8420)<0.001"}
]