Program 7:
AIM:
To implement Linear Regression using Python and evaluate its performance using
Mean Squared Error (MSE).
THEORY:
Linear Regression is one of the most fundamental supervised machine learning
algorithms used for predicting a continuous dependent variable (y) based on the value of
an independent variable (X). It assumes a linear relationship between the input and output
variables.
Mathematical Formula:
The relationship can be represented as:
y = b₀ + b₁X + ε
Where:
b₀ is the intercept (bias)
b₁ is the slope (coefficient)
ε is the error term
Key Concepts:
Training: The model learns the best-fit line by minimizing the error between
the predicted and actual values.
Evaluation: A common metric used is Mean Squared Error (MSE) which
measures the average squared difference between actual and predicted
values.
Applications:
Forecasting (e.g., sales, stock prices)
Trend analysis
Risk assessment
Predictive maintenance
PROCEDURE:
1. Import necessary libraries: numpy, matplotlib, and sklearn.
2. Generate synthetic data simulating a linear pattern with some noise.
3. Split the dataset into training and testing sets using train_test_split.
4. Create and train the Linear Regression model using LinearRegression() from
sklearn.
5. Predict the values for test data.
6. Evaluate the model using mean_squared_error.
7. Plot the actual data points and the regression line for visualization.
PROGRAM:
import numpy as np
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error
# Generate synthetic data
[Link](42)
X = 2 * [Link](100, 1)
y = 4 + 3 * X + [Link](100, 1)
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create and train the model
model = LinearRegression()
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")
print(f"Intercept: {model.intercept_[0]}")
print(f"Coefficient: {model.coef_[0][0]}")
# Plot results
[Link](X_test, y_test, color='blue', label='Actual Data') [Link](X_test,
y_pred, color='red', linewidth=2, label='Regression Line') [Link]("X")
[Link]("y") [Link]()
[Link]("Linear Regression Model")
[Link]()
OUTPUT:
Mean Squared Error: 0.6536995137170021
Intercept: 4.142913319458566
Coefficient: 2.7993236574802762
VIVA QUESTIONS:
1. What is linear regression and when is it used?
2. What do the terms "intercept" and "coefficient" mean in linear regression?
3. What is the difference between simple and multiple linear regression?
4. Why do we split the data into training and testing sets?
5. What does the Mean Squared Error (MSE) indicate?
6. What assumptions are made by linear regression?
7. Can linear regression be used for classification tasks? Why or why not?