0% found this document useful (0 votes)
2 views3 pages

Import Pandas As PD

This document outlines a Simple Linear Regression implementation in Python using the pandas, numpy, and matplotlib libraries. It includes methods for fitting the model to data, making predictions, and calculating the R-squared value. The model is applied to a dataset containing years of experience and corresponding salaries, with results visualized in a scatter plot and regression line.

Uploaded by

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

Import Pandas As PD

This document outlines a Simple Linear Regression implementation in Python using the pandas, numpy, and matplotlib libraries. It includes methods for fitting the model to data, making predictions, and calculating the R-squared value. The model is applied to a dataset containing years of experience and corresponding salaries, with results visualized in a scatter plot and regression line.

Uploaded by

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

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

class SimpleLinearRegression:

def __init__(self):

# Initialize instance attributes

self.alpha = 0

self.beta = 0

def fit(self, X, y):

# Calculate means

X_mean, y_mean = np.mean(X), np.mean(y)

# Calculate coefficients using the least squares method

self.beta = np.sum((X - X_mean) * (y - y_mean)) / np.sum((X - X_mean) ** 2)

self.alpha = y_mean - self.beta * X_mean

def predict(self, X):

# Use the model's coefficients to make predictions

return self.alpha + self.beta * X

def score(self, X, y):

# Predict values

y_pred = self.predict(X)

# Calculate the total sum of squares

ss_total = np.sum((y - np.mean(y)) ** 2)

# Calculate the residual sum of squares

ss_residual = np.sum((y - y_pred) ** 2)


# Calculate R-squared

r_squared = 1 - (ss_residual / ss_total)

return r_squared

# Load the dataset

df = pd.read_excel('C:/Users/abhis/Downloads/Regression_Practice_Dataset.xlsx')

X = df['YearsExperience']

y = df['Salary']

# Create an instance of the SimpleLinearRegression class

model = SimpleLinearRegression()

# Fit the model to the data

model.fit(X, y)

# Make predictions

y_pred = model.predict(X)

# Calculate R-squared value

r_squared = model.score(X, y)

# Print the coefficients and R-squared value

print(f'Intercept (alpha): {model.alpha}')

print(f'Coefficient (beta): {model.beta}')

print(f'R-squared: {r_squared}')

# Plot the data and the regression line

plt.scatter(X, y, color='blue', label='Data points')

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

plt.xlabel('Years of Experience')
plt.ylabel('Salary')

plt.legend()

plt.show()

You might also like