Loading W Code...
5
Topics
sklearn
Implementation
🌾 Indian Context
We model crop yield vs rainfall — showing the classic inverted-U pattern where moderate rainfall gives maximum yield, while drought and floods both reduce it!
Foundation
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import r2_score
# Generate curved data: y = 0.5x² + x + 2 + noise
np.random.seed(42)
X = np.linspace(-3, 3, 50).reshape(-1, 1)
y_true = 0.5 * X**2 + X + 2
y = y_true.ravel() + np.random.randn(50) * 0.5
print("=" * 60)
print("📈 LINEAR vs POLYNOMIAL REGRESSION")
print("=" * 60)
# 1. Linear Regression (Will fail on curved data!)
lin_reg = LinearRegression()
lin_reg.fit(X, y)
y_pred_linear = lin_reg.predict(X)
r2_linear = r2_score(y, y_pred_linear)
print(f"\n❌ Linear Regression:")
print(f" R² Score: {r2_linear:.4f}")
print(f" Equation: ŷ = {lin_reg.coef_[0]:.2f}x + {lin_reg.intercept_:.2f}")
# 2. Polynomial Regression (Degree 2 - perfect for quadratic!)
# Step 1: Transform X into [1, x, x²]
poly = PolynomialFeatures(degree=2)
X_poly = poly.fit_transform(X)
print(f"\n📐 Feature Transformation:")
print(f" Original shape: {X.shape} → Transformed: {X_poly.shape}")
print(f" Features: [1, x, x²]")
# Step 2: Fit Linear Regression on polynomial features
poly_reg = LinearRegression()
poly_reg.fit(X_poly, y)
y_pred_poly = poly_reg.predict(X_poly)
r2_poly = r2_score(y, y_pred_poly)
print(f"\n✅ Polynomial Regression (Degree 2):")
print(f" R² Score: {r2_poly:.4f}")
coefs = poly_reg.coef_
intercept = poly_reg.intercept_
print(f" Equation: ŷ = {coefs[2]:.2f}x² + {coefs[1]:.2f}x + {intercept:.2f}")
print(f"\n💡 Improvement: R² increased from {r2_linear:.2f} to {r2_poly:.2f}!")