-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear.py
More file actions
109 lines (25 loc) · 1.3 KB
/
Copy pathlinear.py
File metadata and controls
109 lines (25 loc) · 1.3 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# Data: Number of hours spent driving (X) and Risk Score (y)
X = np.array([10, 9, 2, 15, 10, 16, 11, 16]).reshape(-1, 1)
y = np.array([95, 80, 10, 50, 45, 98, 38, 93])
# Split 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=42)
# Create and train the linear regression model
model = LinearRegression()
model.fit(X_train, y_train)
# Predict risk score for 20 hours of driving
hours_spent = np.array([[20]])
predicted_risk_score = model.predict(hours_spent)
# Display the result
print(f"Predicted risk score for 20 hours of driving: {predicted_risk_score[0]:.2f}")
# Plot the results
plt.scatter(X, y, color='blue', label='Actual data')
plt.plot(X, model.predict(X), color='red', label='Best fit line')
plt.scatter(hours_spent, predicted_risk_score, color='green', marker='x', s=100, label='Prediction for 20 hours')
plt.xlabel('Number of hours spent driving')
plt.ylabel('Risk Score')
plt.legend()
plt.show()