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="Project Futbiz (FIFA19)", page_icon="⚽", layout="wide" ) # Load all pickle files @st.cache_resource def load_data(): try: with open('newdf3.pkl', 'rb') as f: df3 = pickle.load(f) with open('predictorsscale.pkl', 'rb') as f: predictors_scaled = pickle.load(f) with open('newpredictors.pkl', 'rb') as f: predictors_df = pickle.load(f) with open('train_predictors_val.pkl', 'rb') as f: train_predictors_val = pickle.load(f) with open('newfifa.pkl', 'rb') as f: fifa = pickle.load(f) with open('df3scaled.pkl', 'rb') as f: df3scaled = pickle.load(f) 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): try: # 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) 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,] try: #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') try: predictions = abs(xbr.predict(predictors_anomaly_processed)) predictions = predictions.astype('int64') except AttributeError: st.warning("Using fallback prediction method due to model version mismatch") # Fallback to using value directly if prediction fails predictions = predicted_players_value except Exception as e: st.error(f"Error in prediction part: {str(e)}") # Fallback to using original values if prediction fails predictions = predicted_players_value result = final_pred(NUM_RECOM, predictions, predicted_players_value, predicted_players_name) return result except Exception as e: st.error(f"Error in recommendation part: {str(e)}") return [] 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(): # Replace st.title with custom markdown for smaller title st.markdown("""

Project Futbiz (FIFA19) 🎮⚽

""", unsafe_allow_html=True) # Create two columns - one for instructions and one for results left_col, right_col = st.columns([1, 1]) with left_col: # Add business context and description st.markdown("""

About this App

This FIFA 19 Player Recommender helps football clubs and managers identify similar players and predict their market value. It's particularly useful for:

How it Works

  1. The app uses advanced machine learning(KNN and XGBoost) to analyze player attributes and find similar players
  2. It considers over 70 different player statistics and characteristics
  3. Provides market value predictions to help with transfer budget planning

How to Use

  1. Select Your Team from the sidebar - this helps find players that would fit your team's style
  2. Choose Position you're looking to fill
  3. Adjust Number of Recommendations (1-10 players)
  4. Set Maximum Age to focus on your preferred age range
  5. Click "Get Recommendations" to see your matches!
""", unsafe_allow_html=True) # 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 right_col: 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.markdown(f"""

Recommended Players for {team_chosen} - {postion_chosen}

""", unsafe_allow_html=True) # Create a container for recommendations for idx, player in enumerate(recommendations, 1): with st.container(): st.markdown(f"""
Recommendation #{idx}: {player['player_name']}
Estimated Value: €{player['starting_bid']:,.0f}

""", unsafe_allow_html=True) 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'))