Spaces:
Sleeping
Sleeping
Initial commit
Browse files- app.py +88 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from sklearn.linear_model import LinearRegression
|
4 |
+
from sklearn.preprocessing import LabelEncoder
|
5 |
+
|
6 |
+
def predict_grand_prix_winner(previous_season_data):
|
7 |
+
"""
|
8 |
+
Predicts the grand prix winner for the 2025 season based on previous season data.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
previous_season_data (pd.DataFrame): DataFrame containing previous season results.
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
pd.DataFrame: DataFrame containing predicted results for the 2025 season.
|
15 |
+
"""
|
16 |
+
|
17 |
+
# Preprocessing
|
18 |
+
le_driver = LabelEncoder()
|
19 |
+
le_team = LabelEncoder()
|
20 |
+
previous_season_data['Driver_encoded'] = le_driver.fit_transform(previous_season_data['Driver'])
|
21 |
+
previous_season_data['Team_encoded'] = le_team.fit_transform(previous_season_data['Team'])
|
22 |
+
|
23 |
+
# Feature Engineering
|
24 |
+
features = ['Driver_encoded', 'Team_encoded', 'Points', 'Starting Grid']
|
25 |
+
target = 'Position'
|
26 |
+
|
27 |
+
# Model Training
|
28 |
+
model = LinearRegression()
|
29 |
+
model.fit(previous_season_data[features], previous_season_data[target])
|
30 |
+
|
31 |
+
# Create a DataFrame for 2025 predictions
|
32 |
+
last_race = previous_season_data.groupby('Driver').last().reset_index()
|
33 |
+
|
34 |
+
# Predict positions for 2025
|
35 |
+
predictions = model.predict(last_race[features])
|
36 |
+
last_race['Predicted_Position'] = predictions
|
37 |
+
last_race['Predicted_Position'] = last_race['Predicted_Position'].round().astype(int)
|
38 |
+
|
39 |
+
# Decode labels back to original names
|
40 |
+
last_race['Driver'] = le_driver.inverse_transform(last_race['Driver_encoded'])
|
41 |
+
last_race['Team'] = le_team.inverse_transform(last_race['Team_encoded'])
|
42 |
+
|
43 |
+
# Sort by predicted position
|
44 |
+
predicted_results = last_race[['Driver', 'Team', 'Predicted_Position']].sort_values(by='Predicted_Position')
|
45 |
+
|
46 |
+
return predicted_results
|
47 |
+
|
48 |
+
def main():
|
49 |
+
st.title("2025 Grand Prix Winner Prediction")
|
50 |
+
|
51 |
+
st.write("Upload the previous season's results (CSV format).")
|
52 |
+
|
53 |
+
# Modified file_uploader to accept CSV
|
54 |
+
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
55 |
+
|
56 |
+
# Input for GP name and date
|
57 |
+
gp_name = st.text_input("Enter the Grand Prix Name (e.g., Australian Grand Prix):")
|
58 |
+
gp_date = st.date_input("Enter the Date of the Grand Prix:")
|
59 |
+
|
60 |
+
if uploaded_file is not None:
|
61 |
+
try:
|
62 |
+
previous_season_data = pd.read_csv(uploaded_file)
|
63 |
+
st.write("Uploaded data:")
|
64 |
+
st.dataframe(previous_season_data)
|
65 |
+
|
66 |
+
# Ensure necessary columns exist
|
67 |
+
required_columns = ['Driver', 'Team', 'Points', 'Position', 'Starting Grid']
|
68 |
+
if not all(col in previous_season_data.columns for col in required_columns):
|
69 |
+
st.error(f"Error: CSV must contain the following columns: {', '.join(required_columns)}")
|
70 |
+
return
|
71 |
+
|
72 |
+
if st.button("Predict 2025 Winners"):
|
73 |
+
predicted_results = predict_grand_prix_winner(previous_season_data)
|
74 |
+
st.write("Predicted 2025 Grand Prix Results:")
|
75 |
+
st.dataframe(predicted_results)
|
76 |
+
if not predicted_results.empty:
|
77 |
+
winner = predicted_results.iloc[0]['Driver']
|
78 |
+
team = predicted_results.iloc[0]['Team']
|
79 |
+
# Display the prediction with GP name and date
|
80 |
+
st.success(f"2025 {gp_name} Winner ({gp_date.strftime('%Y-%m-%d')}): {winner} from team {team}")
|
81 |
+
|
82 |
+
except Exception as e:
|
83 |
+
st.error(f"An error occurred: {e}")
|
84 |
+
else:
|
85 |
+
st.info("Please upload a CSV file with the previous season's results.")
|
86 |
+
|
87 |
+
if __name__ == "__main__":
|
88 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas
|
3 |
+
scikit-learn
|