Spaces:
Sleeping
Sleeping
File size: 7,007 Bytes
05d029a 48fbeda 05d029a 48fbeda 05d029a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
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
@st.cache_resource
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'))
|