0% found this document useful (0 votes)
9 views

Implementation of Simple Linear Regression in Python

The document provides a Python implementation of Simple Linear Regression using NumPy, Matplotlib, and Scikit-learn. It includes steps for defining data, creating and fitting the model, making predictions, evaluating the model with Mean Squared Error, and visualizing the results with a scatter plot and regression line. Key outputs include the slope coefficient and intercept of the regression line.

Uploaded by

sumit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Implementation of Simple Linear Regression in Python

The document provides a Python implementation of Simple Linear Regression using NumPy, Matplotlib, and Scikit-learn. It includes steps for defining data, creating and fitting the model, making predictions, evaluating the model with Mean Squared Error, and visualizing the results with a scatter plot and regression line. Key outputs include the slope coefficient and intercept of the regression line.

Uploaded by

sumit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Implementation of Simple Linear Regression in Python

import numpy as np

import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error

# Manually defined data

X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1) # Reshape to 2D

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

# Create model and fit

model = LinearRegression()

model.fit(X, Y)

# Prediction

Y_pred = model.predict(X)

# Evaluation

print("Mean Squared Error:", mean_squared_error(Y, Y_pred))

print("Coefficient (slope):", model.coef_[0])

print("Intercept:", model.intercept_)

# Plotting

plt.scatter(X, Y, color='blue', label='Actual')

plt.plot(X, Y_pred, color='red', label='Regression line')

plt.xlabel('X')

plt.ylabel('Y')

plt.title('Simple Linear Regression (Manual Data)')

plt.legend()

plt.show()

You might also like