GNGiseldo Neo and Alana NeoBooks
Linear Regression with Python11 / 19

Improving the Model

Improving the performance of a linear regression model involves various strategies that help increase accuracy, generalization, and interpretability. This chapter explores key techniques for enhancing regression models.

10.1 Feature Engineering

Feature engineering is the process of creating new variables from raw data to improve the predictive power of the model.

10.1.1 Common Feature Engineering Techniques

Mathematical Transformations

Transformations such as logarithm, square root, and powers can help linearize nonlinear relationships.

import numpy as np
df['log_feature'] = np.log(df['feature'] + 1)

Interactions Between Variables

Creating interaction variables, where multiple variables are multiplied together to capture combined effects.

df['interaction'] = df['feature1'] * df['feature2']

Binning and Categorization

Creating categorical or binary variables from continuous variables.

df['binned'] = pd.cut(df['feature'], bins=5, labels=False)

10.2 Regularization (Lasso and Ridge)

Regularization is a technique used to prevent overfitting by adding a penalty to the magnitude of the regression coefficients.

10.2.1 Types of Regularization

Ridge Regression

Adds an L2 penalty to the sum of squared coefficients. Useful for dealing with multicollinearity.

Cost=∑(yi−y^i)2+λ∑βj2\textit{Cost} = \sum (y_i - \hat{y}_i)^2 + \lambda \sum \beta_j^2
from sklearn.linear_model import Ridge
ridge = Ridge(alpha=1.0)
ridge.fit(x_train, y_train)

Lasso Regression

Adds an L1 penalty, which can shrink some coefficients to exactly zero, effectively performing feature selection.

Cost=∑(yi−y^i)2+λ∑∣βj∣\textit{Cost} = \sum (y_i - \hat{y}_i)^2 + \lambda \sum |\beta_j|
from sklearn.linear_model import Lasso
lasso = Lasso(alpha=0.1)
lasso.fit(x_train, y_train)

Elastic Net

Combines L1 and L2 penalties, making it useful when there are many correlated variables.

from sklearn.linear_model import ElasticNet
elastic_net = ElasticNet(alpha=1.0, l1_ratio=0.5)
elastic_net.fit(x_train, y_train)

10.3 Variable Selection

Variable selection is the process of identifying and retaining the most relevant variables for the model while removing those that do not contribute significantly.

10.3.1 Variable Selection Methods

Sequential Selection

  • Forward Selection: Starts with no predictors and adds variables one by one, evaluating the model at each addition.

  • Backward Elimination: Starts with all variables and removes them one at a time, choosing the one that has the least impact on the model.

from sklearn.feature_selection import RFE
from sklearn.linear_model import LinearRegression

model = LinearRegression()
rfe = RFE(model, n_features_to_select=3)
rfe.fit(x_train, y_train)

Akaike Information Criterion (AIC) and Bayesian Information Criterion (BIC)

Used to evaluate the quality of statistical models by penalizing complexity.

Cross-Validation

Evaluates model performance across multiple subsets of the data to ensure that the model generalizes well to unseen data.

from sklearn.model_selection import cross_val_score

scores = cross_val_score(model, x_train, y_train, cv=5)
print("Mean accuracy: ", scores.mean())