Split data, fit linear regression on the training rows, and predict the test set

Quick Overview

Practice splitting a dataset with a fixed seed, fitting scikit-learn linear regression on the training rows only, and predicting the held-out test rows. It tests the return order of train_test_split, reproducible splits, and keeping test data out of model fitting.

Split data, fit linear regression on the training rows, and predict the test set

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `train_test_predict(X: np.ndarray, y: np.ndarray) -> np.ndarray`. It holds out a test set, fits ordinary least squares (OLS) with an intercept on the training rows only, and returns predictions for the test rows. The function must: - Split with `train_test_split(X, y, test_size=0.25, random_state=42)`, leaving `shuffle` at its default `True`. - Fit `sklearn.linear_model.LinearRegression` (intercept fitted) on the training rows only. - Return predictions for the test rows in test-set row order, as a 1-D `np.ndarray` of shape `(10,)`. ```hint Unpacking the split Check the order in which `train_test_split` returns its four arrays before you assign them to names. ``` ### Constraints & Assumptions - The grader passes a float `X` of shape `(40, 3)` and a `y` of shape `(40,)`. - With 40 rows and `test_size=0.25`, the split holds out 10 test rows and keeps 30 for training. ### What a Strong Answer Covers - Uses the exact split arguments, so the partition matches the grader's. - Unpacks the four split outputs in the right order. - Fits the model on the training rows only, so no test row affects the coefficients. - Returns a 1-D array of test predictions in the order the split produced them. ### Follow-up Questions - How would you report the model's error on the test set? - Why does `random_state=42` matter here? - If you added feature scaling before the regression, which rows would you fit the scaler on?

Overview: Practice splitting a dataset with a fixed seed, fitting scikit-learn linear regression on the training rows only, and predicting the held-out test rows. It tests the return order of train_test_split, reproducible splits, and keeping test data out of model fitting.

General logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement train_test_predict(X: np.ndarray, y: np.ndarray) -> np.ndarray. It holds out a test set, fits ordinary least squares (OLS) with an intercept on the training rows only, and returns predictions for the test rows.

The function must:

  • Split with train_test_split(X, y, test_size=0.25, random_state=42) , leaving shuffle at its default True .
  • Fit sklearn.linear_model.LinearRegression (intercept fitted) on the training rows only.
  • Return predictions for the test rows in test-set row order, as a 1-D np.ndarray of shape (10,) .

Constraints & Assumptions

  • The grader passes a float X of shape (40, 3) and a y of shape (40,) .
  • With 40 rows and test_size=0.25 , the split holds out 10 test rows and keeps 30 for training.

What a Strong Answer Covers Guidance

  • Uses the exact split arguments, so the partition matches the grader's.
  • Unpacks the four split outputs in the right order.
  • Fits the model on the training rows only, so no test row affects the coefficients.
  • Returns a 1-D array of test predictions in the order the split produced them.

Follow-up Questions Guidance

  • How would you report the model's error on the test set?
  • Why does random_state=42 matter here?
  • If you added feature scaling before the regression, which rows would you fit the scaler on?