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