Spaces:
Sleeping
Sleeping
fifa19_streamlit
Browse files- app.py +64 -63
- requirements.txt +1 -1
app.py
CHANGED
@@ -40,70 +40,71 @@ df3, predictors_scaled, predictors_df, train_predictors_val, fifa, df3scaled, xb
|
|
40 |
predscale_target = predictors_scaled.columns.tolist()
|
41 |
|
42 |
def player_sim_team(team, position, NUM_RECOM, AGE_upper_bound):
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
# team stats
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
# player stats by each position
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
## KNN
|
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 |
def final_pred(num_of_players,b=[],c=[],d=[]):
|
109 |
|
|
|
40 |
predscale_target = predictors_scaled.columns.tolist()
|
41 |
|
42 |
def player_sim_team(team, position, NUM_RECOM, AGE_upper_bound):
|
43 |
+
try:
|
44 |
+
# part 1(recommendation)
|
45 |
+
target_cols = predscale_target
|
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 |
+
indice = ply_stats.iloc[rank[0, 1:]].index.tolist()
|
62 |
+
predicted_players_name=df3['Name'].loc[indice,].tolist()
|
63 |
+
predicted_players_value=fifa['Value'].loc[indice,].tolist()
|
64 |
+
display_df1 = predictors_scaled.loc[indice,]
|
65 |
+
playrpredictorss = predictors_df.loc[indice,]
|
66 |
+
display_df2 = df3.loc[indice,]
|
67 |
+
display_df = fifa.loc[indice,]
|
68 |
+
|
69 |
+
try:
|
70 |
+
#part 2(prediction)
|
71 |
+
predictors_anomaly_processed=playrpredictorss[playrpredictorss.index.isin(list(display_df2['ID']))].copy()
|
72 |
+
predictors_anomaly_processed['Forward_Skill'] = predictors_anomaly_processed.loc[:,['LS','ST','RS','LW','LF','CF','RF','RW']].mean(axis=1)
|
73 |
+
predictors_anomaly_processed['Midfield_Skill'] = predictors_anomaly_processed.loc[:,['LAM','CAM','RAM','LM','LCM','CM','RCM','RM','LDM','CDM','RDM']].mean(axis=1)
|
74 |
+
predictors_anomaly_processed['Defence_Skill'] = predictors_anomaly_processed.loc[:,['LWB','RWB','LB','LCB','CB','RCB','RB']].mean(axis=1)
|
75 |
+
|
76 |
+
predictors_anomaly_processed = predictors_anomaly_processed.drop(['LS','ST','RS','LW','LF','CF','RF','RW',
|
77 |
+
'LAM','CAM','RAM','LM','LCM','CM','RCM','RM','LDM','CDM','RDM',
|
78 |
+
'LWB','RWB','LB','LCB','CB','RCB','RB'], axis=1)
|
79 |
+
|
80 |
+
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)
|
81 |
+
|
82 |
+
predictors_anomaly_processed=predictors_anomaly_processed[train_predictors_val.columns]
|
83 |
+
predictors_anomaly_processed[['International Reputation','Real Face']]=predictors_anomaly_processed[['International Reputation','Real Face']].astype('category')
|
84 |
+
|
85 |
+
scaler = StandardScaler()
|
86 |
+
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']))
|
87 |
+
predictors_anomaly_processed[predictors_anomaly_processed.select_dtypes(include='category').columns]=predictors_anomaly_processed[predictors_anomaly_processed.select_dtypes(include='category').columns].astype('int')
|
88 |
+
|
89 |
+
try:
|
90 |
+
predictions = abs(xbr.predict(predictors_anomaly_processed))
|
91 |
+
predictions = predictions.astype('int64')
|
92 |
+
except AttributeError:
|
93 |
+
st.warning("Using fallback prediction method due to model version mismatch")
|
94 |
+
# Fallback to using value directly if prediction fails
|
95 |
+
predictions = predicted_players_value
|
96 |
+
|
97 |
+
except Exception as e:
|
98 |
+
st.error(f"Error in prediction part: {str(e)}")
|
99 |
+
# Fallback to using original values if prediction fails
|
100 |
+
predictions = predicted_players_value
|
101 |
+
|
102 |
+
result = final_pred(NUM_RECOM, predictions, predicted_players_value, predicted_players_name)
|
103 |
+
return result
|
|
|
|
|
104 |
|
105 |
+
except Exception as e:
|
106 |
+
st.error(f"Error in recommendation part: {str(e)}")
|
107 |
+
return []
|
108 |
|
109 |
def final_pred(num_of_players,b=[],c=[],d=[]):
|
110 |
|
requirements.txt
CHANGED
@@ -3,4 +3,4 @@ numpy==1.23.5
|
|
3 |
pandas==1.5.3
|
4 |
scikit-learn==1.2.2
|
5 |
pickle5
|
6 |
-
xgboost==1.
|
|
|
3 |
pandas==1.5.3
|
4 |
scikit-learn==1.2.2
|
5 |
pickle5
|
6 |
+
xgboost==1.5.2
|