Update driverpospredictionfinal.py
Browse files- driverpospredictionfinal.py +43 -57
driverpospredictionfinal.py
CHANGED
@@ -15,8 +15,6 @@ app = Flask(__name__)
|
|
15 |
from sklearn import tree, linear_model
|
16 |
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
|
17 |
from sklearn.model_selection import train_test_split, GridSearchCV
|
18 |
-
!pip install bayesian-optimization
|
19 |
-
from bayes_opt import BayesianOptimization
|
20 |
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, mean_squared_log_error, median_absolute_error
|
21 |
import pandas as pd
|
22 |
import joblib
|
@@ -27,10 +25,20 @@ import xgboost as xgb
|
|
27 |
import pickle
|
28 |
|
29 |
def read_data(file_name):
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
#returns the object for model being used
|
36 |
def decision_tree_regressor_method():
|
@@ -88,16 +96,20 @@ def hyperamter_tuning_paramter_grid():
|
|
88 |
def prediction(req):
|
89 |
data = json.loads(req)
|
90 |
input_data = {}
|
91 |
-
|
92 |
-
input_data['
|
93 |
-
input_data['
|
94 |
-
input_data['
|
95 |
-
input_data['
|
96 |
-
input_data['
|
97 |
-
input_data['
|
98 |
-
input_data['
|
99 |
-
input_data['
|
100 |
-
|
|
|
|
|
|
|
|
|
101 |
return jsonify({'message':'Hello world'})
|
102 |
|
103 |
|
@@ -150,52 +162,26 @@ def ask_user(model, X_test=None):
|
|
150 |
|
151 |
|
152 |
|
|
|
|
|
153 |
|
|
|
|
|
|
|
154 |
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
model_1 = linear_reg(xtrain,ytrain)
|
160 |
-
|
161 |
-
ypred = predict(model_1,xtest,ytest)
|
162 |
-
#visvualize(ytest,ypred)
|
163 |
-
|
164 |
-
#-----descision tree classifier-----
|
165 |
-
x , y= read_data("b.csv")
|
166 |
-
xtrain, xtest , ytrain, ytest = train_test_split(x,y,test_size = 0.3)
|
167 |
-
|
168 |
-
model_2 = decsison_tree_classifier_method(xtrain,ytrain)
|
169 |
-
|
170 |
-
parameter_grid= hyperamter_tuning_paramter_grid()
|
171 |
-
tuned_model_2 = hyper_paramter_tuning(model_2,xtrain,ytrain,**parameter_grid)
|
172 |
-
y_pred = predict(tuned_model_2,xtest,ytest)
|
173 |
-
|
174 |
-
#visvualize(ytest,ypred)
|
175 |
-
|
176 |
-
#----descision tree regressor-------
|
177 |
-
x , y= read_data("b.csv")
|
178 |
-
xtrain, xtest , ytrain, ytest = train_test_split(x,y,test_size = 0.3)
|
179 |
|
180 |
model_3 = decision_tree_regressor_method()
|
181 |
-
|
182 |
-
|
183 |
-
tuned_model_3 = hyper_paramter_tuning(model_3,xtrain,ytrain,**parameter_grid)
|
184 |
-
y_pred= predict(tuned_model_3,xtest,ytest)
|
185 |
-
|
186 |
-
|
187 |
-
with open('model.pkl', 'wb') as file:
|
188 |
-
pickle.dump(tuned_model_3, file)
|
189 |
-
|
190 |
-
|
191 |
|
192 |
# Ask user for input and predict position
|
193 |
-
|
194 |
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
#predicted_position = ask_user(tuned_model_3, xtest)
|
199 |
-
|
200 |
-
#pickle.dump(tuned_model_3.open('model.pkl','mb'))
|
201 |
|
|
|
15 |
from sklearn import tree, linear_model
|
16 |
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
|
17 |
from sklearn.model_selection import train_test_split, GridSearchCV
|
|
|
|
|
18 |
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, mean_squared_log_error, median_absolute_error
|
19 |
import pandas as pd
|
20 |
import joblib
|
|
|
25 |
import pickle
|
26 |
|
27 |
def read_data(file_name):
|
28 |
+
df = pd.read_csv(file_name)
|
29 |
+
|
30 |
+
# Convert 'LapTime' column to numeric (removing 'days' and 'seconds' part)
|
31 |
+
df['LapTime'] = pd.to_timedelta(df['LapTime']).dt.total_seconds()
|
32 |
+
|
33 |
+
# Encoding categorical columns (like 'Compound' and 'Compound2')
|
34 |
+
label_encoder = LabelEncoder()
|
35 |
+
df['Compound'] = label_encoder.fit_transform(df['Compound'])
|
36 |
+
df['Compound2'] = label_encoder.fit_transform(df['Compound2'])
|
37 |
+
|
38 |
+
# Drop non-predictive columns and set target variable
|
39 |
+
X = df.drop(['Race', 'Driver', 'Position', 'Rainfall', 'RainFall2'], axis=1)
|
40 |
+
y = df['Position']
|
41 |
+
return X, y
|
42 |
|
43 |
#returns the object for model being used
|
44 |
def decision_tree_regressor_method():
|
|
|
96 |
def prediction(req):
|
97 |
data = json.loads(req)
|
98 |
input_data = {}
|
99 |
+
|
100 |
+
input_data['LapNumber'] = float(input("Enter the number of total laps in race: "))
|
101 |
+
input_data['LapTime'] = float(input("Avg lap time from prev year (in seconds): "))
|
102 |
+
input_data['TyreLife'] = float(input("Enter tyre life (in laps): "))
|
103 |
+
input_data['Speedl1'] = float(input("Enter Speedl1 (in km/h): "))
|
104 |
+
input_data['Speedl2'] = float(input("Enter Speedl2 (in km/h): "))
|
105 |
+
input_data['SpeedFL'] = float(input("Enter SpeedFL (in km/h): "))
|
106 |
+
input_data['SpeedST'] = float(input("Enter SpeedST (in km/h): "))
|
107 |
+
input_data['AirTemp'] = float(input("Enter air temperature in Celsius: "))
|
108 |
+
input_data['TrackTemp'] = float(input("Enter track temperature in Celsius: "))
|
109 |
+
input_data['Humidity'] = float(input("Enter humidity percentage (0-100): "))
|
110 |
+
input_data['WindSpeed'] = float(input("Enter wind speed in km/h: "))
|
111 |
+
input_data['Compound'] = float(input("Enter compound (numeric): "))
|
112 |
+
input_data['Compound2'] = float(input("Enter compound2 (numeric): "))
|
113 |
return jsonify({'message':'Hello world'})
|
114 |
|
115 |
|
|
|
162 |
|
163 |
|
164 |
|
165 |
+
X, y = read_data("b.csv")
|
166 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
|
167 |
|
168 |
+
# Train and evaluate models
|
169 |
+
# model_1 = linear_reg(X_train, y_train)
|
170 |
+
# y_pred_1 = predict(model_1, X_test, y_test)
|
171 |
|
172 |
+
#model_2 = decision_tree_classifier_method(X_train, y_train)
|
173 |
+
parameter_grid = hyperparameter_tuning_paramter_grid()
|
174 |
+
# tuned_model_2 = hyper_paramter_tuning(model_2, X_train, y_train, **parameter_grid)
|
175 |
+
# y_pred_2 = predict(tuned_model_2, X_test, y_test)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
model_3 = decision_tree_regressor_method()
|
178 |
+
tuned_model_3 = hyper_paramter_tuning(model_3, X_train, y_train, **parameter_grid)
|
179 |
+
y_pred_3 = predict(tuned_model_3, X_test, y_test)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
# Ask user for input and predict position
|
182 |
+
predicted_position = ask_user(tuned_model_3, X_test)
|
183 |
|
184 |
+
# Save model
|
185 |
+
with open('tuned_model.pkl', 'wb') as file:
|
186 |
+
pickle.dump(tuned_model_3, file)
|
|
|
|
|
|
|
187 |
|