{ "cells": [ { "cell_type": "markdown", "id": "a6837a67c2f9d3e4", "metadata": { "collapsed": false, "id": "a6837a67c2f9d3e4" }, "source": [ "# UD05 · EX2 — Previsión del valor de mercado de jugadores\n", "\n", "Como hemos visto en teoría, los sistemas mixtos de conocimiento humano/ml son muy poderosos. En este caso, veremos cómo prever el valor de mercado de los jugadores emergentes utilizando reglas definidas por nosotros y luego usar un modelo ML para predecir el valor de mercado de los jugadores. Esta combinación nos permitirá validar nuestras predicciones y ver si el modelo ML puede mejorar nuestro análisis.\n", "\n", "Crearemos un modelo que predecirá el valor de mercado de los jugadores emergentes. Es por eso que usaremos los datos de los jugadores del Fifa 22 del fichero `EX2.-players_22.csv`." ] }, { "cell_type": "markdown", "id": "edbf695c0b6c7421", "metadata": { "collapsed": false, "id": "edbf695c0b6c7421" }, "source": [ "## 1. Preparación del entorno (Solo en Google Colab)\n", "\n", "En la parte izquierda de la pantalla tienes un apartado `ficheros` que puedes usar para `subir` el fichero `EX2.-players_22.csv` y luego poder referenciarlo como si se tratase de un fichero local.\n", "\n", "## 2. Libreria Human Learn\n", "\n", "También necesitaremos instalar la libreria de Human Learn para más adelante\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "614e3f256ed002c5", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T15:18:03.913737Z", "start_time": "2024-01-08T15:17:49.214884Z" }, "colab": { "base_uri": "https://localhost:8080/" }, "id": "614e3f256ed002c5", "outputId": "dd58d9c2-537f-46e2-87a4-1707f48685e0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/koaning/human-learn.git\n", " Cloning https://github.com/koaning/human-learn.git to /tmp/pip-req-build-dkpfcr9d\n", " Running command git clone --filter=blob:none --quiet https://github.com/koaning/human-learn.git /tmp/pip-req-build-dkpfcr9d\n", " Resolved https://github.com/koaning/human-learn.git to commit ab961c200829a3270084fb1345eeea60623fa89d\n", " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Requirement already satisfied: scikit-learn>=0.23.2 in /usr/local/lib/python3.10/dist-packages (from human-learn==0.3.5) (1.5.2)\n", "Requirement already satisfied: pandas>=0.23.4 in /usr/local/lib/python3.10/dist-packages (from human-learn==0.3.5) (2.2.2)\n", "Collecting clumper<0.3.0,>=0.2.5 (from human-learn==0.3.5)\n", " Downloading clumper-0.2.15-py2.py3-none-any.whl.metadata (1.2 kB)\n", "Requirement already satisfied: Shapely>=1.7.1 in /usr/local/lib/python3.10/dist-packages (from human-learn==0.3.5) (2.0.6)\n", "Collecting bokeh<3.0.0,>=2.2.1 (from human-learn==0.3.5)\n", " Downloading bokeh-2.4.3-py3-none-any.whl.metadata (14 kB)\n", "Requirement already satisfied: Jinja2>=2.9 in /usr/local/lib/python3.10/dist-packages (from bokeh<3.0.0,>=2.2.1->human-learn==0.3.5) (3.1.4)\n", "Requirement already satisfied: numpy>=1.11.3 in /usr/local/lib/python3.10/dist-packages (from bokeh<3.0.0,>=2.2.1->human-learn==0.3.5) (1.26.4)\n", "Requirement already satisfied: packaging>=16.8 in /usr/local/lib/python3.10/dist-packages (from bokeh<3.0.0,>=2.2.1->human-learn==0.3.5) (24.2)\n", "Requirement already satisfied: pillow>=7.1.0 in /usr/local/lib/python3.10/dist-packages (from bokeh<3.0.0,>=2.2.1->human-learn==0.3.5) (11.0.0)\n", "Requirement already satisfied: PyYAML>=3.10 in /usr/local/lib/python3.10/dist-packages (from bokeh<3.0.0,>=2.2.1->human-learn==0.3.5) (6.0.2)\n", "Requirement already satisfied: tornado>=5.1 in /usr/local/lib/python3.10/dist-packages (from bokeh<3.0.0,>=2.2.1->human-learn==0.3.5) (6.3.3)\n", "Requirement already satisfied: typing-extensions>=3.10.0 in /usr/local/lib/python3.10/dist-packages (from bokeh<3.0.0,>=2.2.1->human-learn==0.3.5) (4.12.2)\n", "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas>=0.23.4->human-learn==0.3.5) (2.8.2)\n", "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=0.23.4->human-learn==0.3.5) (2024.2)\n", "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas>=0.23.4->human-learn==0.3.5) (2024.2)\n", "Requirement already satisfied: scipy>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=0.23.2->human-learn==0.3.5) (1.13.1)\n", "Requirement already satisfied: joblib>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=0.23.2->human-learn==0.3.5) (1.4.2)\n", "Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=0.23.2->human-learn==0.3.5) (3.5.0)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from Jinja2>=2.9->bokeh<3.0.0,>=2.2.1->human-learn==0.3.5) (3.0.2)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas>=0.23.4->human-learn==0.3.5) (1.16.0)\n", "Downloading bokeh-2.4.3-py3-none-any.whl (18.5 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m18.5/18.5 MB\u001b[0m \u001b[31m55.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hDownloading clumper-0.2.15-py2.py3-none-any.whl (18 kB)\n", "Building wheels for collected packages: human-learn\n", " Building wheel for human-learn (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for human-learn: filename=human_learn-0.3.5-py3-none-any.whl size=116455 sha256=75eb8ef0f1a6a183576e85b8512dcc0fa4410368df6f73cf4baf24db3c31a851\n", " Stored in directory: /tmp/pip-ephem-wheel-cache-_817jwon/wheels/84/81/6b/a9c7f40f0c7d485c35c319703ee16c9a3b0ae88f5bd49d8248\n", "Successfully built human-learn\n", "Installing collected packages: clumper, bokeh, human-learn\n", " Attempting uninstall: bokeh\n", " Found existing installation: bokeh 3.6.2\n", " Uninstalling bokeh-3.6.2:\n", " Successfully uninstalled bokeh-3.6.2\n", "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", "holoviews 1.20.0 requires bokeh>=3.1, but you have bokeh 2.4.3 which is incompatible.\n", "panel 1.5.4 requires bokeh<3.7.0,>=3.5.0, but you have bokeh 2.4.3 which is incompatible.\u001b[0m\u001b[31m\n", "\u001b[0mSuccessfully installed bokeh-2.4.3 clumper-0.2.15 human-learn-0.3.5\n" ] } ], "source": [ "# Instalamos la librería\n", "%pip install git+https://github.com/koaning/human-learn.git" ] }, { "cell_type": "markdown", "id": "JKMsX5O9idAd", "metadata": { "id": "JKMsX5O9idAd" }, "source": [ "## 3. Preparación de los datos\n", "\n", "Cargaremos los datos de los jugadores del FIFA 22 y los metemos en un DataFrame de datos de `Pandas`." ] }, { "cell_type": "code", "execution_count": null, "id": "173cd7942b3548e3", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:38:51.993199Z", "start_time": "2024-01-08T14:38:50.932270Z" }, "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "id": "173cd7942b3548e3", "outputId": "ff2b810e-31cb-475d-83d3-be025e37d590" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ ":3: DtypeWarning: Columns (25,108) have mixed types. Specify dtype option on import or set low_memory=False.\n", " df = pd.read_csv('EX2.-players_22.csv')\n" ] }, { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "variable_name": "df" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sofifa_idplayer_urlshort_namelong_nameplayer_positionsoverallpotentialvalue_eurwage_eurage...lcbcbrcbrbgkplayer_face_urlclub_logo_urlclub_flag_urlnation_logo_urlnation_flag_url
0158023https://sofifa.com/player/158023/lionel-messi/...L. MessiLionel Andrés Messi CuccittiniRW, ST, CF939378000000.0320000.034...50+350+350+361+319+3https://cdn.sofifa.net/players/158/023/22_120.pnghttps://cdn.sofifa.net/teams/73/60.pnghttps://cdn.sofifa.net/flags/fr.pnghttps://cdn.sofifa.net/teams/1369/60.pnghttps://cdn.sofifa.net/flags/ar.png
1188545https://sofifa.com/player/188545/robert-lewand...R. LewandowskiRobert LewandowskiST9292119500000.0270000.032...60+360+360+361+319+3https://cdn.sofifa.net/players/188/545/22_120.pnghttps://cdn.sofifa.net/teams/21/60.pnghttps://cdn.sofifa.net/flags/de.pnghttps://cdn.sofifa.net/teams/1353/60.pnghttps://cdn.sofifa.net/flags/pl.png
220801https://sofifa.com/player/20801/c-ronaldo-dos-...Cristiano RonaldoCristiano Ronaldo dos Santos AveiroST, LW919145000000.0270000.036...53+353+353+360+320+3https://cdn.sofifa.net/players/020/801/22_120.pnghttps://cdn.sofifa.net/teams/11/60.pnghttps://cdn.sofifa.net/flags/gb-eng.pnghttps://cdn.sofifa.net/teams/1354/60.pnghttps://cdn.sofifa.net/flags/pt.png
3190871https://sofifa.com/player/190871/neymar-da-sil...Neymar JrNeymar da Silva Santos JúniorLW, CAM9191129000000.0270000.029...50+350+350+362+320+3https://cdn.sofifa.net/players/190/871/22_120.pnghttps://cdn.sofifa.net/teams/73/60.pnghttps://cdn.sofifa.net/flags/fr.pngNaNhttps://cdn.sofifa.net/flags/br.png
4192985https://sofifa.com/player/192985/kevin-de-bruy...K. De BruyneKevin De BruyneCM, CAM9191125500000.0350000.030...69+369+369+375+321+3https://cdn.sofifa.net/players/192/985/22_120.pnghttps://cdn.sofifa.net/teams/10/60.pnghttps://cdn.sofifa.net/flags/gb-eng.pnghttps://cdn.sofifa.net/teams/1325/60.pnghttps://cdn.sofifa.net/flags/be.png
..................................................................
19234261962https://sofifa.com/player/261962/defu-song/220002Song Defu宋德福CDM475270000.01000.022...46+246+246+248+215+2https://cdn.sofifa.net/players/261/962/22_120.pnghttps://cdn.sofifa.net/teams/112541/60.pnghttps://cdn.sofifa.net/flags/cn.pngNaNhttps://cdn.sofifa.net/flags/cn.png
19235262040https://sofifa.com/player/262040/caoimhin-port...C. PorterCaoimhin PorterCM4759110000.0500.019...44+244+244+248+214+2https://cdn.sofifa.net/players/262/040/22_120.pnghttps://cdn.sofifa.net/teams/445/60.pnghttps://cdn.sofifa.net/flags/ie.pngNaNhttps://cdn.sofifa.net/flags/ie.png
19236262760https://sofifa.com/player/262760/nathan-logue/...N. LogueNathan Logue-CunninghamCM4755100000.0500.021...45+245+245+247+212+2https://cdn.sofifa.net/players/262/760/22_120.pnghttps://cdn.sofifa.net/teams/111131/60.pnghttps://cdn.sofifa.net/flags/ie.pngNaNhttps://cdn.sofifa.net/flags/ie.png
19237262820https://sofifa.com/player/262820/luke-rudden/2...L. RuddenLuke RuddenST4760110000.0500.019...26+226+226+232+215+2https://cdn.sofifa.net/players/262/820/22_120.pnghttps://cdn.sofifa.net/teams/111131/60.pnghttps://cdn.sofifa.net/flags/ie.pngNaNhttps://cdn.sofifa.net/flags/ie.png
19238264540https://sofifa.com/player/264540/emanuel-lalch...E. LalchhanchhuahaEmanuel LalchhanchhuahaCAM4760110000.0500.019...41+241+241+245+216+2https://cdn.sofifa.net/players/264/540/22_120.pnghttps://cdn.sofifa.net/teams/113040/60.pnghttps://cdn.sofifa.net/flags/in.pngNaNhttps://cdn.sofifa.net/flags/in.png
\n", "

19239 rows × 110 columns

\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " sofifa_id player_url \\\n", "0 158023 https://sofifa.com/player/158023/lionel-messi/... \n", "1 188545 https://sofifa.com/player/188545/robert-lewand... \n", "2 20801 https://sofifa.com/player/20801/c-ronaldo-dos-... \n", "3 190871 https://sofifa.com/player/190871/neymar-da-sil... \n", "4 192985 https://sofifa.com/player/192985/kevin-de-bruy... \n", "... ... ... \n", "19234 261962 https://sofifa.com/player/261962/defu-song/220002 \n", "19235 262040 https://sofifa.com/player/262040/caoimhin-port... \n", "19236 262760 https://sofifa.com/player/262760/nathan-logue/... \n", "19237 262820 https://sofifa.com/player/262820/luke-rudden/2... \n", "19238 264540 https://sofifa.com/player/264540/emanuel-lalch... \n", "\n", " short_name long_name \\\n", "0 L. Messi Lionel Andrés Messi Cuccittini \n", "1 R. Lewandowski Robert Lewandowski \n", "2 Cristiano Ronaldo Cristiano Ronaldo dos Santos Aveiro \n", "3 Neymar Jr Neymar da Silva Santos Júnior \n", "4 K. De Bruyne Kevin De Bruyne \n", "... ... ... \n", "19234 Song Defu 宋德福 \n", "19235 C. Porter Caoimhin Porter \n", "19236 N. Logue Nathan Logue-Cunningham \n", "19237 L. Rudden Luke Rudden \n", "19238 E. Lalchhanchhuaha Emanuel Lalchhanchhuaha \n", "\n", " player_positions overall potential value_eur wage_eur age ... \\\n", "0 RW, ST, CF 93 93 78000000.0 320000.0 34 ... \n", "1 ST 92 92 119500000.0 270000.0 32 ... \n", "2 ST, LW 91 91 45000000.0 270000.0 36 ... \n", "3 LW, CAM 91 91 129000000.0 270000.0 29 ... \n", "4 CM, CAM 91 91 125500000.0 350000.0 30 ... \n", "... ... ... ... ... ... ... ... \n", "19234 CDM 47 52 70000.0 1000.0 22 ... \n", "19235 CM 47 59 110000.0 500.0 19 ... \n", "19236 CM 47 55 100000.0 500.0 21 ... \n", "19237 ST 47 60 110000.0 500.0 19 ... \n", "19238 CAM 47 60 110000.0 500.0 19 ... \n", "\n", " lcb cb rcb rb gk \\\n", "0 50+3 50+3 50+3 61+3 19+3 \n", "1 60+3 60+3 60+3 61+3 19+3 \n", "2 53+3 53+3 53+3 60+3 20+3 \n", "3 50+3 50+3 50+3 62+3 20+3 \n", "4 69+3 69+3 69+3 75+3 21+3 \n", "... ... ... ... ... ... \n", "19234 46+2 46+2 46+2 48+2 15+2 \n", "19235 44+2 44+2 44+2 48+2 14+2 \n", "19236 45+2 45+2 45+2 47+2 12+2 \n", "19237 26+2 26+2 26+2 32+2 15+2 \n", "19238 41+2 41+2 41+2 45+2 16+2 \n", "\n", " player_face_url \\\n", "0 https://cdn.sofifa.net/players/158/023/22_120.png \n", "1 https://cdn.sofifa.net/players/188/545/22_120.png \n", "2 https://cdn.sofifa.net/players/020/801/22_120.png \n", "3 https://cdn.sofifa.net/players/190/871/22_120.png \n", "4 https://cdn.sofifa.net/players/192/985/22_120.png \n", "... ... \n", "19234 https://cdn.sofifa.net/players/261/962/22_120.png \n", "19235 https://cdn.sofifa.net/players/262/040/22_120.png \n", "19236 https://cdn.sofifa.net/players/262/760/22_120.png \n", "19237 https://cdn.sofifa.net/players/262/820/22_120.png \n", "19238 https://cdn.sofifa.net/players/264/540/22_120.png \n", "\n", " club_logo_url \\\n", "0 https://cdn.sofifa.net/teams/73/60.png \n", "1 https://cdn.sofifa.net/teams/21/60.png \n", "2 https://cdn.sofifa.net/teams/11/60.png \n", "3 https://cdn.sofifa.net/teams/73/60.png \n", "4 https://cdn.sofifa.net/teams/10/60.png \n", "... ... \n", "19234 https://cdn.sofifa.net/teams/112541/60.png \n", "19235 https://cdn.sofifa.net/teams/445/60.png \n", "19236 https://cdn.sofifa.net/teams/111131/60.png \n", "19237 https://cdn.sofifa.net/teams/111131/60.png \n", "19238 https://cdn.sofifa.net/teams/113040/60.png \n", "\n", " club_flag_url \\\n", "0 https://cdn.sofifa.net/flags/fr.png \n", "1 https://cdn.sofifa.net/flags/de.png \n", "2 https://cdn.sofifa.net/flags/gb-eng.png \n", "3 https://cdn.sofifa.net/flags/fr.png \n", "4 https://cdn.sofifa.net/flags/gb-eng.png \n", "... ... \n", "19234 https://cdn.sofifa.net/flags/cn.png \n", "19235 https://cdn.sofifa.net/flags/ie.png \n", "19236 https://cdn.sofifa.net/flags/ie.png \n", "19237 https://cdn.sofifa.net/flags/ie.png \n", "19238 https://cdn.sofifa.net/flags/in.png \n", "\n", " nation_logo_url \\\n", "0 https://cdn.sofifa.net/teams/1369/60.png \n", "1 https://cdn.sofifa.net/teams/1353/60.png \n", "2 https://cdn.sofifa.net/teams/1354/60.png \n", "3 NaN \n", "4 https://cdn.sofifa.net/teams/1325/60.png \n", "... ... \n", "19234 NaN \n", "19235 NaN \n", "19236 NaN \n", "19237 NaN \n", "19238 NaN \n", "\n", " nation_flag_url \n", "0 https://cdn.sofifa.net/flags/ar.png \n", "1 https://cdn.sofifa.net/flags/pl.png \n", "2 https://cdn.sofifa.net/flags/pt.png \n", "3 https://cdn.sofifa.net/flags/br.png \n", "4 https://cdn.sofifa.net/flags/be.png \n", "... ... \n", "19234 https://cdn.sofifa.net/flags/cn.png \n", "19235 https://cdn.sofifa.net/flags/ie.png \n", "19236 https://cdn.sofifa.net/flags/ie.png \n", "19237 https://cdn.sofifa.net/flags/ie.png \n", "19238 https://cdn.sofifa.net/flags/in.png \n", "\n", "[19239 rows x 110 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "df = pd.read_csv('EX2.-players_22.csv')\n", "df" ] }, { "cell_type": "markdown", "id": "2358fa8de8891abf", "metadata": { "collapsed": false, "id": "2358fa8de8891abf" }, "source": [ "Queremos clasificar a los jugadores en 4 categorías:\n", "- **Estrella** (3): Valor de mercado superior a 50 m €.\n", "- **Promesa** (2): Valor de mercado entre 10M€ y 50M€\n", "- **Jugador de rotación** (1): Valor de mercado entre 1M€ y 10M€\n", "- **Jugador de cantera** (0): Valor de mercado inferior a 1M€\n", "\n", "Primero debemos preparar los datos, aprovechando la columna `value_eur` para crear la columna `categoría` que nos permitirá clasificar a los jugadores." ] }, { "cell_type": "code", "execution_count": null, "id": "8e2fb20a74c1563", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:38:52.344768Z", "start_time": "2024-01-08T14:38:51.709857Z" }, "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "id": "8e2fb20a74c1563", "outputId": "10633b88-97d9-4ed5-8c43-4e612dd22e7f" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "variable_name": "df" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sofifa_idplayer_urlshort_namelong_nameplayer_positionsoverallpotentialvalue_eurwage_eurage...cbrcbrbgkplayer_face_urlclub_logo_urlclub_flag_urlnation_logo_urlnation_flag_urlcategory
0158023https://sofifa.com/player/158023/lionel-messi/...L. MessiLionel Andrés Messi CuccittiniRW, ST, CF939378000000.0320000.034...50+350+361+319+3https://cdn.sofifa.net/players/158/023/22_120.pnghttps://cdn.sofifa.net/teams/73/60.pnghttps://cdn.sofifa.net/flags/fr.pnghttps://cdn.sofifa.net/teams/1369/60.pnghttps://cdn.sofifa.net/flags/ar.png3
1188545https://sofifa.com/player/188545/robert-lewand...R. LewandowskiRobert LewandowskiST9292119500000.0270000.032...60+360+361+319+3https://cdn.sofifa.net/players/188/545/22_120.pnghttps://cdn.sofifa.net/teams/21/60.pnghttps://cdn.sofifa.net/flags/de.pnghttps://cdn.sofifa.net/teams/1353/60.pnghttps://cdn.sofifa.net/flags/pl.png3
220801https://sofifa.com/player/20801/c-ronaldo-dos-...Cristiano RonaldoCristiano Ronaldo dos Santos AveiroST, LW919145000000.0270000.036...53+353+360+320+3https://cdn.sofifa.net/players/020/801/22_120.pnghttps://cdn.sofifa.net/teams/11/60.pnghttps://cdn.sofifa.net/flags/gb-eng.pnghttps://cdn.sofifa.net/teams/1354/60.pnghttps://cdn.sofifa.net/flags/pt.png2
3190871https://sofifa.com/player/190871/neymar-da-sil...Neymar JrNeymar da Silva Santos JúniorLW, CAM9191129000000.0270000.029...50+350+362+320+3https://cdn.sofifa.net/players/190/871/22_120.pnghttps://cdn.sofifa.net/teams/73/60.pnghttps://cdn.sofifa.net/flags/fr.pngNaNhttps://cdn.sofifa.net/flags/br.png3
4192985https://sofifa.com/player/192985/kevin-de-bruy...K. De BruyneKevin De BruyneCM, CAM9191125500000.0350000.030...69+369+375+321+3https://cdn.sofifa.net/players/192/985/22_120.pnghttps://cdn.sofifa.net/teams/10/60.pnghttps://cdn.sofifa.net/flags/gb-eng.pnghttps://cdn.sofifa.net/teams/1325/60.pnghttps://cdn.sofifa.net/flags/be.png3
..................................................................
19234261962https://sofifa.com/player/261962/defu-song/220002Song Defu宋德福CDM475270000.01000.022...46+246+248+215+2https://cdn.sofifa.net/players/261/962/22_120.pnghttps://cdn.sofifa.net/teams/112541/60.pnghttps://cdn.sofifa.net/flags/cn.pngNaNhttps://cdn.sofifa.net/flags/cn.png0
19235262040https://sofifa.com/player/262040/caoimhin-port...C. PorterCaoimhin PorterCM4759110000.0500.019...44+244+248+214+2https://cdn.sofifa.net/players/262/040/22_120.pnghttps://cdn.sofifa.net/teams/445/60.pnghttps://cdn.sofifa.net/flags/ie.pngNaNhttps://cdn.sofifa.net/flags/ie.png0
19236262760https://sofifa.com/player/262760/nathan-logue/...N. LogueNathan Logue-CunninghamCM4755100000.0500.021...45+245+247+212+2https://cdn.sofifa.net/players/262/760/22_120.pnghttps://cdn.sofifa.net/teams/111131/60.pnghttps://cdn.sofifa.net/flags/ie.pngNaNhttps://cdn.sofifa.net/flags/ie.png0
19237262820https://sofifa.com/player/262820/luke-rudden/2...L. RuddenLuke RuddenST4760110000.0500.019...26+226+232+215+2https://cdn.sofifa.net/players/262/820/22_120.pnghttps://cdn.sofifa.net/teams/111131/60.pnghttps://cdn.sofifa.net/flags/ie.pngNaNhttps://cdn.sofifa.net/flags/ie.png0
19238264540https://sofifa.com/player/264540/emanuel-lalch...E. LalchhanchhuahaEmanuel LalchhanchhuahaCAM4760110000.0500.019...41+241+245+216+2https://cdn.sofifa.net/players/264/540/22_120.pnghttps://cdn.sofifa.net/teams/113040/60.pnghttps://cdn.sofifa.net/flags/in.pngNaNhttps://cdn.sofifa.net/flags/in.png0
\n", "

19239 rows × 111 columns

\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " sofifa_id player_url \\\n", "0 158023 https://sofifa.com/player/158023/lionel-messi/... \n", "1 188545 https://sofifa.com/player/188545/robert-lewand... \n", "2 20801 https://sofifa.com/player/20801/c-ronaldo-dos-... \n", "3 190871 https://sofifa.com/player/190871/neymar-da-sil... \n", "4 192985 https://sofifa.com/player/192985/kevin-de-bruy... \n", "... ... ... \n", "19234 261962 https://sofifa.com/player/261962/defu-song/220002 \n", "19235 262040 https://sofifa.com/player/262040/caoimhin-port... \n", "19236 262760 https://sofifa.com/player/262760/nathan-logue/... \n", "19237 262820 https://sofifa.com/player/262820/luke-rudden/2... \n", "19238 264540 https://sofifa.com/player/264540/emanuel-lalch... \n", "\n", " short_name long_name \\\n", "0 L. Messi Lionel Andrés Messi Cuccittini \n", "1 R. Lewandowski Robert Lewandowski \n", "2 Cristiano Ronaldo Cristiano Ronaldo dos Santos Aveiro \n", "3 Neymar Jr Neymar da Silva Santos Júnior \n", "4 K. De Bruyne Kevin De Bruyne \n", "... ... ... \n", "19234 Song Defu 宋德福 \n", "19235 C. Porter Caoimhin Porter \n", "19236 N. Logue Nathan Logue-Cunningham \n", "19237 L. Rudden Luke Rudden \n", "19238 E. Lalchhanchhuaha Emanuel Lalchhanchhuaha \n", "\n", " player_positions overall potential value_eur wage_eur age ... \\\n", "0 RW, ST, CF 93 93 78000000.0 320000.0 34 ... \n", "1 ST 92 92 119500000.0 270000.0 32 ... \n", "2 ST, LW 91 91 45000000.0 270000.0 36 ... \n", "3 LW, CAM 91 91 129000000.0 270000.0 29 ... \n", "4 CM, CAM 91 91 125500000.0 350000.0 30 ... \n", "... ... ... ... ... ... ... ... \n", "19234 CDM 47 52 70000.0 1000.0 22 ... \n", "19235 CM 47 59 110000.0 500.0 19 ... \n", "19236 CM 47 55 100000.0 500.0 21 ... \n", "19237 ST 47 60 110000.0 500.0 19 ... \n", "19238 CAM 47 60 110000.0 500.0 19 ... \n", "\n", " cb rcb rb gk \\\n", "0 50+3 50+3 61+3 19+3 \n", "1 60+3 60+3 61+3 19+3 \n", "2 53+3 53+3 60+3 20+3 \n", "3 50+3 50+3 62+3 20+3 \n", "4 69+3 69+3 75+3 21+3 \n", "... ... ... ... ... \n", "19234 46+2 46+2 48+2 15+2 \n", "19235 44+2 44+2 48+2 14+2 \n", "19236 45+2 45+2 47+2 12+2 \n", "19237 26+2 26+2 32+2 15+2 \n", "19238 41+2 41+2 45+2 16+2 \n", "\n", " player_face_url \\\n", "0 https://cdn.sofifa.net/players/158/023/22_120.png \n", "1 https://cdn.sofifa.net/players/188/545/22_120.png \n", "2 https://cdn.sofifa.net/players/020/801/22_120.png \n", "3 https://cdn.sofifa.net/players/190/871/22_120.png \n", "4 https://cdn.sofifa.net/players/192/985/22_120.png \n", "... ... \n", "19234 https://cdn.sofifa.net/players/261/962/22_120.png \n", "19235 https://cdn.sofifa.net/players/262/040/22_120.png \n", "19236 https://cdn.sofifa.net/players/262/760/22_120.png \n", "19237 https://cdn.sofifa.net/players/262/820/22_120.png \n", "19238 https://cdn.sofifa.net/players/264/540/22_120.png \n", "\n", " club_logo_url \\\n", "0 https://cdn.sofifa.net/teams/73/60.png \n", "1 https://cdn.sofifa.net/teams/21/60.png \n", "2 https://cdn.sofifa.net/teams/11/60.png \n", "3 https://cdn.sofifa.net/teams/73/60.png \n", "4 https://cdn.sofifa.net/teams/10/60.png \n", "... ... \n", "19234 https://cdn.sofifa.net/teams/112541/60.png \n", "19235 https://cdn.sofifa.net/teams/445/60.png \n", "19236 https://cdn.sofifa.net/teams/111131/60.png \n", "19237 https://cdn.sofifa.net/teams/111131/60.png \n", "19238 https://cdn.sofifa.net/teams/113040/60.png \n", "\n", " club_flag_url \\\n", "0 https://cdn.sofifa.net/flags/fr.png \n", "1 https://cdn.sofifa.net/flags/de.png \n", "2 https://cdn.sofifa.net/flags/gb-eng.png \n", "3 https://cdn.sofifa.net/flags/fr.png \n", "4 https://cdn.sofifa.net/flags/gb-eng.png \n", "... ... \n", "19234 https://cdn.sofifa.net/flags/cn.png \n", "19235 https://cdn.sofifa.net/flags/ie.png \n", "19236 https://cdn.sofifa.net/flags/ie.png \n", "19237 https://cdn.sofifa.net/flags/ie.png \n", "19238 https://cdn.sofifa.net/flags/in.png \n", "\n", " nation_logo_url \\\n", "0 https://cdn.sofifa.net/teams/1369/60.png \n", "1 https://cdn.sofifa.net/teams/1353/60.png \n", "2 https://cdn.sofifa.net/teams/1354/60.png \n", "3 NaN \n", "4 https://cdn.sofifa.net/teams/1325/60.png \n", "... ... \n", "19234 NaN \n", "19235 NaN \n", "19236 NaN \n", "19237 NaN \n", "19238 NaN \n", "\n", " nation_flag_url category \n", "0 https://cdn.sofifa.net/flags/ar.png 3 \n", "1 https://cdn.sofifa.net/flags/pl.png 3 \n", "2 https://cdn.sofifa.net/flags/pt.png 2 \n", "3 https://cdn.sofifa.net/flags/br.png 3 \n", "4 https://cdn.sofifa.net/flags/be.png 3 \n", "... ... ... \n", "19234 https://cdn.sofifa.net/flags/cn.png 0 \n", "19235 https://cdn.sofifa.net/flags/ie.png 0 \n", "19236 https://cdn.sofifa.net/flags/ie.png 0 \n", "19237 https://cdn.sofifa.net/flags/ie.png 0 \n", "19238 https://cdn.sofifa.net/flags/in.png 0 \n", "\n", "[19239 rows x 111 columns]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def categorize(row):\n", " if row['value_eur'] >= 50000000:\n", " return 3\n", " elif row['value_eur'] >= 10000000:\n", " return 2\n", " elif row['value_eur'] >= 1000000:\n", " return 1\n", " else:\n", " return 0\n", "\n", "\n", "df['category'] = df.apply(categorize, axis=1)\n", "df" ] }, { "cell_type": "markdown", "id": "f74b6cf5d4e947fb", "metadata": { "collapsed": false, "id": "f74b6cf5d4e947fb" }, "source": [ "Finalmente, antes de entrenar los modelos, debemos separar los datos en un conjunto de entrenamiento y un conjunto de pruebas y pasarlos al formato X e Y." ] }, { "cell_type": "code", "execution_count": null, "id": "c10f846155bca29f", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:38:52.922920Z", "start_time": "2024-01-08T14:38:52.285720Z" }, "id": "c10f846155bca29f" }, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "\n", "# pandas 3 ya no admite rellenar con 0 una columna de texto: rellenamos solo\n", "# las numericas (las de texto se descartan justo despues, en `to_drop`).\n", "numericas = df.select_dtypes(include='number').columns\n", "df[numericas] = df[numericas].fillna(0)\n", "\n", "to_drop = ['category', 'value_eur',\n", " 'sofifa_id', 'player_url', 'short_name', 'long_name', 'player_positions', 'dob',\n", " 'club_name', 'club_loaned_from', 'league_name', 'club_position', 'nation_position', 'club_joined', 'nationality_name', 'preferred_foot', 'work_rate',\n", " 'body_type', 'real_face', 'player_tags', 'player_traits',\n", " 'nation_logo_url', 'nation_flag_url', 'ls', 'st', 'rs', 'lw', 'lf', 'cf',\n", " 'rf', 'rw', 'lam', 'cam', 'ram', 'lm', 'lcm', 'cm', 'rcm', 'rm', 'lwb', 'ldm',\n", " 'cdm', 'rdm', 'rwb', 'lb', 'lcb', 'cb', 'rcb', 'rb', 'gk',\n", " 'player_face_url', 'club_logo_url', 'club_flag_url', 'nation_logo_url', 'nation_flag_url']\n", "\n", "X = df.drop(to_drop, axis=1)\n", "y = df['category']\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)" ] }, { "cell_type": "markdown", "id": "584761094b2514e1", "metadata": { "collapsed": false, "id": "584761094b2514e1" }, "source": [ "## Human Learn (HL)\n", "\n", "Usaremos Human Learn para estimar el valor de mercado de los jugadores. Crearemos una `FunctionClassifier` para hacer una primera estimación del valor de mercado de los jugadores.\n", "\n", "### Classificador simple\n", "\n", "Para eso tienes que crear una función simple utilizando un parámetro que nos permita clasificar a los jugadores en estas categorías y pasarlo al `FunctionClassifier`. Una vez que se crea el clasificador, entrenarlo con los datos de entrenamiento (`Fit`) y muestra su precisión con los datos de prueba (`score``)." ] }, { "cell_type": "code", "execution_count": null, "id": "79e159293c9db121", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:38:53.141610Z", "start_time": "2024-01-08T14:38:52.463596Z" }, "colab": { "base_uri": "https://localhost:8080/" }, "id": "79e159293c9db121", "outputId": "cd66251e-588a-4289-ad2b-a7cc8259812f" }, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, ..., 0, 0, 0])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "from hulearn.classification import FunctionClassifier\n", "# Completa la celda" ] }, { "cell_type": "code", "execution_count": null, "id": "3e12058f60426477", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:38:53.258332Z", "start_time": "2024-01-08T14:38:53.087202Z" }, "colab": { "base_uri": "https://localhost:8080/" }, "id": "3e12058f60426477", "outputId": "07518be8-360e-400b-d697-fb0cfaa1d898" }, "outputs": [ { "data": { "text/plain": [ "0.7305093555093555" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Completa la celda" ] }, { "cell_type": "markdown", "id": "3210be86fa821709", "metadata": { "collapsed": false, "id": "3210be86fa821709" }, "source": [ "Intenta obtener una precisión de más de 0.8. Puedes aprovechar `GridSearchCV` para encontrar los mejores parámetros para la función de clasificación.\n", "\n", "### Clasificador con más parámetros\n", "\n", "Los clasificadores de un solo parámetro son muy simples y no nos permiten aprovechar todo el potencial del aprendizaje humano. Por esta razón, haz una exploración interactiva de los datos para encontrar los parámetros que nos permiten clasificar mejor a los jugadores. Puedes usar `Parallel_Coordinates` para hacer esta exploración." ] }, { "cell_type": "markdown", "id": "f24e96b551971603", "metadata": { "collapsed": false, "id": "f24e96b551971603" }, "source": [ "## FIGS\n", "\n", "Como hemos visto en el ejemplo del Titanic, `FIGS` genera reglas fáciles de interpretar para clasificar los datos. Pero `FIGS` no permite una clasificación múltiple directamente, por lo que simplificaremos el problema y solo clasificaremos a los jugadores en dos categorías: **Estrella** (3) y **No estrella** (0, 1, 2).\n", "\n", "Primero, crearemos un DataFrame de datos adaptado, donde los jugadores con la Categoría 3 tendrán la Categoría 1 y los jugadores con la Categoría 0, 1 o 2 tendrán la Categoría 0." ] }, { "cell_type": "code", "execution_count": null, "id": "227259ac0ac2c25f", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:38:53.532999Z", "start_time": "2024-01-08T14:38:53.212237Z" }, "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "id": "227259ac0ac2c25f", "outputId": "0b10718f-7919-46df-a5d0-f82f043c62aa" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "variable_name": "df_skope" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sofifa_idplayer_urlshort_namelong_nameplayer_positionsoverallpotentialvalue_eurwage_eurage...cbrcbrbgkplayer_face_urlclub_logo_urlclub_flag_urlnation_logo_urlnation_flag_urlcategory
0158023https://sofifa.com/player/158023/lionel-messi/...L. MessiLionel Andrés Messi CuccittiniRW, ST, CF939378000000.0320000.034...50+350+361+319+3https://cdn.sofifa.net/players/158/023/22_120.pnghttps://cdn.sofifa.net/teams/73/60.pnghttps://cdn.sofifa.net/flags/fr.pnghttps://cdn.sofifa.net/teams/1369/60.pnghttps://cdn.sofifa.net/flags/ar.png1
1188545https://sofifa.com/player/188545/robert-lewand...R. LewandowskiRobert LewandowskiST9292119500000.0270000.032...60+360+361+319+3https://cdn.sofifa.net/players/188/545/22_120.pnghttps://cdn.sofifa.net/teams/21/60.pnghttps://cdn.sofifa.net/flags/de.pnghttps://cdn.sofifa.net/teams/1353/60.pnghttps://cdn.sofifa.net/flags/pl.png1
220801https://sofifa.com/player/20801/c-ronaldo-dos-...Cristiano RonaldoCristiano Ronaldo dos Santos AveiroST, LW919145000000.0270000.036...53+353+360+320+3https://cdn.sofifa.net/players/020/801/22_120.pnghttps://cdn.sofifa.net/teams/11/60.pnghttps://cdn.sofifa.net/flags/gb-eng.pnghttps://cdn.sofifa.net/teams/1354/60.pnghttps://cdn.sofifa.net/flags/pt.png0
3190871https://sofifa.com/player/190871/neymar-da-sil...Neymar JrNeymar da Silva Santos JúniorLW, CAM9191129000000.0270000.029...50+350+362+320+3https://cdn.sofifa.net/players/190/871/22_120.pnghttps://cdn.sofifa.net/teams/73/60.pnghttps://cdn.sofifa.net/flags/fr.png0https://cdn.sofifa.net/flags/br.png1
4192985https://sofifa.com/player/192985/kevin-de-bruy...K. De BruyneKevin De BruyneCM, CAM9191125500000.0350000.030...69+369+375+321+3https://cdn.sofifa.net/players/192/985/22_120.pnghttps://cdn.sofifa.net/teams/10/60.pnghttps://cdn.sofifa.net/flags/gb-eng.pnghttps://cdn.sofifa.net/teams/1325/60.pnghttps://cdn.sofifa.net/flags/be.png1
..................................................................
19234261962https://sofifa.com/player/261962/defu-song/220002Song Defu宋德福CDM475270000.01000.022...46+246+248+215+2https://cdn.sofifa.net/players/261/962/22_120.pnghttps://cdn.sofifa.net/teams/112541/60.pnghttps://cdn.sofifa.net/flags/cn.png0https://cdn.sofifa.net/flags/cn.png0
19235262040https://sofifa.com/player/262040/caoimhin-port...C. PorterCaoimhin PorterCM4759110000.0500.019...44+244+248+214+2https://cdn.sofifa.net/players/262/040/22_120.pnghttps://cdn.sofifa.net/teams/445/60.pnghttps://cdn.sofifa.net/flags/ie.png0https://cdn.sofifa.net/flags/ie.png0
19236262760https://sofifa.com/player/262760/nathan-logue/...N. LogueNathan Logue-CunninghamCM4755100000.0500.021...45+245+247+212+2https://cdn.sofifa.net/players/262/760/22_120.pnghttps://cdn.sofifa.net/teams/111131/60.pnghttps://cdn.sofifa.net/flags/ie.png0https://cdn.sofifa.net/flags/ie.png0
19237262820https://sofifa.com/player/262820/luke-rudden/2...L. RuddenLuke RuddenST4760110000.0500.019...26+226+232+215+2https://cdn.sofifa.net/players/262/820/22_120.pnghttps://cdn.sofifa.net/teams/111131/60.pnghttps://cdn.sofifa.net/flags/ie.png0https://cdn.sofifa.net/flags/ie.png0
19238264540https://sofifa.com/player/264540/emanuel-lalch...E. LalchhanchhuahaEmanuel LalchhanchhuahaCAM4760110000.0500.019...41+241+245+216+2https://cdn.sofifa.net/players/264/540/22_120.pnghttps://cdn.sofifa.net/teams/113040/60.pnghttps://cdn.sofifa.net/flags/in.png0https://cdn.sofifa.net/flags/in.png0
\n", "

19239 rows × 111 columns

\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " sofifa_id player_url \\\n", "0 158023 https://sofifa.com/player/158023/lionel-messi/... \n", "1 188545 https://sofifa.com/player/188545/robert-lewand... \n", "2 20801 https://sofifa.com/player/20801/c-ronaldo-dos-... \n", "3 190871 https://sofifa.com/player/190871/neymar-da-sil... \n", "4 192985 https://sofifa.com/player/192985/kevin-de-bruy... \n", "... ... ... \n", "19234 261962 https://sofifa.com/player/261962/defu-song/220002 \n", "19235 262040 https://sofifa.com/player/262040/caoimhin-port... \n", "19236 262760 https://sofifa.com/player/262760/nathan-logue/... \n", "19237 262820 https://sofifa.com/player/262820/luke-rudden/2... \n", "19238 264540 https://sofifa.com/player/264540/emanuel-lalch... \n", "\n", " short_name long_name \\\n", "0 L. Messi Lionel Andrés Messi Cuccittini \n", "1 R. Lewandowski Robert Lewandowski \n", "2 Cristiano Ronaldo Cristiano Ronaldo dos Santos Aveiro \n", "3 Neymar Jr Neymar da Silva Santos Júnior \n", "4 K. De Bruyne Kevin De Bruyne \n", "... ... ... \n", "19234 Song Defu 宋德福 \n", "19235 C. Porter Caoimhin Porter \n", "19236 N. Logue Nathan Logue-Cunningham \n", "19237 L. Rudden Luke Rudden \n", "19238 E. Lalchhanchhuaha Emanuel Lalchhanchhuaha \n", "\n", " player_positions overall potential value_eur wage_eur age ... \\\n", "0 RW, ST, CF 93 93 78000000.0 320000.0 34 ... \n", "1 ST 92 92 119500000.0 270000.0 32 ... \n", "2 ST, LW 91 91 45000000.0 270000.0 36 ... \n", "3 LW, CAM 91 91 129000000.0 270000.0 29 ... \n", "4 CM, CAM 91 91 125500000.0 350000.0 30 ... \n", "... ... ... ... ... ... ... ... \n", "19234 CDM 47 52 70000.0 1000.0 22 ... \n", "19235 CM 47 59 110000.0 500.0 19 ... \n", "19236 CM 47 55 100000.0 500.0 21 ... \n", "19237 ST 47 60 110000.0 500.0 19 ... \n", "19238 CAM 47 60 110000.0 500.0 19 ... \n", "\n", " cb rcb rb gk \\\n", "0 50+3 50+3 61+3 19+3 \n", "1 60+3 60+3 61+3 19+3 \n", "2 53+3 53+3 60+3 20+3 \n", "3 50+3 50+3 62+3 20+3 \n", "4 69+3 69+3 75+3 21+3 \n", "... ... ... ... ... \n", "19234 46+2 46+2 48+2 15+2 \n", "19235 44+2 44+2 48+2 14+2 \n", "19236 45+2 45+2 47+2 12+2 \n", "19237 26+2 26+2 32+2 15+2 \n", "19238 41+2 41+2 45+2 16+2 \n", "\n", " player_face_url \\\n", "0 https://cdn.sofifa.net/players/158/023/22_120.png \n", "1 https://cdn.sofifa.net/players/188/545/22_120.png \n", "2 https://cdn.sofifa.net/players/020/801/22_120.png \n", "3 https://cdn.sofifa.net/players/190/871/22_120.png \n", "4 https://cdn.sofifa.net/players/192/985/22_120.png \n", "... ... \n", "19234 https://cdn.sofifa.net/players/261/962/22_120.png \n", "19235 https://cdn.sofifa.net/players/262/040/22_120.png \n", "19236 https://cdn.sofifa.net/players/262/760/22_120.png \n", "19237 https://cdn.sofifa.net/players/262/820/22_120.png \n", "19238 https://cdn.sofifa.net/players/264/540/22_120.png \n", "\n", " club_logo_url \\\n", "0 https://cdn.sofifa.net/teams/73/60.png \n", "1 https://cdn.sofifa.net/teams/21/60.png \n", "2 https://cdn.sofifa.net/teams/11/60.png \n", "3 https://cdn.sofifa.net/teams/73/60.png \n", "4 https://cdn.sofifa.net/teams/10/60.png \n", "... ... \n", "19234 https://cdn.sofifa.net/teams/112541/60.png \n", "19235 https://cdn.sofifa.net/teams/445/60.png \n", "19236 https://cdn.sofifa.net/teams/111131/60.png \n", "19237 https://cdn.sofifa.net/teams/111131/60.png \n", "19238 https://cdn.sofifa.net/teams/113040/60.png \n", "\n", " club_flag_url \\\n", "0 https://cdn.sofifa.net/flags/fr.png \n", "1 https://cdn.sofifa.net/flags/de.png \n", "2 https://cdn.sofifa.net/flags/gb-eng.png \n", "3 https://cdn.sofifa.net/flags/fr.png \n", "4 https://cdn.sofifa.net/flags/gb-eng.png \n", "... ... \n", "19234 https://cdn.sofifa.net/flags/cn.png \n", "19235 https://cdn.sofifa.net/flags/ie.png \n", "19236 https://cdn.sofifa.net/flags/ie.png \n", "19237 https://cdn.sofifa.net/flags/ie.png \n", "19238 https://cdn.sofifa.net/flags/in.png \n", "\n", " nation_logo_url \\\n", "0 https://cdn.sofifa.net/teams/1369/60.png \n", "1 https://cdn.sofifa.net/teams/1353/60.png \n", "2 https://cdn.sofifa.net/teams/1354/60.png \n", "3 0 \n", "4 https://cdn.sofifa.net/teams/1325/60.png \n", "... ... \n", "19234 0 \n", "19235 0 \n", "19236 0 \n", "19237 0 \n", "19238 0 \n", "\n", " nation_flag_url category \n", "0 https://cdn.sofifa.net/flags/ar.png 1 \n", "1 https://cdn.sofifa.net/flags/pl.png 1 \n", "2 https://cdn.sofifa.net/flags/pt.png 0 \n", "3 https://cdn.sofifa.net/flags/br.png 1 \n", "4 https://cdn.sofifa.net/flags/be.png 1 \n", "... ... ... \n", "19234 https://cdn.sofifa.net/flags/cn.png 0 \n", "19235 https://cdn.sofifa.net/flags/ie.png 0 \n", "19236 https://cdn.sofifa.net/flags/ie.png 0 \n", "19237 https://cdn.sofifa.net/flags/ie.png 0 \n", "19238 https://cdn.sofifa.net/flags/in.png 0 \n", "\n", "[19239 rows x 111 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_skope = df.copy()\n", "# Completa la celda" ] }, { "cell_type": "markdown", "id": "a11aa2d73399d54b", "metadata": { "collapsed": false, "id": "a11aa2d73399d54b" }, "source": [ "Para entrenar el clasificador, primero debemos separar los datos en un conjunto de entrenamiento y un conjunto de pruebas y pasarlos al formato X e Y." ] }, { "cell_type": "code", "execution_count": null, "id": "6d5ee24fe03aea26", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:38:53.592676Z", "start_time": "2024-01-08T14:38:53.334122Z" }, "id": "6d5ee24fe03aea26" }, "outputs": [], "source": [ "X = df_skope.drop(to_drop, axis=1)\n", "y = df_skope['category']\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)" ] }, { "cell_type": "code", "execution_count": null, "id": "fd405e39b8c0948f", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:40:45.150380Z", "start_time": "2024-01-08T14:40:32.855540Z" }, "colab": { "base_uri": "https://localhost:8080/" }, "id": "fd405e39b8c0948f", "outputId": "bbeff948-ca0a-4ce9-e3a9-aa38d6da6c6d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting imodels\n", " Downloading imodels-2.0.0-py3-none-any.whl.metadata (30 kB)\n", "Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (from imodels) (3.8.0)\n", "Requirement already satisfied: mlxtend>=0.18.0 in /usr/local/lib/python3.10/dist-packages (from imodels) (0.23.3)\n", "Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from imodels) (1.26.4)\n", "Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from imodels) (2.2.2)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from imodels) (2.32.3)\n", "Requirement already satisfied: scipy in /usr/local/lib/python3.10/dist-packages (from imodels) (1.13.1)\n", "Requirement already satisfied: scikit-learn>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from imodels) (1.5.2)\n", "Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from imodels) (4.66.6)\n", "Requirement already satisfied: joblib>=0.13.2 in /usr/local/lib/python3.10/dist-packages (from mlxtend>=0.18.0->imodels) (1.4.2)\n", "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->imodels) (1.3.1)\n", "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib->imodels) (0.12.1)\n", "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->imodels) (4.55.1)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->imodels) (1.4.7)\n", "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->imodels) (24.2)\n", "Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->imodels) (11.0.0)\n", "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->imodels) (3.2.0)\n", "Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib->imodels) (2.8.2)\n", "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->imodels) (2024.2)\n", "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas->imodels) (2024.2)\n", "Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=1.2.0->imodels) (3.5.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->imodels) (3.4.0)\n", "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->imodels) (3.10)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->imodels) (2.2.3)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->imodels) (2024.8.30)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib->imodels) (1.16.0)\n", "Downloading imodels-2.0.0-py3-none-any.whl (243 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m243.1/243.1 kB\u001b[0m \u001b[31m11.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hInstalling collected packages: imodels\n", "Successfully installed imodels-2.0.0\n" ] } ], "source": [ "# instal·lem la llibreria\n", "%pip install imodels" ] }, { "cell_type": "code", "execution_count": null, "id": "fb6d4bad9a488541", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:40:45.209239Z", "start_time": "2024-01-08T14:40:45.160055Z" }, "id": "fb6d4bad9a488541" }, "outputs": [], "source": [ "# Carreguem la llibreria\n", "from imodels import FIGSClassifier" ] }, { "cell_type": "markdown", "id": "e91e73182b417a51", "metadata": { "collapsed": false, "id": "e91e73182b417a51" }, "source": [ "Cree un clasificador de `imodels` y entrenalo solo con los datos de entrenamiento (`Fit`). Luego muestra la precisión del clasificador con los datos de prueba (`score`)." ] }, { "cell_type": "code", "execution_count": null, "id": "3c7c51ae1cc81170", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:41:15.979816Z", "start_time": "2024-01-08T14:41:15.948515Z" }, "colab": { "base_uri": "https://localhost:8080/" }, "id": "3c7c51ae1cc81170", "outputId": "5485c030-affb-4db3-988b-2efe49bddd1b" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.\n", " and should_run_async(code)\n" ] } ], "source": [ "# Completa la celda creando un clasificador con FIGS" ] }, { "cell_type": "code", "execution_count": null, "id": "847d9667ef53ffcf", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:41:20.232848Z", "start_time": "2024-01-08T14:41:18.668011Z" }, "colab": { "base_uri": "https://localhost:8080/", "height": 313 }, "id": "847d9667ef53ffcf", "outputId": "410cf6b6-8e3f-4fda-ec66-e4b2670210df" }, "outputs": [], "source": [ "# Completa la celda entrenando el modelo" ] }, { "cell_type": "code", "execution_count": null, "id": "ab32e1ce2881d3bb", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:41:26.035122Z", "start_time": "2024-01-08T14:41:25.991891Z" }, "colab": { "base_uri": "https://localhost:8080/" }, "id": "ab32e1ce2881d3bb", "outputId": "2938ef09-dcae-4002-d223-4d823328efe6" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.\n", " and should_run_async(code)\n" ] }, { "data": { "text/plain": [ "array([0, 0, 0, ..., 0, 0, 0])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# completa la celda realizando la predicción con el modelo recien entrenado" ] }, { "cell_type": "code", "execution_count": null, "id": "6c0b7ea905e5f5cf", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:41:30.241724Z", "start_time": "2024-01-08T14:41:30.208648Z" }, "colab": { "base_uri": "https://localhost:8080/" }, "id": "6c0b7ea905e5f5cf", "outputId": "714f79f8-8a99-4bd1-df80-6d047bee3bb5" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.\n", " and should_run_async(code)\n" ] }, { "data": { "text/plain": [ "0.998960498960499" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# completa la celda calculando el `score` obtenido por el modelo" ] }, { "cell_type": "markdown", "id": "7aeb6f7fe951ce98", "metadata": { "collapsed": false, "id": "7aeb6f7fe951ce98" }, "source": [ "Podemos ver que el clasificador ha logrado una precisión muy alta. Esto se debe al hecho de que hay datos que son muy esclarecedores, como la clausula de rescisión, el valor general, el potencial, el salario o el valor de mercado. Es por eso que debemos tener en cuenta que estos datos no estarán disponibles para los jugadores emergentes y, por lo tanto, nuestro clasificador no será tan preciso.\n", "\n", "Eliminaremos todos estos datos y entrenaremos nuevamente el clasificador." ] }, { "cell_type": "code", "execution_count": null, "id": "b6a5ce0997083186", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:49:26.832457Z", "start_time": "2024-01-08T14:49:25.650644Z" }, "colab": { "base_uri": "https://localhost:8080/", "height": 313 }, "id": "b6a5ce0997083186", "outputId": "ad30d99d-5353-4dbc-9413-d9821030a5f2" }, "outputs": [], "source": [ "# completa la celda, modificando el dataframe, separando los datos en train y test, y genera un nuevo clasificador y entrenalo" ] }, { "cell_type": "code", "execution_count": null, "id": "2e4eb725234c91ea", "metadata": { "ExecuteTime": { "end_time": "2024-01-08T14:49:26.901906Z", "start_time": "2024-01-08T14:49:26.839592Z" }, "colab": { "base_uri": "https://localhost:8080/" }, "id": "2e4eb725234c91ea", "outputId": "c60bac04-0977-4a31-e214-a54864503eb0" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.\n", " and should_run_async(code)\n" ] }, { "data": { "text/plain": [ "0.9968814968814969" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# completa la celda realizando la predicción con el modelo recien entrenado y calculando el `score` obtenido por el modelo" ] }, { "cell_type": "markdown", "id": "ObWEX0pwa6So", "metadata": { "id": "ObWEX0pwa6So" }, "source": [ "# Entrega\n", "\n", "Una vez completado el Notebook y ejecutado completamente (comprueba que funcionan todas las celdas). Envialo a la tarea correspondiente de AULES." ] } ], "metadata": { "colab": { "provenance": [], "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }