For linear regression, we want to see how close our predicted numbers are to the real numbers. The best metrics are Mean Squared Error (MSE) and Root Mean Squared Error (RMSE). They measure the average size of the mistakes our model makes. Smaller values mean better predictions. We also use R-squared to know how much of the data's variation our model explains. A higher R-squared (close to 1) means the model fits well.
First ML prediction (linear regression) in ML Python - Model Metrics & Evaluation
Linear regression predicts continuous values, so confusion matrix (used for categories) does not apply here. Instead, we look at error values like this example:
Actual values: [3, 5, 7, 9]
Predicted values: [2.8, 5.1, 6.9, 9.2]
Errors: [-0.2, 0.1, -0.1, 0.2]
Squared errors: [0.04, 0.01, 0.01, 0.04]
MSE = (0.04+0.01+0.01+0.04)/4 = 0.025
RMSE = sqrt(0.025) = 0.158
Precision and recall are for classification, not regression. For regression, the tradeoff is between bias and variance. A simple model may have high bias (wrong on average) but low variance (stable). A complex model may fit training data perfectly but fail on new data (high variance). We want a balance so errors are low and predictions generalize well.
Example: Predicting house prices. If the model always predicts the average price, it has high bias (misses details). If it fits every training house perfectly but fails on new houses, it has high variance.
Good values:
- MSE and RMSE close to 0 (small errors)
- R-squared close to 1 (explains most variation)
Bad values:
- High MSE or RMSE (large average errors)
- R-squared near 0 or negative (model does not explain data well)
For example, if RMSE is 10, but house prices range from 100 to 200, errors are large. If RMSE is 1, errors are small.
- Using accuracy: Accuracy is for classification, not regression. Using it here is wrong.
- Data leakage: If test data leaks into training, errors look too small and model seems better than it is.
- Overfitting: Very low training error but high test error means model memorizes training data and won't predict well on new data.
- Ignoring scale: MSE depends on the scale of the target variable. Comparing MSE across different problems without scaling is misleading.
No, RMSE of 15 means on average the model is off by 15 units, which is large compared to the price range (100-200). This means predictions are not very accurate and the model needs improvement.
Practice
Solution
Step 1: Understand linear regression purpose
Linear regression aims to find a straight line that best fits the relationship between input and output data.Step 2: Compare with other options
Classification, clustering, and feature reduction are different tasks, not the goal of linear regression.Final Answer:
To find a line that best fits the data points -> Option BQuick Check:
Linear regression = line fitting [OK]
- Confusing regression with classification
- Thinking linear regression clusters data
- Believing it reduces features
Solution
Step 1: Recall model creation syntax
In scikit-learn, you first create the model object with LinearRegression(), without arguments.Step 2: Understand fitting method
The fit method is called on the model object with data, e.g., model.fit(X, y), not during creation.Final Answer:
model = LinearRegression() -> Option AQuick Check:
Model creation uses class name with parentheses [OK]
- Calling fit during model creation
- Passing data to constructor
- Using fit as a class method
X_new = [[5]]?
from sklearn.linear_model import LinearRegression X = [[1], [2], [3], [4]] y = [2, 4, 6, 8] model = LinearRegression() model.fit(X, y) prediction = model.predict([[5]]) print(prediction)
Solution
Step 1: Understand the training data pattern
The output y doubles the input X: y = 2 * X.Step 2: Predict for X_new = 5
Using the pattern, prediction = 2 * 5 = 10.Final Answer:
[10.] -> Option DQuick Check:
2 x 5 = 10 [OK]
- Ignoring the pattern and guessing
- Using last y value as prediction
- Confusing input and output
from sklearn.linear_model import LinearRegression X = [1, 2, 3, 4] y = [2, 4, 6, 8] model = LinearRegression() model.fit(X, y)
Solution
Step 1: Check input shape requirements
scikit-learn expects X as a 2D array (list of lists), but X is a 1D list here.Step 2: Confirm y shape and method usage
y can be 1D list, constructor needs no parameters, fit requires X and y arguments.Final Answer:
X should be a 2D array, not a 1D list -> Option AQuick Check:
X must be 2D for fit() [OK]
- Passing 1D list for X
- Thinking y must be 2D
- Adding parameters to LinearRegression()
y = 3x + 2. After training, you want to predict the output for x = 10. Which code snippet correctly predicts and prints the result?Solution
Step 1: Understand input shape for prediction
scikit-learn expects a 2D array for prediction input, so [[10]] is correct.Step 2: Access prediction output correctly
predict returns an array; to print the single value, use prediction[0].Final Answer:
prediction = model.predict([[10]]) print(prediction[0]) -> Option CQuick Check:
Input 2D array, print first prediction [OK]
- Passing 1D list or scalar to predict
- Printing prediction array directly without indexing
- Passing wrong shaped input with extra values
Ready to prove it?
Pass the test and this pattern moves unknown
