import numpy as np
import matplotlib.pyplot as plt
def estimate_b0_b1(x, y):
# Get de average of "x" and "y"
mean_x, mean_y = np.mean(x), np.mean(y)
# Calculate de summation of (xy) and (xx)
sum_xy = np.sum((x - mean_x)*(y - mean_y))
sum_xx = np.sum(pow(x - mean_x, 2))
# Regression coefficient
b_1 = sum_xy / sum_xx
b_0 = mean_y - (b_1 * mean_x)
return (b_0, b_1)
# Plotter function
def plot_regression(x, y, b):
plt.scatter(x, y, color='red', marker='o', s=30)
y_predictions = b[0] + b[1]*x
plt.plot(x, y_predictions, color='blue')
# Labels
plt.xlabel('x = Independent Variable')
plt.ylabel('y = Dependent Variable')
plt.show()
# Main Code
def main():
# Dataset
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 3, 5, 6, 5])
# Get b0 and b1
b = estimate_b0_b1(x, y)
print("b0 = {}, b1 ={}".format(b[0], b[1]))
#Plot the regression line
plot_regression(x, y, b)
if __name__ == "__main__":
main()
b0 = 1.5, b1 =0.9