Spaces:
Running
Running
fifa19_streamlit
Browse files- README.md +18 -0
- app.py +170 -0
- df3scaled.pkl +3 -0
- finalxbrmodel.pkl +3 -0
- newdf3.pkl +3 -0
- newfifa.pkl +3 -0
- newpredictors.pkl +3 -0
- predictorsscale.pkl +3 -0
- requirements.txt +5 -0
- train_predictors_val.pkl +3 -0
README.md
CHANGED
@@ -11,3 +11,21 @@ short_description: ML App
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
14 |
+
|
15 |
+
# FIFA 19 Player Recommender
|
16 |
+
|
17 |
+
A Streamlit web application that recommends FIFA 19 players based on team and position preferences, and predicts their market value.
|
18 |
+
|
19 |
+
## Setup
|
20 |
+
1. Install requirements: `pip install -r requirements.txt`
|
21 |
+
2. Run the app: `streamlit run app.py`
|
22 |
+
|
23 |
+
## Data Files
|
24 |
+
Make sure all required pickle files are in the `data` directory:
|
25 |
+
- newdf3.pkl
|
26 |
+
- predictorsscale.pkl
|
27 |
+
- newpredictors.pkl
|
28 |
+
- train_predictors_val.pkl
|
29 |
+
- newfifa.pkl
|
30 |
+
- df3scaled.pkl
|
31 |
+
- finalxbrmodel.pkl
|
app.py
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
+
from sklearn.preprocessing import StandardScaler
|
5 |
+
from sklearn.neighbors import NearestNeighbors
|
6 |
+
import pickle
|
7 |
+
|
8 |
+
# Set page config
|
9 |
+
st.set_page_config(
|
10 |
+
page_title="FIFA 19 Player Recommender",
|
11 |
+
page_icon="⚽",
|
12 |
+
layout="wide"
|
13 |
+
)
|
14 |
+
|
15 |
+
# Load all pickle files
|
16 |
+
@st.cache_resource
|
17 |
+
def load_data():
|
18 |
+
try:
|
19 |
+
with open('newdf3.pkl', 'rb') as f:
|
20 |
+
df3 = pickle.load(f)
|
21 |
+
with open('predictorsscale.pkl', 'rb') as f:
|
22 |
+
predictors_scaled = pickle.load(f)
|
23 |
+
with open('newpredictors.pkl', 'rb') as f:
|
24 |
+
predictors_df = pickle.load(f)
|
25 |
+
with open('train_predictors_val.pkl', 'rb') as f:
|
26 |
+
train_predictors_val = pickle.load(f)
|
27 |
+
with open('newfifa.pkl', 'rb') as f:
|
28 |
+
fifa = pickle.load(f)
|
29 |
+
with open('df3scaled.pkl', 'rb') as f:
|
30 |
+
df3scaled = pickle.load(f)
|
31 |
+
with open('finalxbrmodel.pkl', 'rb') as f:
|
32 |
+
xbr = pickle.load(f)
|
33 |
+
return df3, predictors_scaled, predictors_df, train_predictors_val, fifa, df3scaled, xbr
|
34 |
+
except Exception as e:
|
35 |
+
st.error(f"Error loading data: {str(e)}")
|
36 |
+
raise e
|
37 |
+
|
38 |
+
# Load data
|
39 |
+
df3, predictors_scaled, predictors_df, train_predictors_val, fifa, df3scaled, xbr = load_data()
|
40 |
+
predscale_target = predictors_scaled.columns.tolist()
|
41 |
+
|
42 |
+
def player_sim_team(team, position, NUM_RECOM, AGE_upper_bound):
|
43 |
+
# part 1(recommendation)
|
44 |
+
target_cols = predscale_target
|
45 |
+
|
46 |
+
|
47 |
+
# team stats
|
48 |
+
team_stats = df3scaled.query('position_group == @position and Club == @team').head(3)[target_cols].mean(axis=0)
|
49 |
+
team_stats_np = team_stats.values
|
50 |
+
|
51 |
+
# player stats by each position
|
52 |
+
ply_stats = df3scaled.query('position_group == @position and Club != @team and Age1 <= @AGE_upper_bound')[
|
53 |
+
['ID'] + target_cols]
|
54 |
+
ply_stats_np = ply_stats[target_cols].values
|
55 |
+
X = np.vstack((team_stats_np, ply_stats_np))
|
56 |
+
|
57 |
+
## KNN
|
58 |
+
nbrs = NearestNeighbors(n_neighbors=NUM_RECOM + 1, algorithm='auto').fit(X)
|
59 |
+
dist, rank = nbrs.kneighbors(X)
|
60 |
+
|
61 |
+
|
62 |
+
global indice
|
63 |
+
global predicted_players_name
|
64 |
+
global predicted_players_value
|
65 |
+
global predictions
|
66 |
+
|
67 |
+
indice = ply_stats.iloc[rank[0, 1:]].index.tolist()
|
68 |
+
predicted_players_name=df3['Name'].loc[indice,].tolist()
|
69 |
+
predicted_players_value=fifa['Value'].loc[indice,].tolist()
|
70 |
+
display_df1 = predictors_scaled.loc[indice,]
|
71 |
+
playrpredictorss = predictors_df.loc[indice,]
|
72 |
+
display_df2 = df3.loc[indice,]
|
73 |
+
display_df = fifa.loc[indice,]
|
74 |
+
|
75 |
+
|
76 |
+
#part 2(prediction)
|
77 |
+
predictors_anomaly_processed=playrpredictorss[playrpredictorss.index.isin(list(display_df2['ID']))].copy()
|
78 |
+
predictors_anomaly_processed['Forward_Skill'] = predictors_anomaly_processed.loc[:,['LS', 'ST', 'RS', 'LW', 'LF', 'CF', 'RF', 'RW']].mean(axis=1)
|
79 |
+
|
80 |
+
predictors_anomaly_processed['Midfield_Skill'] = predictors_anomaly_processed.loc[:,['LAM','CAM','RAM', 'LM', 'LCM', 'CM' ,'RCM', 'RM','LDM', 'CDM', 'RDM']].mean(axis=1)
|
81 |
+
|
82 |
+
predictors_anomaly_processed['Defence_Skill'] = predictors_anomaly_processed.loc[:,['LWB','RWB', 'LB','LCB','CB','RCB','RB']].mean(axis=1)
|
83 |
+
|
84 |
+
predictors_anomaly_processed = predictors_anomaly_processed.drop(['LS', 'ST', 'RS', 'LW', 'LF', 'CF', 'RF', 'RW',
|
85 |
+
'LAM','CAM','RAM', 'LM', 'LCM', 'CM' ,'RCM', 'RM','LDM', 'CDM', 'RDM',
|
86 |
+
'LWB','RWB', 'LB','LCB','CB','RCB','RB'], axis = 1)
|
87 |
+
|
88 |
+
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)
|
89 |
+
|
90 |
+
predictors_anomaly_processed=predictors_anomaly_processed[train_predictors_val.columns]
|
91 |
+
predictors_anomaly_processed[['International Reputation','Real Face']]=predictors_anomaly_processed[['International Reputation','Real Face']].astype('category')
|
92 |
+
|
93 |
+
scaler = StandardScaler()
|
94 |
+
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']))
|
95 |
+
predictors_anomaly_processed[predictors_anomaly_processed.select_dtypes(include='category').columns]=predictors_anomaly_processed[predictors_anomaly_processed.select_dtypes(include='category').columns].astype('int')
|
96 |
+
|
97 |
+
|
98 |
+
predictions = abs(xbr.predict(predictors_anomaly_processed))
|
99 |
+
predictions = predictions.astype('int64')
|
100 |
+
|
101 |
+
result=final_pred(NUM_RECOM,predictions,predicted_players_value,predicted_players_name)
|
102 |
+
return result
|
103 |
+
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
|
108 |
+
def final_pred(num_of_players,b=[],c=[],d=[]):
|
109 |
+
|
110 |
+
z=[]
|
111 |
+
for m in range(0,num_of_players):
|
112 |
+
|
113 |
+
|
114 |
+
c[m]=((c[m]+b[m])/2)
|
115 |
+
z.append({"starting_bid":c[m],"player_name":d[m]})
|
116 |
+
|
117 |
+
|
118 |
+
return z
|
119 |
+
|
120 |
+
def main():
|
121 |
+
st.title("FIFA 19 Player Recommender 🎮⚽")
|
122 |
+
|
123 |
+
# Sidebar inputs
|
124 |
+
st.sidebar.header("Search Parameters")
|
125 |
+
|
126 |
+
# Get unique teams and positions
|
127 |
+
teams = sorted(df3['Club'].unique())
|
128 |
+
positions = sorted(df3['position_group'].unique())
|
129 |
+
|
130 |
+
team_chosen = st.sidebar.selectbox("Select Team", teams)
|
131 |
+
postion_chosen = st.sidebar.selectbox("Select Position", positions)
|
132 |
+
num_of_players = st.sidebar.slider("Number of Players to Recommend", 1, 10, 5)
|
133 |
+
age_up = st.sidebar.slider("Maximum Age", 16, 45, 30)
|
134 |
+
|
135 |
+
if st.sidebar.button("Get Recommendations"):
|
136 |
+
with st.spinner("Finding similar players..."):
|
137 |
+
recommendations = player_sim_team(team_chosen, postion_chosen, num_of_players, age_up)
|
138 |
+
|
139 |
+
# Display results in a nice format
|
140 |
+
st.subheader(f"Recommended Players for {team_chosen} - {postion_chosen}")
|
141 |
+
|
142 |
+
# Create columns for each player
|
143 |
+
cols = st.columns(min(3, len(recommendations)))
|
144 |
+
for idx, player in enumerate(recommendations):
|
145 |
+
col_idx = idx % 3
|
146 |
+
with cols[col_idx]:
|
147 |
+
st.markdown(f"""
|
148 |
+
#### {player['player_name']}
|
149 |
+
**Estimated Value:** €{player['starting_bid']:,.2f}
|
150 |
+
---
|
151 |
+
""")
|
152 |
+
|
153 |
+
if __name__ == '__main__':
|
154 |
+
main()
|
155 |
+
|
156 |
+
|
157 |
+
|
158 |
+
|
159 |
+
#print("postions=side_df,cent_df,cent_md,side_md,cent_fw,side_fw,goalkeep")
|
160 |
+
#print("team=any club teams in any of the countries ")
|
161 |
+
#print("*********************************************** \n")
|
162 |
+
#team_chosen = str(input("Enter the team you are looking for: \n"))
|
163 |
+
#postion_chosen = str(input("Enter the position you are looking for: \n"))
|
164 |
+
#num_of_players = input("Enter the number of similar players you are looking for: \n")
|
165 |
+
#age_up = input("Enter the age limit: ")
|
166 |
+
#print("***please have some biscuits, it will take some time***")
|
167 |
+
|
168 |
+
#player_sim_team(team_chosen,postion_chosen, int(num_of_players), int(age_up))
|
169 |
+
#finalfunction = player_sim_team(team_chosen,postion_chosen, int(num_of_players), int(age_up))
|
170 |
+
#pickle.dump(finalfunction, open('finalfunction.pkl', 'wb'))
|
df3scaled.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3f77ccab011c9ca2dc7e0b5d9ec2211be8e84d0dceafaacacf6e01190e0c85f2
|
3 |
+
size 12965615
|
finalxbrmodel.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7c3bc0ea4da01e2e0472b65613058efc7bda182555688ccb93cb464e5e5150ac
|
3 |
+
size 337921
|
newdf3.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:eb9025fc993b7662b20b43fbbd2b51743e3e8e7d8639c1823163b3a05bef2d44
|
3 |
+
size 14511354
|
newfifa.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4e5b9afc1d32d860f5580c513e7473d86313230d2ff6e39411ba9563fe3dc437
|
3 |
+
size 14505670
|
newpredictors.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a6b6865fa545e3cb23a09666f495576375cffba58b7ad2ad0b92542617af756c
|
3 |
+
size 13605297
|
predictorsscale.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b2803a7d147279fef83e66a70999e86e376de6cd2fd98d700cca3b13bab06ad9
|
3 |
+
size 11769153
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
numpy
|
3 |
+
pandas
|
4 |
+
scikit-learn
|
5 |
+
pickle5
|
train_predictors_val.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:83b5f49f75184babae988db8cb5332a205c50f108a204ff013ee322ff69b1134
|
3 |
+
size 7789691
|