-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab_01.py
More file actions
67 lines (56 loc) · 3.2 KB
/
Copy pathLab_01.py
File metadata and controls
67 lines (56 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import matplotlib.pyplot as plt
# Step 1: Ask the user to input height and weight data
print("Enter height and weight data (e.g., '170 70'). Type 'done' when finished:")
heights = [] # To store the height values
weights = [] # To store the weight values
# Collect data until the user types 'done'
while True:
user_input = input("Height and Weight: ").strip()
if user_input.lower() == "done":
break # Exit the loop if the user is done
try:
# Try to split the input into two float values (height and weight)
height, weight = map(float, user_input.split())
heights.append(height) # Add height to the list
weights.append(weight) # Add weight to the list
except ValueError:
# If the input is not valid, show an error message
print("Invalid input. Please enter two numbers separated by a space.")
# Step 2: Convert the height and weight data into numpy arrays
x = np.array(heights) # Heights as x values
y = np.array(weights) # Weights as y values
# Step 3: Check if we have enough data for the regression
if len(x) < 2:
print("Not enough data to perform regression. Please input at least two data points.")
exit() # Exit if there are not enough data points
# Step 4: Calculate the mean (average) of the height and weight
mean_x = np.mean(x) # Mean of the heights
mean_y = np.mean(y) # Mean of the weights
# Step 5: Calculate the slope (m) and intercept (b) for the regression line
numerator = np.sum((x - mean_x) * (y - mean_y)) # Top part of the slope formula
denominator = np.sum((x - mean_x) ** 2) # Bottom part of the slope formula
m = numerator / denominator # The slope (m)
b = mean_y - m * mean_x # The intercept (b)
# Step 6: Use the regression line to predict weight values based on height
y_pred = m * x + b # Predicted weight values using the regression line
# Step 7: Calculate the Mean Squared Error (MSE) to evaluate the model's accuracy
mse = np.mean((y - y_pred) ** 2) # Average squared difference between actual and predicted weights
# Step 8: Calculate the R-squared value to measure the goodness of fit
ss_total = np.sum((y - mean_y) ** 2) # Total variation in weight
ss_residual = np.sum((y - y_pred) ** 2) # Remaining variation after regression
r_squared = 1 - (ss_residual / ss_total) # How well the model fits the data
# Step 9: Output the results
print("\nLinear Regression Results")
print(f"Equation: y = {m:.2f}x + {b:.2f}") # Display the regression equation
print(f"Mean Squared Error (MSE): {mse:.2f}") # Display MSE
print(f"R-squared: {r_squared:.2f}") # Display R-squared
# Step 10: Plot the original data and the regression line
plt.scatter(x, y, color="blue", label="Data Points") # Plot original data points as blue dots
plt.plot(x, y_pred, color="red", label="Regression Line") # Plot regression line in red
plt.xlabel("Height (e.g., cm)") # Label for the x-axis
plt.ylabel("Weight (e.g., kg)") # Label for the y-axis
plt.title("Linear Regression") # Title of the plot
plt.legend() # Show legend
plt.grid(True) # Show grid for better readability
plt.show() # Display the plot