Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanMendez01 committed Apr 24, 2024
1 parent a0f05d4 commit e0b7c42
Show file tree
Hide file tree
Showing 8 changed files with 11,006 additions and 0 deletions.
Binary file added assists_prediction_model.pkl
Binary file not shown.
102 changes: 102 additions & 0 deletions fantasy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import pandas as pd
import numpy as np # Add numpy for generating random data
from flask import Flask, render_template, request
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import LabelEncoder
import joblib

app = Flask(__name__)

# Load the preprocessed data
data = pd.read_csv('static/players_data.csv', index_col='player') # Set 'player' column as index

# Prepare features and target
X = data.select_dtypes(include=['float64', 'int64']) # Only select numeric columns
y_goals = data['goals']
y_assists = data['assists']

# Encode categorical variables
label_encoder = LabelEncoder()
X.index = label_encoder.fit_transform(X.index)

# Train the models
model_goals = RandomForestRegressor(n_estimators=100, random_state=42)
model_goals.fit(X, y_goals)

model_assists = RandomForestRegressor(n_estimators=100, random_state=42)
model_assists.fit(X, y_assists)

# Save the trained models
joblib.dump(model_goals, 'goals_prediction_model.pkl')
joblib.dump(model_assists, 'assists_prediction_model.pkl')
joblib.dump(label_encoder, 'label_encoder.pkl')

# Get all teams
all_teams = sorted(data['team'].unique())

def select_best_players(team):
# Filter the data for the selected team
team_data = data[data['team'] == team]

# Sort the players based on some criteria (e.g., goals or assists)
sorted_players = team_data.sort_values(by=['goals', 'assists'], ascending=False)

# Select the top 5 players and get their names
best_players = sorted_players.index[:5].tolist()

return best_players

def predict_player_performance(player_stats, model_goals, model_assists):
# Ensure proper feature names
player_stats.columns = X.columns

# Make predictions
player_stats_values = player_stats.values.reshape(1, -1) # Reshape the data for prediction
predicted_goals = int(model_goals.predict(player_stats_values)[0])
predicted_assists = int(model_assists.predict(player_stats_values)[0])

# Return all predicted stats
return predicted_goals, predicted_assists


@app.route('/')
def home():
return render_template('index2.html', teams=all_teams)

@app.route('/select_players', methods=['POST'])
def select_players():
team1 = request.form['team1']
team2 = request.form['team2']

best_players_team1 = select_best_players(team1)
best_players_team2 = select_best_players(team2)

return render_template('index2.html', team1=team1, team2=team2, teams=all_teams, best_players_team1=best_players_team1, best_players_team2=best_players_team2)

@app.route('/predict_performance', methods=['POST'])
def predict_performance():
team1 = request.form['team1']
team2 = request.form['team2']

best_players_team1 = select_best_players(team1)
best_players_team2 = select_best_players(team2)

# Load the trained models
model_goals = joblib.load('goals_prediction_model.pkl')
model_assists = joblib.load('assists_prediction_model.pkl')

# Generate random player statistics for prediction
random_player_stats = pd.DataFrame(np.random.randint(0, 10, size=(len(best_players_team1 + best_players_team2), len(X.columns))), columns=X.columns)

# Get predicted stats for each player
predicted_stats = {}
for player, stats in zip(best_players_team1 + best_players_team2, random_player_stats.iterrows()):
predicted_goals, predicted_assists = predict_player_performance(stats[1], model_goals, model_assists)
predicted_stats[player] = (predicted_goals, predicted_assists)

return render_template('index2.html', team1=team1, team2=team2, teams=all_teams,
best_players_team1=best_players_team1, best_players_team2=best_players_team2,
predicted_stats=predicted_stats)

if __name__ == '__main__':
app.run(debug=True)
Binary file added goals_prediction_model.pkl
Binary file not shown.
Binary file added label_encoder.pkl
Binary file not shown.
Binary file added player_performance_prediction_model.pkl
Binary file not shown.
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
gunicorn
Flask
pandas
numpy
scikit-learn
joblib
10,755 changes: 10,755 additions & 0 deletions static/players_data.csv

Large diffs are not rendered by default.

143 changes: 143 additions & 0 deletions templates/index2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fantasy Soccer</title>
<!-- <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}"> -->
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 50px auto;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #333;
}
form {
margin-top: 20px;
}
.team-select {
margin-bottom: 20px;
}
label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
select {
width: 100%;
padding: 10px;
border-radius: 5px;
border: 1px solid #ccc;
}
input[type="submit"] {
width: 100%;
padding: 10px;
border-radius: 5px;
border: none;
background-color: #4CAF50;
color: white;
cursor: pointer;
font-size: 16px;
}
input[type="submit"]:hover {
background-color: #45a049;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin-bottom: 5px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<div class="container">
<h1>Fantasy Soccer</h1>
<form action="/select_players" method="post">
<div class="team-select">
<label for="team1">Select Team 1:</label>
<select name="team1" id="team1">
{% for team in teams %}
<option value="{{ team }}">{{ team }}</option>
{% endfor %}
</select>
</div>
<div class="team-select">
<label for="team2">Select Team 2:</label>
<select name="team2" id="team2">
{% for team in teams %}
<option value="{{ team }}">{{ team }}</option>
{% endfor %}
</select>
</div>
<input type="submit" value="Select Players">
</form>
<!-- Display the selected teams and best players here -->
{% if team1 and team2 %}
<h2>Team 1: {{ team1 }}</h2>
<h3>Best Players:</h3>
<ul>
{% for player in best_players_team1 %}
<li>{{ player.split('/')[1].split('/')[0].replace('-', ' ') }}</li>
{% endfor %}
</ul>
<h2>Team 2: {{ team2 }}</h2>
<h3>Best Players:</h3>
<ul>
{% for player in best_players_team2 %}
<li>{{ player.split('/')[1].split('/')[0].replace('-', ' ') }}</li>
{% endfor %}
</ul>
<form action="/predict_performance" method="post">
<input type="hidden" name="team1" value="{{ team1 }}">
<input type="hidden" name="team2" value="{{ team2 }}">
<input type="submit" value="Predict Performance">
</form>
{% endif %}
<!-- Prediction results will be displayed here -->
{% if predicted_stats %}
<h2>Prediction Results</h2>
<table>
<tr>
<th>Player</th>
<th>Predicted Goals</th>
<th>Predicted Assists</th>
</tr>
{% for player, stats in predicted_stats.items() %}
<tr>
<td>{{ player.split('/')[1].split('/')[0].replace('-', ' ') }}</td>
<td>{{ stats[0] }}</td>
<td>{{ stats[1] }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
</div>
</body>
</html>

0 comments on commit e0b7c42

Please sign in to comment.