Thursday, October 16, 2025

Top 10 Python Linear Regression MCQs with Answers | Data Science Interview


Top 10 Python Linear Regression MCQs with Answers | Data Science Interview


1. Which Python library is most commonly used for implementing simple and multiple linear regression?

A. NumPy
B. scikit-learn
C. matplotlib
D. pandas

Answer:  B

Explanation: scikit-learn provides LinearRegression() for fitting linear models easily in Python.



2. In scikit-learn, after fitting a LinearRegression model, which attribute gives the coefficients?

A. model.predict()
B. model.score()
C. model.coef_
D. model.intercept_

Answer:  C

Explanation: model.coef_ contains the slope(s) for all independent variables, while model.intercept_ gives the bias term.



3. What is the purpose of train_test_split() in linear regression implementation?

A. To split features into numerical and categorical
B. To divide the dataset into training and testing sets
C. To normalize the dataset
D. To compute residuals

Answer:  B

Explanation: train_test_split() ensures model evaluation on unseen data, which helps detect overfitting.


4. Why do we often use StandardScaler() or MinMaxScaler() before applying linear regression?

A. To improve gradient descent convergence
B. Linear regression requires normalized residuals
C. To reduce heteroscedasticity automatically
D. To visualize data better

Answer:  A

Explanation: Scaling features improves numerical stability and speeds up convergence for algorithms like gradient descent.



5. In Python, which function calculates R² score for a fitted linear regression model?

A. r2_score(y_true, y_pred)
B. mean_squared_error(y_true, y_pred)
C. np.corrcoef(y, y_pred)
D. score()

Answer:  A

Explanation: sklearn.metrics.r2_score() computes the proportion of variance explained. You can also use model.score(X_test, y_test) in scikit-learn.



6. What is the shape of X when using scikit-learn’s LinearRegression for multiple features?

A. (n_samples,)
B. (n_samples, n_features)
C. (n_features, n_samples)
D. (n_features,)

Answer:  B

Explanation: scikit-learn expects 2D input: rows = samples, columns = features. For a single feature, X must be reshaped as (n_samples, 1).



7. When implementing linear regression in Python using gradient descent manually, which of the following must be computed in each iteration?

A. Residuals only
B. Partial derivatives of the cost function w.r.t coefficients
C. R² score
D. Train-test split

Answer:  B

Explanation: Gradient descent updates coefficients by computing gradients of the cost function (MSE) with respect to β. It iteratively adjusts each coefficient in the direction that reduces the error, continuing until the model converges to the minimum MSE.



8. Which of the following commands adds a bias (intercept) term when using NumPy for manual linear regression?

A. X = np.append(X, 1)
B. X = np.ones((n,1))
C. X = np.c_[np.ones((n,1)), X]
D. X = np.concatenate(X)

Answer:  C

Explanation: np.c_ concatenates a column of ones to X, representing the intercept term in the normal equation.



9. If y_pred = model.predict(X_test) in scikit-learn, how do you compute Mean Squared Error?

A. mse = np.mean(y_test - y_pred)
B. mse = np.mean((y_test - y_pred)**2)
C. mse = model.score(X_test, y_test)
D. mse = np.sqrt(np.sum(y_test - y_pred))

Answer:  B

Explanation: MSE is the mean of squared differences between actual and predicted values.



10. Which Python function/method can be used to detect multicollinearity before fitting a linear regression model?

A. model.score()
B. np.corrcoef() or Variance Inflation Factor (VIF)
C. train_test_split()
D. model.predict()

Answer:  B

Explanation: Correlation matrix or VIF helps detect highly correlated independent variables that may destabilize regression coefficients.





 

No comments:

Post a Comment

Featured Content

Multiple choice questions in Natural Language Processing Home

MCQ in Natural Language Processing, Quiz questions with answers in NLP, Top interview questions in NLP with answers Multiple Choice Que...

All time most popular contents