Linear Regression
Linear Regression is a statistical method used to predict the value of a continuous target
variable based on one or more independent variables. A simple linear regression predicts the
target variable using one independent variable.
The relationship is modeled as:
y=mx+c
Where:
• y is the target variable (output)
• x is the input variable (feature)
• m is the slope (coefficient)
• c is the y-intercept
The objective is to find the values of mmm and ccc that minimize the error (difference)
between the predicted values (y^) and actual values.
Sample Dataset:
We will use a simple dataset for predicting a student's score based on the number of hours
studied:
Hours Studied Score
1 50
2 55
3 65
4 70
5 75
6 85
7 95
Steps to Implement Linear Regression:
1. Understand the Dataset: We use "Hours Studied" as the input variable (x) and
"Score" as the target variable (y).
2. Calculate the Coefficients: Using mathematical formulas for m and c:
Where:
• xˉ is the mean of x.
• yˉis the mean of y.
Predict Values: Use y=mx+c to predict scores.
Python Code Implementation:
Explanation of the Code:
1. Input Data: The hours_studied and scores arrays represent the dataset.
2. Calculate Mean: Compute the average of the input (x) and target (y).
3. Calculate Slope and Intercept: Using the formulas for mmm and ccc.
4. Predict: Use the equation y=mx+c to generate predictions.
5. Visualize: The scatter plot shows actual data points, and the red line represents the
regression line.
Output: