Linear regression

Linear regression is a basic statistical technique used to understand the relationship between two variables. It helps us predict the value of one variable based on the values of another variable.

Think of it like this: imagine you have a set of data points scattered on a graph. Linear regression tries to draw a straight line that best fits these data points. This line is called the "regression line" or "line of best fit."

The idea is to find the line that minimizes the distance between the data points and the line itself. Once we have this line, we can use it to make predictions. For example, if we know the value of one variable (let's call it X), linear regression allows us to estimate the corresponding value of the other variable (let's call it Y).

The regression line has two important components: the slope and the intercept. The slope represents how the Y variable changes for a one-unit increase in X. The intercept is the point where the line crosses the Y-axis.

Linear regression is useful in various fields, such as economics, finance, and social sciences, where we want to understand the relationship between variables and make predictions based on that understanding.

Python Example

import numpy as np
from sklearn.linear_model import LinearRegression

# Create sample input data
X = np.array([[1], [2], [3], [4], [5]])  # Independent variable
Y = np.array([2, 4, 6, 8, 10])  # Dependent variable

# Create and fit the linear regression model
model = LinearRegression()
model.fit(X, Y)

# Make predictions
X_new = np.array([[6], [7]])  # New values of X for prediction
predictions = model.predict(X_new)

# Print the predicted values
for x, y_pred in zip(X_new, predictions):
  print(f"Input: {x}, Predicted Output: {y_pred}")

Results:


Input: [6], Predicted Output: 12.0
Input: [7], Predicted Output: 14.0
      

In this example, we have a simple dataset with X values representing independent variables and Y values representing dependent variables. We create an instance of the LinearRegression class from scikit-learn, fit the model using the fit method, and then use the predict method to make predictions on new X values (X_new).

The output will be the predicted values of the dependent variable (Y) based on the given independent variable (X).