I N F O A R Y A N

Best 10 Regression Methods in ML - Python - SkLearn

In this blog, we will embark on a journey through various regression techniques using Python’s Scikit-Learn library. We will go through normal and easy techniques to very complex non-linear methods like boosting and bagging in this blog post. Also, we will discover the strength and weaknesses of these models.

You may also want to explore Automated EDA, Transfer Learning using Regression, Stratified K-Fold or Performance Metrics.

Flow of Blog:

  1. Description of 10 Regression Techniques 
  2. Advantages and Disadvantages
  3. Conclusion of these models
  4. Information on advance approaches
  5. How to select best model?

 

1. Linear Regression

Linear regression is a simple yet powerful model that assumes a linear relationship between the input features and the output variable.

Equation/Parameters: y=mx+b(for simple linear regression), where is the slope and is the intercept.

Python Code:

from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train, y_train)

Pros and Cons:

  • Strengths: Easy to understand, quick to implement, and performs well when the relationship is approximately linear.
  • Weaknesses: May struggle with complex relationships, sensitive to outliers.

 

2. Ridge Regression

Ridge regression is an extension of linear regression that introduces regularization to prevent overfitting.

Equation/Parameters: The model includes a regularization term, which is a penalty added to the sum of the squared coefficients.

Python Code:

from sklearn.linear_model import Ridge
model = Ridge(alpha=1.0)
model.fit(X_train, y_train)

Pros and Cons:

  • Strengths: Effective in handling multicollinearity, reduces the impact of irrelevant features.
  • Weaknesses: Requires tuning of the regularization parameter, may not perform well if all features are relevant.

 

3. Lasso Regression:

Lasso regression is another regularization technique that, like Ridge, aims to prevent overfitting. It introduces a penalty term based on the absolute values of the coefficients.

Equation/Parameters: The loss function includes a penalty term α×∑∣wi∣, where wiare the regression coefficients.

Python Code:

from sklearn.linear_model import Lasso
model = Lasso(alpha=0.1)
model.fit(X_train, y_train)

Pros and Cons:

  • Strengths: Performs feature selection, useful for high-dimensional datasets.
  • Weaknesses: May not work well if there are many relevant features.

 

4. Elastic Net Regression:

Description: Elastic Net regression combines both L1 (Lasso) and L2 (Ridge) regularization terms, providing a balance between variable selection and regularization.

Equation/Parameters: Combines the L1 and L2 penalty terms in the loss function.

Python Code:

from sklearn.linear_model import ElasticNet
model = ElasticNet(alpha=0.1, l1_ratio=0.5)
model.fit(X_train, y_train)

Pros and Cons:

  • Strengths: Addresses limitations of Lasso and Ridge, suitable for datasets with many features.
  • Weaknesses: Requires tuning of both alpha and l1_ratio parameters.

 

5. K-Nearest Neighbors Regression:

Description: K-Nearest Neighbors (KNN) is a non-parametric regression method that predicts the target variable based on the average of its k-nearest neighbors. An object is classified by a plurality vote of its neighbors, with the object being assigned to the class most common among its k nearest neighbors.

Equation/Parameters: No explicit equation, prediction is based on the average of the target variable of k-nearest neighbors.

Python Code:

from sklearn.neighbors import KNeighborsRegressor
model = KNeighborsRegressor(n_neighbors=5)
model.fit(X_train, y_train)

Pros and Cons:

  • Strengths: Captures complex relationships, doesn’t assume a specific functional form.
  • Weaknesses: Sensitive to outliers, computationally expensive for large datasets.
  •  

6. Support Vector Regression (SVR):

SVR extends support vector machines to regression tasks by finding a hyperplane that best fits the data within a certain margin.

Equation/Parameters: Involves a loss function and a regularization term.

Python Code:

from sklearn.svm import SVR
model = SVR(kernel=’linear’)
model.fit(X_train, y_train)

Pros and Cons:

  • Strengths: Effective in high-dimensional spaces, robust to outliers.
  • Weaknesses: Requires tuning of parameters, sensitive to the choice of kernel.

 

7. Decision Tree Regression:

Decision tree builds a tree structure to represent the relationship between features and the target variable.

Equation/Parameters: No explicit equation, the tree structure determines the decision-making process.

Python Code:

from sklearn.svm import SVR
model = SVR(kernel=’linear’)
model.fit(X_train, y_train)

Pros and Cons:

  • Strengths: Can capture complex relationships, handles non-linearity well.
  • Weaknesses: Prone to overfitting, sensitive to small variations in the data.

 

8. Random Forest Regression:

Random forest is an ensemble technique that builds multiple decision trees and combines their predictions.

Equation/Parameters: Aggregation of predictions from multiple decision trees.

Python Code:

from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
model.fit(X_train, y_train)

Pros and Cons:

  • Strengths: Reduces overfitting, handles large datasets well, provides feature importance.
  • Weaknesses: Complexity and lack of interpretability.

 

9. Gradient Boosting Regression:

Gradient Boosting builds an ensemble of weak learners (usually decision trees) sequentially, with each tree correcting the errors of the previous ones.

Equation/Parameters: Combines the predictions of multiple weak learners with a weighted sum.

Python Code:

from sklearn.ensemble import GradientBoostingRegressor
model = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1)
model.fit(X_train, y_train)

Pros and Cons:

  • Strengths: High predictive accuracy, handles non-linearity and interactions well.
  • Weaknesses: May be prone to overfitting, requires careful tuning.

 

10. XGBoost Regression:

XGBoost is an optimized and scalable version of gradient boosting, known for its efficiency and performance.

Equation/Parameters: Similar to gradient boosting but with additional regularization terms.

Python Code:

from xgboost import XGBRegressor
model = XGBRegressor(n_estimators=100, learning_rate=0.1)
model.fit(X_train, y_train)

Pros and Cons:

  • Strengths: Fast and scalable, handles missing data, effective for a wide range of datasets.
  • Weaknesses: Requires careful parameter tuning, potential for overfitting.

 

Conclusion :

In the expansive landscape of machine learning, Python offers a versatile array of regression models for predicting numerical outcomes. Linear regression, a fundamental technique, assumes a linear relationship between input features and the target variable, providing simplicity and interpretability. Regularized versions like Ridge and Lasso mitigate overfitting concerns, with Lasso excelling in feature selection. Decision tree and its ensemble counterpart, Random Forest, embrace non-linear relationships and are robust in handling complex data patterns. For those seeking a balance between Ridge and Lasso, Elastic Net regression integrates both regularization techniques.

 

Note for advance approaches

Moving beyond traditional approaches, K-Nearest Neighbors doesn’t assume a specific functional form, relying on the proximity of neighbors for predictions. Gradient Boosting and XGBoost, formidable ensemble methods, iteratively build models to correct errors, offering high predictive accuracy with scalability. As we traverse this diverse array of regression models in Python, each technique contributes distinct strengths—be it simplicity, regularization, non-linearity capture, or ensemble learning.

 

How to select the best model ?

In selecting the best regression model in Python, considerations of dataset characteristics, interpretability, and computational efficiency come into play. While linear regression is a starting point for its simplicity, complex relationships may call for decision tree or ensemble methods. Regularized models like Ridge and Lasso handle multicollinearity, and ensemble techniques like XGBoost shine in scenarios demanding both predictive accuracy and scalability. The journey through these models underscores the importance of choosing the right tool for the task at hand, as the Python ecosystem empowers practitioners to explore, compare, and deploy models tailored to diverse analytical needs.