Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added linear-regression #508

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions linear-regression-without-libraries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import numpy as np
import matplotlib.pyplot as plt

# Sample Data (Height vs. Weight, for example)
# X represents height in cm, Y represents weight in kg
X = np.array([150, 160, 170, 180, 190])
Y = np.array([50, 60, 65, 70, 80])
n = len(X)

# Mean of X and Y
mean_x = np.mean(X)
mean_y = np.mean(Y)

# Calculating the coefficients for Linear Regression
numerator = np.sum((X - mean_x) * (Y - mean_y)) # Σ((Xi - X̄) * (Yi - Ȳ))
denominator = np.sum((X - mean_x) ** 2) # Σ((Xi - X̄)^2)

# Slope (m) and Intercept (b) for y = mx + b
m = numerator / denominator # Slope
b = mean_y - (m * mean_x) # Intercept

print(f"Slope (m): {m}")
print(f"Intercept (b): {b}")

# Prediction using the regression model
def predict(x):
return m * x + b

# Predicted values for X
Y_pred = predict(X)

# Plotting the results
plt.scatter(X, Y, color="blue", label="Data Points") # Plot original data points
plt.plot(X, Y_pred, color="red", label="Regression Line") # Plot regression line

plt.xlabel("Height (cm)")
plt.ylabel("Weight (kg)")
plt.title("Linear Regression Example")
plt.legend()

# Save the plot as an image file
plt.savefig("linear_regression_plot.png")
print("Plot saved as 'linear_regression_plot.png'")