For regression tasks, common metrics are Mean Squared Error (MSE), Root Mean Squared Error (RMSE), and R-squared (R²). These metrics measure how close the predicted values are to the actual values. When handling non-linearity, these metrics help us see if the model captures complex patterns better than simple linear regression.
Why advanced regression handles non-linearity in ML Python - Why Metrics Matter
Start learning this pattern below
Jump into concepts and practice - no test required
Regression does not use a confusion matrix. Instead, we look at error values. For example, if actual values are [3, 5, 7] and predicted are [2.8, 5.1, 6.9], errors are small, showing good fit. A simple table of actual vs predicted helps visualize this:
Actual: 3.0 5.0 7.0 Predicted: 2.8 5.1 6.9 Error: 0.2 -0.1 0.1
In regression, the tradeoff is between bias and variance. Simple linear regression has high bias and low variance, so it misses non-linear patterns (underfitting). Advanced regression methods (like polynomial regression, decision trees, or kernel methods) reduce bias by fitting curves but can increase variance (overfitting). The goal is to balance this to capture non-linearity without fitting noise.
Example: Predicting house prices that rise sharply after a certain size. Linear regression misses this curve, advanced regression fits it better but might overfit if too complex.
Good regression model:
- Low MSE or RMSE (errors close to zero)
- High R² (close to 1), meaning predictions explain most of the variation
Bad regression model:
- High MSE or RMSE (large errors)
- Low or negative R², meaning predictions are worse than just guessing the average
Advanced regression models that handle non-linearity usually show better metrics on complex data than simple linear models.
- Ignoring non-linearity: Using linear regression on non-linear data leads to poor fit and misleading metrics.
- Overfitting: Advanced models may fit training data perfectly but fail on new data, causing low test performance.
- Data leakage: Using future or target information in training inflates metrics falsely.
- Relying on a single metric: Always check multiple metrics and visualize predictions to understand model behavior.
Your advanced regression model has an R² of 0.95 on training data but only 0.60 on test data. Is it good at handling non-linearity? Why or why not?
Answer: The model fits training data well, capturing non-linearity, but the drop on test data suggests overfitting. It handles non-linearity but needs better generalization.
Practice
Solution
Step 1: Understand simple linear regression limits
Simple linear regression fits a straight line, so it cannot capture curves or bends in data.Step 2: Recognize advanced regression capabilities
Advanced regression models like decision trees or polynomial regression can fit curves and complex patterns.Final Answer:
Because they can model complex curved relationships in data -> Option CQuick Check:
Advanced regression models handle curves [OK]
- Thinking advanced regression ignores data points
- Believing advanced regression uses fewer data points
- Assuming advanced regression only uses one feature
Solution
Step 1: Identify polynomial feature creation
Polynomial regression requires transforming features using PolynomialFeatures to add powers of features.Step 2: Recognize correct syntax for polynomial transformation
from sklearn.preprocessing import PolynomialFeatures; poly = PolynomialFeatures(degree=2); X_poly = poly.fit_transform(X) correctly imports PolynomialFeatures and transforms X to X_poly for regression.Final Answer:
from sklearn.preprocessing import PolynomialFeatures; poly = PolynomialFeatures(degree=2); X_poly = poly.fit_transform(X) -> Option AQuick Check:
PolynomialFeatures creates polynomial features [OK]
- Confusing decision tree with polynomial regression
- Using clustering models for regression tasks
- Not transforming features before fitting polynomial regression
print(predictions)?
from sklearn.tree import DecisionTreeRegressor X = [[1], [2], [3], [4], [5]] y = [1, 4, 9, 16, 25] model = DecisionTreeRegressor() model.fit(X, y) predictions = model.predict([[6]]) print(predictions)
Solution
Step 1: Understand decision tree prediction behavior
Decision trees predict by assigning the output of the closest training leaf node, not extrapolating beyond training data.Step 2: Check training data and prediction input
Input 6 is beyond training max 5, so prediction will be the leaf value for closest known input, which is 5 with output 25.Final Answer:
[25] -> Option DQuick Check:
Decision tree predicts closest leaf value = 25 [OK]
- Assuming decision tree extrapolates like polynomial regression
- Expecting exact square of 6 (36) as output
- Confusing prediction with training labels
from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures X = [[1], [2], [3], [4]] y = [1, 4, 9, 16] model = LinearRegression() X_poly = PolynomialFeatures(degree=2) model.fit(X_poly, y)
Solution
Step 1: Identify how PolynomialFeatures is used
PolynomialFeatures is a transformer class; it needs to be applied to X using fit_transform to create polynomial features.Step 2: Spot the error in code
Code assigns X_poly to the class instance, not the transformed data. The model.fit expects numeric array, not a class object.Final Answer:
X_poly is a class, not transformed data; need to call fit_transform on X -> Option BQuick Check:
Call fit_transform on X before fitting model [OK]
- Passing transformer class instead of transformed data
- Thinking LinearRegression can't fit polynomial features
- Misunderstanding y shape requirements
Solution
Step 1: Analyze model capabilities for non-linearity
Simple linear regression cannot model curves; decision tree with low depth may underfit; dropping features loses info.Step 2: Evaluate polynomial regression for multiple features
Polynomial regression with degree 3 creates interaction and power terms, capturing complex curves in multiple features.Final Answer:
Polynomial regression of degree 3 can model complex curves with multiple features -> Option AQuick Check:
Degree 3 polynomial regression models complex curves [OK]
- Choosing shallow decision trees that underfit
- Dropping features reduces model power
- Using simple linear regression for curved data
