import matplotlib.pyplot as plt # -------------------------------------------------- # STEP 1: DATASET # -------------------------------------------------- # House sizes in square feet (Independent variable X) X = [500, 800, 1000, 1200, 1500] # House prices in thousands of dollars (Dependent variable y) y = [100, 160, 200, 240, 300] # -------------------------------------------------- # STEP 2: CALCULATE THE MEAN (AVERAGE) # -------------------------------------------------- # Calculate the average house size avg_x = sum(X) / len(X) # Calculate the average house price avg_y = sum(y) / len(y) # -------------------------------------------------- # STEP 3: CALCULATE DEVIATIONS FROM THE MEAN # -------------------------------------------------- # Deviation means: # Each value minus its average # Formula: X - avg_x dev_x = [] for _x in X: dev_x.append(_x - avg_x) print("X deviations:", dev_x) # Calculate deviations for house prices # Formula: y - avg_y dev_y = [] for _y in y: dev_y.append(_y - avg_y) print("Y deviations:", dev_y) # -------------------------------------------------- # STEP 4: MULTIPLY THE DEVIATIONS # -------------------------------------------------- # Multiply corresponding X and Y deviations. # Formula: (X - avg_x) * (y - avg_y) # # These products are used to calculate the slope. product = [] for pair in zip(dev_x, dev_y): product.append(pair[0] * pair[1]) # Sum all the products of deviations print("Sum of products:", sum(product)) # -------------------------------------------------- # STEP 5: CALCULATE SQUARED X DEVIATIONS # -------------------------------------------------- # Square each X deviation. # Formula: (X - avg_x)^2 # # These values form the denominator of the slope formula. squared_dev_x = [] for _x in X: diff_x = _x - avg_x squared_dev_x.append(diff_x * diff_x) print("Squared X deviations:", squared_dev_x) # -------------------------------------------------- # STEP 6: CALCULATE SQUARED Y DEVIATIONS # -------------------------------------------------- # Square each Y deviation. # Formula: (y - avg_y)^2 # # These values are useful when calculating # correlation and variance. squared_dev_y = [] for _y in y: diff_y = _y - avg_y squared_dev_y.append(diff_y * diff_y) print("Squared Y deviations:", squared_dev_y) # -------------------------------------------------- # STEP 7: CALCULATE THE SLOPE (m) # -------------------------------------------------- # Slope formula: # # m = Sum[(X - avg_x) * (y - avg_y)] # -------------------------------- # Sum[(X - avg_x)^2] # # Slope tells us how much the predicted house price # changes when house size increases by one unit. slope = sum(product) / sum(squared_dev_x) print("Slope:", slope) # -------------------------------------------------- # STEP 8: CALCULATE THE INTERCEPT (b) # -------------------------------------------------- # Intercept formula: # # b = avg_y - (slope * avg_x) # # Intercept is where the regression line crosses # the Y-axis (when X = 0). intercept = avg_y - (slope * avg_x) print("Intercept:", intercept) # -------------------------------------------------- # STEP 9: CALCULATE PREDICTED HOUSE PRICES # -------------------------------------------------- # Linear regression equation: # # y = mx + b # # Use the slope and intercept to predict # the house price for each house size. predicted_y = [] for x in X: predicted_y.append(slope * x + intercept) # -------------------------------------------------- # STEP 10: VISUALIZE THE DATA AND REGRESSION LINE # -------------------------------------------------- # Plot actual house sizes and prices as blue dots plt.scatter(X, y, color="blue", label="Actual prices") # Plot predicted prices as a red regression line plt.plot(X, predicted_y, color="red", label="Regression line") # Add labels to the X and Y axes plt.xlabel("House Size (sq ft)") plt.ylabel("Price ($1000s)") # Add a chart title plt.title("Linear Regression: House Size vs House Price") # Display the legend plt.legend() # Display the final chart plt.show()