{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":63056,"databundleVersionId":9094797,"sourceType":"competition"},{"sourceId":84933,"databundleVersionId":10843083,"sourceType":"competition"}],"dockerImageVersionId":30887,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:20:05.781676Z","iopub.execute_input":"2025-02-17T10:20:05.781962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\ndata = pd.read_csv('/kaggle/input/itobos-2024-detection/_train/_train/metadata.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:39:43.261148Z","iopub.execute_input":"2025-02-17T10:39:43.261466Z","iopub.status.idle":"2025-02-17T10:39:43.280927Z","shell.execute_reply.started":"2025-02-17T10:39:43.26144Z","shell.execute_reply":"2025-02-17T10:39:43.280241Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Anàlisi de les dades","metadata":{}},{"cell_type":"code","source":"nInstances, nAttributes = data.shape\nprint(nInstances, nAttributes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:39:46.044136Z","iopub.execute_input":"2025-02-17T10:39:46.044421Z","iopub.status.idle":"2025-02-17T10:39:46.049391Z","shell.execute_reply.started":"2025-02-17T10:39:46.044397Z","shell.execute_reply":"2025-02-17T10:39:46.04827Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:32:51.32727Z","iopub.execute_input":"2025-02-17T10:32:51.327576Z","iopub.status.idle":"2025-02-17T10:32:51.35184Z","shell.execute_reply.started":"2025-02-17T10:32:51.327531Z","shell.execute_reply":"2025-02-17T10:32:51.350893Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"null_columns = data.columns[data.isnull().any()]\ndata[null_columns].isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:39:49.925873Z","iopub.execute_input":"2025-02-17T10:39:49.92615Z","iopub.status.idle":"2025-02-17T10:39:49.935088Z","shell.execute_reply.started":"2025-02-17T10:39:49.926129Z","shell.execute_reply":"2025-02-17T10:39:49.934107Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Countplot de les edats","metadata":{}},{"cell_type":"code","source":"edat = data['age_at_baseline'].tolist()\ngraf = sns.countplot(data['age_at_baseline'], x=edat, order = sorted(data[\"age_at_baseline\"].unique()))\ngraf.set_xticklabels(graf.get_xticklabels(), rotation = 90, ha = \"right\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:39:53.006845Z","iopub.execute_input":"2025-02-17T10:39:53.007142Z","iopub.status.idle":"2025-02-17T10:39:53.374001Z","shell.execute_reply.started":"2025-02-17T10:39:53.007118Z","shell.execute_reply":"2025-02-17T10:39:53.373039Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"unknown_count = (data['age_at_baseline'] == \"Unknown\").sum()\nprint(f\"Nombre de valors 'Unknown': {unknown_count}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:39:57.31544Z","iopub.execute_input":"2025-02-17T10:39:57.31574Z","iopub.status.idle":"2025-02-17T10:39:57.321277Z","shell.execute_reply.started":"2025-02-17T10:39:57.315715Z","shell.execute_reply":"2025-02-17T10:39:57.320327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data['age_at_baseline'] = pd.to_numeric(data['age_at_baseline'], errors='coerce')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:40:10.201954Z","iopub.execute_input":"2025-02-17T10:40:10.202244Z","iopub.status.idle":"2025-02-17T10:40:10.206855Z","shell.execute_reply.started":"2025-02-17T10:40:10.202211Z","shell.execute_reply":"2025-02-17T10:40:10.206015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data['edat'] = pd.cut(data['age_at_baseline'], bins=range(0, 101, 10), right=False)\ndata['edat'] = data['edat'].astype(str)\n\nplt.figure(figsize=(10, 5))\ngraf = sns.countplot(data=data, x=\"edat\", order=sorted(data[\"edat\"].unique()))\ngraf.set_xticklabels(graf.get_xticklabels(), rotation=45, ha=\"right\")\ngraf.set_xlabel(\"Grup d'edat\")\ngraf.set_ylabel(\"Freqüència\")\ngraf.set_title(\"Distribució d'edats agrupades per dècades\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:40:13.290955Z","iopub.execute_input":"2025-02-17T10:40:13.291243Z","iopub.status.idle":"2025-02-17T10:40:13.468496Z","shell.execute_reply.started":"2025-02-17T10:40:13.29122Z","shell.execute_reply":"2025-02-17T10:40:13.467634Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Pie chart de les edats, per veure les edats agrupades cada 10 anys i en gràfic de sectors","metadata":{}},{"cell_type":"code","source":"bins = range(0, 101, 10)\nlabels = [f\"{i}-{i+9}\" for i in bins[:-1]]\ndata['edat'] = pd.cut(data['age_at_baseline'], bins=bins, labels=labels, right=False)\n\ngrups = data['edat'].value_counts()\n\nplt.figure(figsize=(12, 10))  \nplt.pie(grups, labels=grups.index, autopct='%1.1f%%', startangle=90)\nplt.title(\"Distribució d'edats per grups (cada 10 anys)\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:43:46.823137Z","iopub.execute_input":"2025-02-17T10:43:46.823455Z","iopub.status.idle":"2025-02-17T10:43:46.999505Z","shell.execute_reply.started":"2025-02-17T10:43:46.82343Z","shell.execute_reply":"2025-02-17T10:43:46.998659Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Boxplot de les edats","metadata":{}},{"cell_type":"code","source":"data['age_at_baseline'] = data['age_at_baseline'].replace('Unknown', 0.0)\ndata['age_at_baseline'].head()\ndata['age_at_baseline'] = pd.to_numeric(data['age_at_baseline'], errors='coerce')\nsns.boxplot(x=data['age_at_baseline'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:43:57.635477Z","iopub.execute_input":"2025-02-17T10:43:57.635806Z","iopub.status.idle":"2025-02-17T10:43:57.747616Z","shell.execute_reply.started":"2025-02-17T10:43:57.635778Z","shell.execute_reply":"2025-02-17T10:43:57.746816Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Anàlisi parts del cos","metadata":{}},{"cell_type":"code","source":"part_cos = data['body_part'].tolist()\ngraf2 = sns.countplot(data['body_part'], x=part_cos)\ngraf2.set_xticklabels(graf2.get_xticklabels(), rotation = 45, ha = \"right\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:44:19.753018Z","iopub.execute_input":"2025-02-17T10:44:19.753345Z","iopub.status.idle":"2025-02-17T10:44:20.047999Z","shell.execute_reply.started":"2025-02-17T10:44:19.753319Z","shell.execute_reply":"2025-02-17T10:44:20.047016Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Gràfic de barres horitzontal","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nsns.barplot(x=data['body_part'].value_counts().values, y=data['body_part'].value_counts().index)\nplt.xlabel(\"Freqüència\")\nplt.ylabel(\"Part del cos\")\nplt.title(\"Distribució de les parts del cos\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:44:28.602751Z","iopub.execute_input":"2025-02-17T10:44:28.603056Z","iopub.status.idle":"2025-02-17T10:44:28.846687Z","shell.execute_reply.started":"2025-02-17T10:44:28.603031Z","shell.execute_reply":"2025-02-17T10:44:28.84575Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"A continuació, fem una agrupació dels diferents grups a els grups general on només tenim left leg, left arm, right leg, right arm, torso i unknown","metadata":{}},{"cell_type":"code","source":"# Mapeig dels valors originals als grups desitjats\ngroup_map = {\n    'Right Arm - Lower': 'Right Arm',\n    'Right Arm - Upper': 'Right Arm',\n    'Left Arm - Lower': 'Left Arm',\n    'Left Arm - Upper': 'Left Arm',\n    'Right Leg - Lower': 'Right Leg',\n    'Right Leg - Upper': 'Right Leg',\n    'Left Leg - Upper': 'Left Leg',\n    'Left Leg - Lower': 'Left Leg',\n    'Torso': 'Torso',\n    'Unknown': 'Unknown',\n    'Left Leg': 'Left Leg',\n    'Left Arm': 'Left Arm',\n    'Right Leg': 'Right Leg',\n    'Right Arm': 'Right Arm'\n}\n\n# Apliquem el mapeig a les dades\ndata['body_part_grouped'] = data['body_part'].map(group_map)\n\n# Filtrem només els grups que ens interessen (Torso, Left Leg, Left Arm, Right Leg, Right Arm)\nfiltered_data = data[data['body_part_grouped'].isin(['Torso', 'Left Leg', 'Left Arm', 'Right Leg', 'Right Arm'])]\n\n# Gràfic de barres\nplt.figure(figsize=(10, 6))\ngraf2 = sns.countplot(data=filtered_data, x='body_part_grouped')\ngraf2.set_xticklabels(graf2.get_xticklabels(), rotation=45, ha=\"right\")\ngraf2.set_xlabel(\"Part de cos\")\ngraf2.set_ylabel(\"Freqüència\")\ngraf2.set_title(\"Distribució de parts del cos agrupades\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:54:46.334527Z","iopub.execute_input":"2025-02-17T10:54:46.334852Z","iopub.status.idle":"2025-02-17T10:54:46.518069Z","shell.execute_reply.started":"2025-02-17T10:54:46.334828Z","shell.execute_reply":"2025-02-17T10:54:46.517195Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Pie chart","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\ndata['body_part_grouped'].value_counts().plot.pie(autopct='%1.1f%%', startangle=90, cmap=\"tab10\")\nplt.ylabel(\"\")  \nplt.title(\"Distribució percentual de les parts del cos\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:55:15.61291Z","iopub.execute_input":"2025-02-17T10:55:15.613212Z","iopub.status.idle":"2025-02-17T10:55:15.754787Z","shell.execute_reply.started":"2025-02-17T10:55:15.613189Z","shell.execute_reply":"2025-02-17T10:55:15.753904Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"No és un bon mètode perquè hi ha molts grups, però si ho fem amb el data_grouped si que funciona com un bon mètode","metadata":{}},{"cell_type":"markdown","source":"Sun damage level analisi","metadata":{}},{"cell_type":"code","source":"valors_sun_damage = data['sun_damage_level'].unique()\n\nprint(valors_sun_damage)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:56:51.171438Z","iopub.execute_input":"2025-02-17T10:56:51.171933Z","iopub.status.idle":"2025-02-17T10:56:51.177234Z","shell.execute_reply.started":"2025-02-17T10:56:51.171904Z","shell.execute_reply":"2025-02-17T10:56:51.176446Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\ngraf3 = sns.countplot(data=data, x='sun_damage_level')\ngraf3.set_xticklabels(graf3.get_xticklabels(), rotation=0, ha=\"right\")\ngraf3.set_xlabel(\"Nivell de dany solar\")\ngraf3.set_ylabel(\"Freqüència\")\ngraf3.set_title(\"Distribució del nivell de dany solar\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T10:57:47.22897Z","iopub.execute_input":"2025-02-17T10:57:47.22927Z","iopub.status.idle":"2025-02-17T10:57:47.381635Z","shell.execute_reply.started":"2025-02-17T10:57:47.229246Z","shell.execute_reply":"2025-02-17T10:57:47.380803Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Combinació del sun_damage_level i body_part_grouped","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\ngraf4 = sns.countplot(data=data, x='body_part_grouped', hue='sun_damage_level')\n\ngraf4.set_xticklabels(graf4.get_xticklabels(), rotation=45, ha=\"right\")\ngraf4.set_xlabel(\"Part de cos\")\ngraf4.set_ylabel(\"Freqüència\")\ngraf4.set_title(\"Distribució de dany solar per part del cos\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T11:02:00.694966Z","iopub.execute_input":"2025-02-17T11:02:00.695309Z","iopub.status.idle":"2025-02-17T11:02:00.937335Z","shell.execute_reply.started":"2025-02-17T11:02:00.695281Z","shell.execute_reply":"2025-02-17T11:02:00.936416Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Aquest gràfic és molt útil perquè podem veure per cada part del cos el nivell de dany solar, com podem veure en totes les aprts destaca el nivell 1, el nivell 3 en el torso és el més alt, igual que el nivell 2.","metadata":{}},{"cell_type":"code","source":"data['pixel_spacing_x'] = data['pixel_spacing'].apply(lambda x: eval(x)[0] if isinstance(x, str) else x[0])  # Obtenim la coordenada X\ndata['pixel_spacing_y'] = data['pixel_spacing'].apply(lambda x: eval(x)[1] if isinstance(x, str) else x[1])  # Obtenim la coordenada Y\n\n\nplt.figure(figsize=(8, 6))\nplt.scatter(data['pixel_spacing_x'], data['pixel_spacing_y'], alpha=0.5)\nplt.xlabel('Coordenada X')\nplt.ylabel('Coordenada Y')\nplt.title('Distribució de les coordenades de pixel_spacing')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T11:09:06.992713Z","iopub.execute_input":"2025-02-17T11:09:06.993012Z","iopub.status.idle":"2025-02-17T11:09:07.334928Z","shell.execute_reply.started":"2025-02-17T11:09:06.992986Z","shell.execute_reply":"2025-02-17T11:09:07.334059Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Crear un heatmap\nplt.figure(figsize=(8, 6))\nsns.kdeplot(x=data['pixel_spacing_x'], y=data['pixel_spacing_y'], cmap=\"Blues\", fill=True)\nplt.xlabel('Coordenada X')\nplt.ylabel('Coordenada Y')\nplt.title('Heatmap de les coordenades de pixel_spacing')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T11:10:07.33539Z","iopub.execute_input":"2025-02-17T11:10:07.335715Z","iopub.status.idle":"2025-02-17T11:10:13.126369Z","shell.execute_reply.started":"2025-02-17T11:10:07.33569Z","shell.execute_reply":"2025-02-17T11:10:13.125531Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Com podem veure els gràfics no mostren resultats útils. ","metadata":{}},{"cell_type":"markdown","source":"**DATA AUGMENTATION**","metadata":{}},{"cell_type":"code","source":"data2 = pd.read_csv('/kaggle/input/isic-2024-challenge/train-metadata.csv')\nvalors = data2['iddx_3'].tolist()\nx = sns.countplot(data2['iddx_3'], x=valors)\nx.set_xticklabels(x.get_xticklabels(), rotation = 90, ha = \"right\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T11:11:19.24609Z","iopub.execute_input":"2025-02-17T11:11:19.246405Z","iopub.status.idle":"2025-02-17T11:11:27.86104Z","shell.execute_reply.started":"2025-02-17T11:11:19.246381Z","shell.execute_reply":"2025-02-17T11:11:27.860111Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Generar el gràfic de comptes\nx2 = sns.countplot(data=data2, x='iddx_full')\n\n# Rotar les etiquetes de l'eix X\nx2.set_xticklabels(x2.get_xticklabels(), rotation=90, ha=\"right\")\n\n# Mostrar el gràfic\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T11:16:26.488299Z","iopub.execute_input":"2025-02-17T11:16:26.488718Z","iopub.status.idle":"2025-02-17T11:16:27.47304Z","shell.execute_reply.started":"2025-02-17T11:16:26.48868Z","shell.execute_reply":"2025-02-17T11:16:27.472141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Filtrar les dades per incloure només els valors que contenen \"malignant\"\ndata_malignant = data2[data2['iddx_full'].str.contains('malignant', case=False, na=False)]\n\n# Generar el gràfic de comptes només per als valors de \"malignant\"\nx2 = sns.countplot(data=data_malignant, x='iddx_full')\n\n# Rotar les etiquetes de l'eix X\nx2.set_xticklabels(x2.get_xticklabels(), rotation=90, ha=\"right\")\n\n# Mostrar el gràfic\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T11:19:32.913333Z","iopub.execute_input":"2025-02-17T11:19:32.913671Z","iopub.status.idle":"2025-02-17T11:19:33.440433Z","shell.execute_reply.started":"2025-02-17T11:19:32.913645Z","shell.execute_reply":"2025-02-17T11:19:33.439507Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(valors)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-13T17:04:20.476488Z","iopub.execute_input":"2025-02-13T17:04:20.476786Z","iopub.status.idle":"2025-02-13T17:04:20.724835Z","shell.execute_reply.started":"2025-02-13T17:04:20.476762Z","shell.execute_reply":"2025-02-13T17:04:20.723921Z"}},"outputs":[],"execution_count":null}]}