# Module 2: Introduction to Linear Regression

## Lesson - 2: Linear Regression (part 1)

Welcome to Module 2 of our journey through Machine Learning with Python! In this lesson, we'll dive deep into Linear Regression, a foundational technique used for predictive modeling. Linear Regression is not only simple but also incredibly powerful, making it an essential tool in any data scientist's toolkit. By the end of this lesson, you'll have a solid understanding of Linear Regression, its underlying principles, and how to implement it using Python.

### Understanding Linear Regression

Linear Regression is a statistical method used for modeling the relationship between a dependent variable (target) and one or more independent variables (predictors). The goal is to find the best-fitting straight line (or hyperplane in higher dimensions) that minimizes the difference between the observed and predicted values.

The general form of a linear regression model is given by:

$y = \beta_0 + \beta_1x_1 + \beta_2x_2 + ... + \beta_nx_n + \epsilon$

Where:

- $$y$$ is the dependent variable (target)

- $$\beta_0$$ is the intercept term

- $$\beta_1, \beta_2, ..., \beta_n$$ are the coefficients of the independent variables $$x_1, x_2, ..., x_n$$

- $$\epsilon$$ is the error term, representing the difference between the observed and predicted values

### Ordinary Least Squares (OLS)

The most common method for estimating the parameters (coefficients) of a linear regression model is Ordinary Least Squares (OLS). OLS aims to minimize the sum of the squared differences between the observed and predicted values. Mathematically, it involves finding the values of $$\beta_0, \beta_1, ..., \beta_n$$ that minimize the sum of squared residuals (RSS):

$RSS = \sum_{i=1}^{n}(y_i - \hat{y}_i)^2$

Where:

- $$y_i$$ is the observed value of the dependent variable for the $$i^{th}$$ observation

- $$\hat{y}_i$$ is the predicted value of the dependent variable for the $$i^{th}$$ observation

### Implementing Linear Regression in Python

Now, let's put our knowledge into practice and implement Linear Regression using Python. We'll use two popular libraries: NumPy for numerical computations and scikit-learn for machine learning algorithms.

python

import numpy as np

from sklearn.linear_model import LinearRegression

# Sample data

X = np.array([[1], [2], [3], [4], [5]])

y = np.array([2, 4, 5, 4, 5])

# Create and fit the Linear Regression model

model = LinearRegression()

model.fit(X, y)

# Print the coefficients

print("Intercept:", model.intercept_)

print("Coefficient:", model.coef_)



In this example, we first create some sample data $$X$$ and $$y$$. Then, we create a LinearRegression object, fit the model to the data, and print the intercept and coefficients.

### Conclusion

In this lesson, we've delved into the world of Linear Regression, understanding its fundamentals and implementation in Python. Linear Regression serves as a cornerstone in predictive modeling, providing a simple yet powerful technique for modeling linear relationships between variables. As you continue your journey in Machine Learning, remember to explore further, experiment with different datasets, and leverage the vast array of tools and libraries available in Python.

## Lesson - 3 Linear Regression (part 2)

In this lesson, we're going to delve deeper into Linear Regression, building upon the foundations established in Part 1. We'll explore advanced topics such as Multiple Linear Regression, Polynomial Regression, and regularization techniques like Ridge and Lasso regression. By the end of this lesson, you'll have a comprehensive understanding of these techniques and how to implement them in Python.

### Multiple Linear Regression

In Part 1, we learned about Simple Linear Regression, where there is only one independent variable. However, in real-world scenarios, relationships between variables are often more complex. Multiple Linear Regression extends Simple Linear Regression to incorporate multiple independent variables. The general form of the model is:

$y = \beta_0 + \beta_1x_1 + \beta_2x_2 + ... + \beta_px_p + \epsilon$

Where:

- $$y$$ is the dependent variable

- $$\beta_0$$ is the intercept term

- $$\beta_1, \beta_2, ..., \beta_p$$ are the coefficients of the independent variables $$x_1, x_2, ..., x_p$$

- $$\epsilon$$ is the error term

Let's see an example of Multiple Linear Regression in Python using scikit-learn:

python

import numpy as np

from sklearn.linear_model import LinearRegression

# Sample data

X = np.array([[1, 2], [2, 4], [3, 5], [4, 4], [5, 6]])

y = np.array([2, 4, 5, 4, 7])

#Create and fit the Multiple Linear Regression model

model = LinearRegression()

model.fit(X, y)

# Print the coefficients

print("Intercept:", model.intercept_)

print("Coefficients:", model.coef_)



### Polynomial Regression

While Linear Regression assumes a linear relationship between the independent and dependent variables, Polynomial Regression allows for more complex relationships by introducing polynomial terms of the independent variables. The general form of Polynomial Regression is:

$y = \beta_0 + \beta_1x + \beta_2x^2 + ... + \beta_dx^d + \epsilon$

Where $$x^2, x^3, ..., x^d$$ are the polynomial terms.

Let's illustrate Polynomial Regression with an example:

python

import numpy as np

from sklearn.preprocessing import PolynomialFeatures

from sklearn.linear_model import LinearRegression

# Sample data

X = np.array([[1], [2], [3], [4], [5]])

y = np.array([2, 4, 5, 4, 7])

# Transforming input data for Polynomial Regression

poly = PolynomialFeatures(degree=2)

X_poly = poly.fit_transform(X)

# Create and fit the Polynomial Regression model

model = LinearRegression()

model.fit(X_poly, y)

# Print the coefficients

print("Intercept:", model.intercept_)

print("Coefficients:", model.coef_)



### Regularization Techniques: Ridge and Lasso Regression

Regularization techniques are used to prevent overfitting in regression models by penalizing large coefficients. Two popular regularization techniques are Ridge and Lasso regression.

- Ridge Regression: Also known as L2 regularization, Ridge regression adds a penalty term to the least squares objective function, forcing the coefficients to be small but not necessarily zero.

- Lasso Regression: Also known as L1 regularization, Lasso regression adds a penalty term equal to the absolute value of the coefficients, promoting sparsity and often leading to some coefficients being exactly zero.

Let's see how to implement Ridge and Lasso regression in Python:

python

from sklearn.linear_model import Ridge, Lasso

# Sample data

X = np.array([[1], [2], [3], [4], [5]])

y = np.array([2, 4, 5, 4, 7])

# Create and fit the Ridge Regression model

ridge_model = Ridge(alpha=0.1)

ridge_model.fit(X, y)

# Create and fit the Lasso Regression model

lasso_model = Lasso(alpha=0.1)

lasso_model.fit(X, y)

# Print the coefficients for Ridge and Lasso Regression

print("Ridge Coefficients:", ridge_model.coef_)

print("Lasso Coefficients:", lasso_model.coef_)



### Conclusion

In this lesson, we've explored advanced techniques in Linear Regression, including Multiple Linear Regression, Polynomial Regression, and regularization techniques like Ridge and Lasso regression. These techniques provide powerful tools for modeling complex relationships in data and mitigating overfitting. By mastering these techniques and implementing them in Python, you'll be well-equipped to tackle a wide range of regression problems in real-world scenarios. Keep experimenting, exploring, and honing your skills in Machine Learning.