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

Linear regression - Colab

The document outlines a Python script that processes salary expenditure data using pandas. It includes functions to calculate the mean and linear regression coefficients (slope and intercept) from the data extracted from a CSV file. The final output displays the linear regression equation based on the calculated coefficients.

Uploaded by

vamsichunchu824
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)
3 views

Linear regression - Colab

The document outlines a Python script that processes salary expenditure data using pandas. It includes functions to calculate the mean and linear regression coefficients (slope and intercept) from the data extracted from a CSV file. The final output displays the linear regression equation based on the calculated coefficients.

Uploaded by

vamsichunchu824
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/ 1

9/5/24, 10:43 AM Untitled0.

ipynb - Colab

df = pd.DataFrame(data)

# Save the DataFr_expenditure.csvame to a CSV file


df.to_csv("/content/salary_expenditure.csv", index=False)

# Function to calculate mean of a list


def calculate_mean(values):
total = 0
count = 0
for value in values:
total += value
count += 1
return total / count if count != 0 else 0

# Function to calculate beta1 (slope) and beta0 (intercept)


def calculate_coefficients(x, y):
x_mean = calculate_mean(x)
y_mean = calculate_mean(y)

numerator = 0
denominator = 0
for i in range(len(x)):
numerator += (x[i] - x_mean) * (y[i] - y_mean)
denominator += (x[i] - x_mean) ** 2

beta1 = numerator / denominator if denominator != 0 else 0


beta0 = y_mean - beta1 * x_mean

return beta0, beta1

# Read data from CSV using pandas


df = pd.read_csv("/content/salary_expenditure.csv")

# Extract 'x' and 'y' as lists from DataFrame


x = df['x_column'].tolist()
y = df['y_column'].tolist()

# Calculate coefficients
beta0, beta1 = calculate_coefficients(x, y)

# Display results
print("Beta0 (Intercept):", beta0)
print("Beta1 (Slope):", beta1)
print(f"Linear Regression Equation: y = {beta0} + {beta1} * x")

Beta0 (Intercept): -7.0


Beta1 (Slope): 6.0
Linear Regression Equation: y = -7.0 + 6.0 * x

https://colab.research.google.com/drive/1vaf3QXneQsauKebxS0rveFdIf_X9L-k4?authuser=1#printMode=true 1/1

You might also like