import numpy as np import matplotlib.pyplot as plt def calculate_slope_intercept(x_values, y_values): """ Calculate the slope and intercept of a regression line. @param x_values the independent x values. @param y_values the dependent y values. @return a list of the slope and y-intercept of the line. """ x = np.array(x_values) y = np.array(y_values) n = len(x) sum_x = np.sum(x) sum_y = np.sum(y) sum_xx = np.sum(x*x) sum_xy = np.sum(x*y) numerator = sum_xy - (sum_x*sum_y)/n denominator = sum_xx - ((sum_x*sum_x)/n) m = numerator/denominator b = (np.mean(y) - m*np.mean(x)) return m, b # slope and intercept def show_least_squares_line(title, x_label, y_label, x_values, y_values): """ @param title the chart title. @param x_label the x-axis label. @param y_label the y-axis label. @param x_values the independent x values to plot. @param y_values the dependent y values to plot. """ # First show the scatter plot. plt.scatter(x_values, y_values) # Now show the least squares line. m, b = calculate_slope_intercept(x_values, y_values) reg_line = [m*x + b for x in x_values] # regression line plt.plot(x_values, reg_line, color='red') plt.title(f'{title}, m = {m:.2f}, b = {b:.2f}') plt.xlabel(x_label) plt.ylabel(y_label) plt.show()