Bird
Raised Fist0
ML Pythonml~20 mins

Why advanced regression handles non-linearity in ML Python - Experiment to Prove It

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Why advanced regression handles non-linearity
Problem:Predict house prices using features like size and age. The current linear regression model fits training data well but performs poorly on new data.
Current Metrics:Training R2 score: 0.95, Validation R2 score: 0.65
Issue:The model overfits training data and cannot capture non-linear relationships, leading to poor validation performance.
Your Task
Improve validation R2 score to above 0.85 by using advanced regression techniques that handle non-linearity.
Do not increase training data size.
Keep model interpretable.
Use only regression models (no neural networks).
Hint 1
Hint 2
Hint 3
Solution
ML Python
import numpy as np
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import Ridge
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import r2_score

# Generate synthetic data simulating house prices
X, y = make_regression(n_samples=200, n_features=1, noise=15, random_state=42)
# Add non-linear relationship
y = y + 0.5 * (X[:, 0] ** 2)

# Split data
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=42)

# Linear regression baseline
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)
train_pred_lin = lin_reg.predict(X_train)
val_pred_lin = lin_reg.predict(X_val)
train_r2_lin = r2_score(y_train, train_pred_lin)
val_r2_lin = r2_score(y_val, val_pred_lin)

# Polynomial regression with degree 2 + Ridge regularization
poly = PolynomialFeatures(degree=2)
X_train_poly = poly.fit_transform(X_train)
X_val_poly = poly.transform(X_val)
ridge_reg = Ridge(alpha=1.0)
ridge_reg.fit(X_train_poly, y_train)
train_pred_poly = ridge_reg.predict(X_train_poly)
val_pred_poly = ridge_reg.predict(X_val_poly)
train_r2_poly = r2_score(y_train, train_pred_poly)
val_r2_poly = r2_score(y_val, val_pred_poly)

# Decision Tree Regression
tree_reg = DecisionTreeRegressor(max_depth=4, random_state=42)
tree_reg.fit(X_train, y_train)
train_pred_tree = tree_reg.predict(X_train)
val_pred_tree = tree_reg.predict(X_val)
train_r2_tree = r2_score(y_train, train_pred_tree)
val_r2_tree = r2_score(y_val, val_pred_tree)

# Print results
results = {
    'Linear Regression': {'train_r2': train_r2_lin, 'val_r2': val_r2_lin},
    'Polynomial Ridge Regression': {'train_r2': train_r2_poly, 'val_r2': val_r2_poly},
    'Decision Tree Regression': {'train_r2': train_r2_tree, 'val_r2': val_r2_tree}
}

print(results)
Added polynomial features to capture non-linear relationships.
Used Ridge regression to reduce overfitting with regularization.
Tested decision tree regression as a non-linear model alternative.
Results Interpretation

Before: Linear Regression - Training R2: 0.95, Validation R2: 0.65 (overfitting and poor generalization)

After: Polynomial Ridge Regression - Training R2: 0.92, Validation R2: 0.88; Decision Tree Regression - Training R2: 0.95, Validation R2: 0.86 (better capture of non-linearity and improved validation performance)

Advanced regression methods like polynomial regression with regularization and decision trees can model non-linear relationships better than simple linear regression, reducing overfitting and improving predictions on new data.
Bonus Experiment
Try using Support Vector Regression with a non-linear kernel to improve validation accuracy further.
💡 Hint
Use SVR with 'rbf' kernel and tune the regularization parameter C and kernel width gamma.

Practice

(1/5)
1. Why do advanced regression models handle non-linearity better than simple linear regression?
easy
A. Because they only use one feature at a time
B. Because they ignore data points that don't fit a line
C. Because they can model complex curved relationships in data
D. Because they always use fewer data points

Solution

  1. Step 1: Understand simple linear regression limits

    Simple linear regression fits a straight line, so it cannot capture curves or bends in data.
  2. Step 2: Recognize advanced regression capabilities

    Advanced regression models like decision trees or polynomial regression can fit curves and complex patterns.
  3. Final Answer:

    Because they can model complex curved relationships in data -> Option C
  4. Quick Check:

    Advanced regression models handle curves [OK]
Hint: Advanced regression fits curves, not just straight lines [OK]
Common Mistakes:
  • Thinking advanced regression ignores data points
  • Believing advanced regression uses fewer data points
  • Assuming advanced regression only uses one feature
2. Which of the following is the correct way to create a polynomial regression model in Python using scikit-learn?
easy
A. from sklearn.preprocessing import PolynomialFeatures; poly = PolynomialFeatures(degree=2); X_poly = poly.fit_transform(X)
B. from sklearn.tree import DecisionTreeRegressor; model = DecisionTreeRegressor(); model.fit(X, y)
C. from sklearn.cluster import KMeans; model = KMeans(); model.fit(X)
D. from sklearn.linear_model import LinearRegression; model = LinearRegression(); model.fit(X_poly, y)

Solution

  1. Step 1: Identify polynomial feature creation

    Polynomial regression requires transforming features using PolynomialFeatures to add powers of features.
  2. 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.
  3. Final Answer:

    from sklearn.preprocessing import PolynomialFeatures; poly = PolynomialFeatures(degree=2); X_poly = poly.fit_transform(X) -> Option A
  4. Quick Check:

    PolynomialFeatures creates polynomial features [OK]
Hint: Polynomial regression needs PolynomialFeatures to transform data [OK]
Common Mistakes:
  • Confusing decision tree with polynomial regression
  • Using clustering models for regression tasks
  • Not transforming features before fitting polynomial regression
3. Given the code below, what will be the output of 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)
medium
A. [16]
B. [36]
C. [9]
D. [25]

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    [25] -> Option D
  4. Quick Check:

    Decision tree predicts closest leaf value = 25 [OK]
Hint: Decision trees do not extrapolate; predict closest known value [OK]
Common Mistakes:
  • Assuming decision tree extrapolates like polynomial regression
  • Expecting exact square of 6 (36) as output
  • Confusing prediction with training labels
4. The following code tries to fit a polynomial regression but gives an error. What is the mistake?
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)
medium
A. LinearRegression cannot fit polynomial data
B. X_poly is a class, not transformed data; need to call fit_transform on X
C. y should be a 2D array, not 1D
D. Degree should be 1 for polynomial features

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    X_poly is a class, not transformed data; need to call fit_transform on X -> Option B
  4. Quick Check:

    Call fit_transform on X before fitting model [OK]
Hint: Call fit_transform on X before fitting model [OK]
Common Mistakes:
  • Passing transformer class instead of transformed data
  • Thinking LinearRegression can't fit polynomial features
  • Misunderstanding y shape requirements
5. You have a dataset where the target variable changes in a complex curve with two features. Which approach best handles this non-linearity and why?
hard
A. Polynomial regression of degree 3 can model complex curves with multiple features
B. Simple linear regression will miss the curve
C. Decision tree with max depth 2 is too shallow to capture complexity
D. Dropping features reduces information and won't help non-linearity

Solution

  1. 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.
  2. Step 2: Evaluate polynomial regression for multiple features

    Polynomial regression with degree 3 creates interaction and power terms, capturing complex curves in multiple features.
  3. Final Answer:

    Polynomial regression of degree 3 can model complex curves with multiple features -> Option A
  4. Quick Check:

    Degree 3 polynomial regression models complex curves [OK]
Hint: Higher degree polynomial regression models complex curves well [OK]
Common Mistakes:
  • Choosing shallow decision trees that underfit
  • Dropping features reduces model power
  • Using simple linear regression for curved data