Spaces:
Running
Running
import numpy as np | |
import pandas as pd | |
import streamlit as st | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.neighbors import NearestNeighbors | |
import pickle | |
# Set page config | |
st.set_page_config( | |
page_title="FIFA 19 Player Recommender", | |
page_icon="âš½", | |
layout="wide" | |
) | |
# Load all pickle files | |
def load_data(): | |
try: | |
# Load DataFrames from CSV | |
df3 = pd.read_csv('newdf3.csv', index_col=0) | |
predictors_scaled = pd.read_csv('predictorsscale.csv', index_col=0) | |
predictors_df = pd.read_csv('newpredictors.csv', index_col=0) | |
train_predictors_val = pd.read_csv('train_predictors_val.csv', index_col=0) | |
fifa = pd.read_csv('newfifa.csv', index_col=0) | |
df3scaled = pd.read_csv('df3scaled.csv', index_col=0) | |
# Only the model needs to stay as pickle | |
with open('finalxbrmodel.pkl', 'rb') as f: | |
xbr = pickle.load(f) | |
return df3, predictors_scaled, predictors_df, train_predictors_val, fifa, df3scaled, xbr | |
except Exception as e: | |
st.error(f"Error loading data: {str(e)}") | |
raise e | |
# Load data | |
df3, predictors_scaled, predictors_df, train_predictors_val, fifa, df3scaled, xbr = load_data() | |
predscale_target = predictors_scaled.columns.tolist() | |
def player_sim_team(team, position, NUM_RECOM, AGE_upper_bound): | |
# part 1(recommendation) | |
target_cols = predscale_target | |
# team stats | |
team_stats = df3scaled.query('position_group == @position and Club == @team').head(3)[target_cols].mean(axis=0) | |
team_stats_np = team_stats.values | |
# player stats by each position | |
ply_stats = df3scaled.query('position_group == @position and Club != @team and Age1 <= @AGE_upper_bound')[ | |
['ID'] + target_cols] | |
ply_stats_np = ply_stats[target_cols].values | |
X = np.vstack((team_stats_np, ply_stats_np)) | |
## KNN | |
nbrs = NearestNeighbors(n_neighbors=NUM_RECOM + 1, algorithm='auto').fit(X) | |
dist, rank = nbrs.kneighbors(X) | |
global indice | |
global predicted_players_name | |
global predicted_players_value | |
global predictions | |
indice = ply_stats.iloc[rank[0, 1:]].index.tolist() | |
predicted_players_name=df3['Name'].loc[indice,].tolist() | |
predicted_players_value=fifa['Value'].loc[indice,].tolist() | |
display_df1 = predictors_scaled.loc[indice,] | |
playrpredictorss = predictors_df.loc[indice,] | |
display_df2 = df3.loc[indice,] | |
display_df = fifa.loc[indice,] | |
#part 2(prediction) | |
predictors_anomaly_processed=playrpredictorss[playrpredictorss.index.isin(list(display_df2['ID']))].copy() | |
predictors_anomaly_processed['Forward_Skill'] = predictors_anomaly_processed.loc[:,['LS', 'ST', 'RS', 'LW', 'LF', 'CF', 'RF', 'RW']].mean(axis=1) | |
predictors_anomaly_processed['Midfield_Skill'] = predictors_anomaly_processed.loc[:,['LAM','CAM','RAM', 'LM', 'LCM', 'CM' ,'RCM', 'RM','LDM', 'CDM', 'RDM']].mean(axis=1) | |
predictors_anomaly_processed['Defence_Skill'] = predictors_anomaly_processed.loc[:,['LWB','RWB', 'LB','LCB','CB','RCB','RB']].mean(axis=1) | |
predictors_anomaly_processed = predictors_anomaly_processed.drop(['LS', 'ST', 'RS', 'LW', 'LF', 'CF', 'RF', 'RW', | |
'LAM','CAM','RAM', 'LM', 'LCM', 'CM' ,'RCM', 'RM','LDM', 'CDM', 'RDM', | |
'LWB','RWB', 'LB','LCB','CB','RCB','RB'], axis = 1) | |
predictors_anomaly_processed=predictors_anomaly_processed.drop(predictors_anomaly_processed.iloc[:,predictors_anomaly_processed.columns.get_loc('Position_CAM'):predictors_anomaly_processed.columns.get_loc('Position_ST')+1], axis=1) | |
predictors_anomaly_processed=predictors_anomaly_processed[train_predictors_val.columns] | |
predictors_anomaly_processed[['International Reputation','Real Face']]=predictors_anomaly_processed[['International Reputation','Real Face']].astype('category') | |
scaler = StandardScaler() | |
predictors_anomaly_processed[predictors_anomaly_processed.select_dtypes(include=['float64','float32','int64','int32'], exclude=['category']).columns] = scaler.fit_transform(predictors_anomaly_processed.select_dtypes(include=['float64','float32','int64','int32'], exclude=['category'])) | |
predictors_anomaly_processed[predictors_anomaly_processed.select_dtypes(include='category').columns]=predictors_anomaly_processed[predictors_anomaly_processed.select_dtypes(include='category').columns].astype('int') | |
predictions = abs(xbr.predict(predictors_anomaly_processed)) | |
predictions = predictions.astype('int64') | |
result=final_pred(NUM_RECOM,predictions,predicted_players_value,predicted_players_name) | |
return result | |
def final_pred(num_of_players,b=[],c=[],d=[]): | |
z=[] | |
for m in range(0,num_of_players): | |
c[m]=((c[m]+b[m])/2) | |
z.append({"starting_bid":c[m],"player_name":d[m]}) | |
return z | |
def main(): | |
st.title("FIFA 19 Player Recommender 🎮⚽") | |
# Sidebar inputs | |
st.sidebar.header("Search Parameters") | |
# Get unique teams and positions | |
teams = sorted(df3['Club'].unique()) | |
positions = sorted(df3['position_group'].unique()) | |
team_chosen = st.sidebar.selectbox("Select Team", teams) | |
postion_chosen = st.sidebar.selectbox("Select Position", positions) | |
num_of_players = st.sidebar.slider("Number of Players to Recommend", 1, 10, 5) | |
age_up = st.sidebar.slider("Maximum Age", 16, 45, 30) | |
if st.sidebar.button("Get Recommendations"): | |
with st.spinner("Finding similar players..."): | |
recommendations = player_sim_team(team_chosen, postion_chosen, num_of_players, age_up) | |
# Display results in a nice format | |
st.subheader(f"Recommended Players for {team_chosen} - {postion_chosen}") | |
# Create columns for each player | |
cols = st.columns(min(3, len(recommendations))) | |
for idx, player in enumerate(recommendations): | |
col_idx = idx % 3 | |
with cols[col_idx]: | |
st.markdown(f""" | |
#### {player['player_name']} | |
**Estimated Value:** €{player['starting_bid']:,.2f} | |
--- | |
""") | |
if __name__ == '__main__': | |
main() | |
#print("postions=side_df,cent_df,cent_md,side_md,cent_fw,side_fw,goalkeep") | |
#print("team=any club teams in any of the countries ") | |
#print("*********************************************** \n") | |
#team_chosen = str(input("Enter the team you are looking for: \n")) | |
#postion_chosen = str(input("Enter the position you are looking for: \n")) | |
#num_of_players = input("Enter the number of similar players you are looking for: \n") | |
#age_up = input("Enter the age limit: ") | |
#print("***please have some biscuits, it will take some time***") | |
#player_sim_team(team_chosen,postion_chosen, int(num_of_players), int(age_up)) | |
#finalfunction = player_sim_team(team_chosen,postion_chosen, int(num_of_players), int(age_up)) | |
#pickle.dump(finalfunction, open('finalfunction.pkl', 'wb')) | |