alinJain commited on
Commit
bb1009c
·
verified ·
1 Parent(s): dfb7845

Upload driverpospredictionfinal.py

Browse files
Files changed (1) hide show
  1. driverpospredictionfinal.py +239 -0
driverpospredictionfinal.py ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """DriverPosPredictionFinal
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1DGTfO4HEZDof1phuficJD_J8v38JnF3C
8
+ """
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+ from sklearn import tree, linear_model
17
+ from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
18
+ from sklearn.model_selection import train_test_split, GridSearchCV
19
+ !pip install bayesian-optimization
20
+ from bayes_opt import BayesianOptimization
21
+ from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, mean_squared_log_error, median_absolute_error
22
+ import pandas as pd
23
+ import joblib
24
+ import matplotlib.pyplot as plt
25
+ import seaborn as sns
26
+ import numpy as np
27
+ import xgboost as xgb
28
+ import pickle
29
+
30
+ def read_data(file_name):
31
+ df = pd.read_csv(file_name)
32
+ x= df.drop(['RaceID', "Pos", "formating" ], axis=1)
33
+ y =df["Pos"]
34
+ return x,y
35
+
36
+ #returns the object for model being used
37
+ def decision_tree_regressor_method():
38
+ dtree = DecisionTreeRegressor(random_state=42)
39
+ return dtree
40
+ def decsison_tree_classifier_method(X_train,y_train):
41
+ dtree = DecisionTreeClassifier()
42
+ dtree = dtree.fit(X_train,y_train)
43
+ return dtree
44
+ def linear_reg(X_train,y_train):
45
+ regr= linear_model.LinearRegression()
46
+ regr.fit(X_train,y_train)
47
+ return regr
48
+ def hyper_paramter_tuning(model, xtrain,ytrain,**kwargs): #doesnt work for linear regression
49
+ #dtree = DecisionTreeRegressor(random_state=42)
50
+ # grid_search_object = GridSearchCV(estimator=model, param_grid=kwargs, cv=5, n_jobs=-1, verbose=2, scoring='neg_mean_squared_error')
51
+ grid_search_object = GridSearchCV(estimator=model, param_grid=kwargs, cv=3, n_jobs=-1, verbose=2, scoring='neg_mean_squared_error')
52
+ grid_search_object.fit(xtrain,ytrain)
53
+ best_estimator = grid_search_object.best_estimator_
54
+ return best_estimator
55
+
56
+
57
+ def predict(model, xtest,ytest):
58
+ # Predict the target for the test set
59
+ y_pred = model.predict(xtest)
60
+ # Calculate various evaluation metrics
61
+ mse = mean_squared_error(ytest, y_pred)
62
+ rmse = mse ** 0.5
63
+ mae = mean_absolute_error(ytest, y_pred)
64
+ r2 = r2_score(ytest, y_pred)
65
+ msle = mean_squared_log_error(ytest, y_pred)
66
+ medae = median_absolute_error(ytest, y_pred)
67
+ # Print the metrics
68
+
69
+ print(f"Test MSE: {mse}") # smaller is better - mean squared error
70
+ print(f"Test RMSE: {rmse}")#
71
+ print(f"Test MAE: {mae}")
72
+ print(f"Test R²: {r2}")
73
+ print(f"Test MSLE: {msle}")
74
+ print(f"Test Median Absolute Error: {medae}")
75
+ #rmse measures abg magnitude of errors betwen actual and predicted values
76
+ return y_pred
77
+
78
+
79
+
80
+ def hyperamter_tuning_paramter_grid():
81
+ parameter_grid = {'max_depth': [1,2,3,4,5,None],
82
+ "min_samples_split":[2,3,5,6] ,
83
+ 'min_samples_leaf': [1,2,4,5],
84
+ "min_weight_fraction_leaf": [0.0,0.01, 0.05,0.1, 0.2],# add more hypermater tuning criteria
85
+ }#'criterion': ['gini', 'entropy']
86
+ return parameter_grid
87
+
88
+
89
+
90
+
91
+ def ask_user(model, X_test=None):
92
+ """
93
+ Collect user input and format it for prediction.
94
+ Returns a DataFrame with a single row formatted like the training data.
95
+ """
96
+ try:
97
+ # Create a dictionary to store inputs
98
+ input_data = {}
99
+
100
+ # Collect basic race information
101
+ input_data['LapNumber'] = float(input("Enter the number of total laps in race: "))
102
+ input_data['LapTimes'] = float(input("Avg lap time from prev year (or estimate 98): "))
103
+ input_data['PitStopTimes'] = float(input("Enter pit stop time (0 if no pit stop): "))
104
+ input_data['PrevLap'] = float(input("Enter avg lap time differece (0-2) (0 if first lap): "))
105
+
106
+ # Collect race conditions
107
+ input_data['AvgSpeed'] = float(input("Enter average speed in km/h from prev year: "))
108
+ input_data['AirTemp_Cel'] = float(input("estimate air temperature in Celsius on race day: "))
109
+ input_data['TrackTemp_Cel'] = float(input("estimate track temperature in Celsius on race day: "))
110
+ input_data['Humidity'] = float(input("estimate the humidity percentage (0-100) on race day: "))
111
+ input_data['WindSpeed_km/h'] = float(input("estimate wind speed in km/h: "))
112
+
113
+ # Create a DataFrame with the input data
114
+ input_df = pd.DataFrame([input_data])
115
+
116
+ # If we have a test set, ensure our columns match the training data
117
+ if X_test is not None:
118
+ missing_cols = set(X_test.columns) - set(input_df.columns)
119
+ # Add any missing columns with 0s
120
+ for col in missing_cols:
121
+ input_df[col] = 0
122
+ # Ensure column order matches training data
123
+ input_df = input_df[X_test.columns]
124
+
125
+ # Make prediction
126
+ prediction = model.predict(input_df)
127
+ print(f"\nPredicted position: {int(round(prediction[0]))}")
128
+
129
+ return prediction[0]
130
+
131
+ except ValueError as e:
132
+ print(f"Error: Please enter valid numeric values. Details: {e}")
133
+ return None
134
+ except Exception as e:
135
+ print(f"An error occurred: {e}")
136
+ return None
137
+
138
+
139
+
140
+
141
+
142
+ #-----linear regresssion------------
143
+ x , y= read_data("b.csv")
144
+ xtrain, xtest , ytrain, ytest = train_test_split(x,y,test_size = 0.3)
145
+
146
+ model_1 = linear_reg(xtrain,ytrain)
147
+
148
+ ypred = predict(model_1,xtest,ytest)
149
+ #visvualize(ytest,ypred)
150
+
151
+ #-----descision tree classifier-----
152
+ x , y= read_data("b.csv")
153
+ xtrain, xtest , ytrain, ytest = train_test_split(x,y,test_size = 0.3)
154
+
155
+ model_2 = decsison_tree_classifier_method(xtrain,ytrain)
156
+
157
+ parameter_grid= hyperamter_tuning_paramter_grid()
158
+ tuned_model_2 = hyper_paramter_tuning(model_2,xtrain,ytrain,**parameter_grid)
159
+ y_pred = predict(tuned_model_2,xtest,ytest)
160
+
161
+ #visvualize(ytest,ypred)
162
+
163
+ #----descision tree regressor-------
164
+ x , y= read_data("b.csv")
165
+ xtrain, xtest , ytrain, ytest = train_test_split(x,y,test_size = 0.3)
166
+
167
+ model_3 = decision_tree_regressor_method()
168
+
169
+ parameter_grid= hyperamter_tuning_paramter_grid()
170
+ tuned_model_3 = hyper_paramter_tuning(model_3,xtrain,ytrain,**parameter_grid)
171
+ y_pred= predict(tuned_model_3,xtest,ytest)
172
+
173
+
174
+ # Ask user for input and predict position
175
+ #predicted_position = ask_user(tuned_model_3, x_columns)
176
+
177
+
178
+
179
+
180
+ predicted_position = ask_user(tuned_model_3, xtest)
181
+
182
+ #pickle.dump(tuned_model_3.open('model.pkl','mb'))
183
+
184
+ !pip install streamlit
185
+ import streamlit as st
186
+ import pickle
187
+ import numpy as np
188
+
189
+ model = pickle.load(open('model.pkl','rb'))
190
+
191
+ def predictPos(TLaps,Laptime,Pit,Prev,AvgSpeed,AirTemp,TrackTemp,Humid,Wind):
192
+ input = np.array([ [TLaps,Laptime,Pit,Prev,AvgSpeed,AirTemp,TrackTemp,Humid,Wind]]).astype(np.float64)
193
+ prediction=model.predict(input)
194
+ return prediction[0]
195
+
196
+ def main():
197
+ st.title("title")
198
+ html_temp="""
199
+ <div style = "background-color:#025244; padding :10px">
200
+ <h2 style = "color:white; text-align:center;"> Driver Position Prediction </h2>
201
+ </div>
202
+ """
203
+ st.markdown(html_temp, unsafe_allow_html=True)
204
+ TLaps = st.text_input("Enter the number of total laps in race: ","Type Here")
205
+ Laptime = st.text_input("Avg lap time from prev year (or estimate 98): ","Type Here")
206
+ Pit = st.text_input("Enter pit stop time (0 if no pit stop): ","Type Here")
207
+ Prev= st.text_input("Enter avg lap time differece (0-2) (0 if first lap): ","Type Here")
208
+
209
+
210
+ AvgSpeed = st.text_input("Enter average speed in km/h from prev year: ","Type Here")
211
+ AirTemp= st.text_input("estimate air temperature in Celsius on race day: ","Type Here")
212
+ TrackTemp = st.text_input("estimate track temperature in Celsius on race day: ","Type Here")
213
+ Humid = st.text_input("estimate the humidity percentage (0-100) on race day: ","Type Here")
214
+ Wind = st.text_input("estimate wind speed in km/h: ","Type Here")
215
+ safe_html="""
216
+ <div style="background-color:#F4D03F;padding:10px >
217
+ <h2 style="color:white;text-align:center;"> Your forest is safe</h2>
218
+ </div>
219
+ """
220
+ danger_html="""
221
+ <div style="background-color:#F08080;padding:10px >
222
+ <h2 style="color:black ;text-align:center;"> Your forest is in danger</h2>
223
+ </div>
224
+ """
225
+
226
+ if st.button("Predict"):
227
+ output=predictPos(TLaps,Laptime,Pit,Prev,AvgSpeed,AirTemp,TrackTemp,Humid,Wind)
228
+ st.success('The postion of driver is {}'.format(output))
229
+
230
+ # if output > 0.5:
231
+ # st.markdown(danger_html,unsafe_allow_html=True)
232
+ # else:
233
+ # st.markdown(safe_html,unsafe_allow_html=True)
234
+
235
+ if __name__=='__main__':
236
+ main()
237
+
238
+
239
+