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()