-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_linear_regression.py
More file actions
30 lines (24 loc) · 931 Bytes
/
sample_linear_regression.py
File metadata and controls
30 lines (24 loc) · 931 Bytes
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
# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# Sample data: hours studied vs. test score
hours_studied = np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1)
test_scores = np.array([50, 55, 65, 70, 75, 80])
# Create a simple linear regression model
model = LinearRegression()
# Train the model but decide on metrics
model.fit(hours_studied, test_scores)
# Predict scores
predicted_scores = model.predict(hours_studied)
# Display the regression line
plt.scatter(hours_studied, test_scores, color='blue', label='Actual Scores')
plt.plot(hours_studied, predicted_scores, color='red', label='Regression Line')
plt.xlabel('Hours Studied')
plt.ylabel('Test Score')
plt.title('Simple Linear Regression')
plt.legend()
plt.show()
# Print the slope and intercept
print(f"Slope (Coefficient): {model.coef_[0]:.2f}")
print(f"Intercept: {model.intercept_:.2f}")