forked from AIPI510/aipi510-fall24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-linear-regression.py
61 lines (43 loc) · 2.06 KB
/
simple-linear-regression.py
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
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
def main():
print("Generating test data for a linear regression example...")
# Generate an array of 100 random numbers between 0 and 10
x = np.random.rand(100, 1) * 10
# Compute `y` values based on the linear relationship `y = 2x + 1 + randn(100, 1)`
y = 2 * x + 1 + np.random.randn(100, 1)
# Split the data into training and testing sets (80/20)
print("Splitting the data into training and testing sets...")
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
# Create a linear regression model
model = LinearRegression()
# Fit the model to the training data
print("Training the linear regression model...")
model.fit(x_train, y_train)
# Make predictions on the test data
print("Making predictions on the test data...")
y_pred = model.predict(x_test)
# Plot the test data (scatterplot)
plt.scatter(x_test, y_test, color='blue')
# Plot the linear regression model (line plot)
plt.plot(x_test, y_pred, color='red', linewidth=2)
# Add labels and title to the plot
plt.xlabel('x')
plt.ylabel('y')
plt.title('Linear Regression Example')
# Create a text box that includes the linear regression equation
plt.text(6, 4, "y = %.2f + %.2fx" % (model.intercept_, model.coef_),
bbox=dict(facecolor='gray', alpha=0.5))
# Create a text box that includes the sum of squared errors (SSE) and R-squared value
plt.text(0.5, 18,
"Sum of squared error (SSE): %.2f\nCoefficient of determination: %.2f" %
(np.sum((y_test - y_pred) ** 2), model.score(x_test, y_test)),
bbox=dict(facecolor='gray', alpha=0.5))
# Save the plot as a PNG file
plt.savefig('linear-regression.png')
print("Model trained and evaluated successfully!")
print("Open linear-regression.png to view the model results.")
if __name__ == "__main__":
main()