Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def convert_minutes(val):\n",
" if val > 10:\n",
" return 1\n",
" else:\n",
" return 0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from Feature_engineering import convert_minutes\n",
"from sklearn.metrics import confusion_matrix, accuracy_score, mean_squared_error, f1_score\n",
"\n",
"def load_and_prepare_results(gameweek):\n",
" \"\"\"\n",
" Load results for a specific gameweek, converting minutes using the convert_minutes function.\n",
" \"\"\"\n",
" results_path = f\"C:\\\\Users\\\\prane\\\\Downloads\\\\FPL\\\\GW_PointsPredictor\\\\Default_data\\\\2023-24\\\\results\\\\GW{gameweek}.csv\"\n",
" results = pd.read_csv(results_path)\n",
" results[\"minutes_actual\"] = results[\"minutes\"].apply(convert_minutes)\n",
" return results\n",
"\n",
"def load_predicted_data(position, gameweek, data_type):\n",
" \"\"\"\n",
" Load predicted minutes or points data for a given position and gameweek.\n",
" \"\"\"\n",
" file_path = f\"C:\\\\Users\\\\prane\\\\Downloads\\\\FPL\\\\GW_PointsPredictor\\\\Predicted_outcomes\\\\GW{gameweek}\\\\{position}_{data_type}.csv\"\n",
" return pd.read_csv(file_path)\n",
"\n",
"def merge_and_clean_data(predicted_df, results_df):\n",
" \"\"\"\n",
" Merge predicted data with actual results and remove rows with missing values.\n",
" \"\"\"\n",
" merged_df = pd.merge(predicted_df, results_df, on=\"name\", how=\"left\").dropna()\n",
" return merged_df\n",
"\n",
"def evaluate_predictions(predictions_df, actual_column, predicted_column):\n",
" \"\"\"\n",
" Calculate and print evaluation metrics for the predictions.\n",
" \"\"\"\n",
" print(confusion_matrix(predictions_df[actual_column], predictions_df[predicted_column]))\n",
" print(\"Accuracy score:\", accuracy_score(predictions_df[actual_column], predictions_df[predicted_column]))\n",
" print(\"F1 SCORE:\", f1_score(predictions_df[actual_column], predictions_df[predicted_column]))\n",
" rmse = np.sqrt(mean_squared_error(predictions_df[actual_column], predictions_df[predicted_column]))\n",
" print(f\"Root Mean Square Error: {rmse}\")\n",
"\n",
"def process_position(position, gameweek, results):\n",
" \"\"\"\n",
" Process and evaluate predictions for a given position (goalkeepers, defenders, midfielders, fowards).\n",
" \"\"\"\n",
" print(f\"\\n{position.upper()}\")\n",
" for data_type in [\"minutes\", \"points\"]:\n",
" predicted_data = load_predicted_data(position, gameweek, data_type)\n",
" if data_type == \"minutes\":\n",
" merged_data = merge_and_clean_data(predicted_data, results[[\"name\", \"minutes_actual\"]])\n",
" else:\n",
" merged_data = merge_and_clean_data(predicted_data, results[[\"name\", \"minutes_actual\", \"total_points\"]])\n",
" \n",
" evaluate_predictions(merged_data, \"minutes_actual\" if data_type == \"minutes\" else \"total_points\", data_type)\n",
"\n",
"gameweek = 26\n",
"for i in range(1, gameweek + 1):\n",
" results = load_and_prepare_results(i)\n",
" \n",
" print(f\"\\n\\nGAMEWEEK {i}\")\n",
" for position in [\"goalkeepers\", \"defenders\", \"midfielders\", \"fowards\"]:\n",
" process_position(position, i, results)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}