{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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","execution":{"iopub.status.busy":"2022-01-16T13:54:27.529000Z","iopub.execute_input":"2022-01-16T13:54:27.529960Z","iopub.status.idle":"2022-01-16T13:54:50.737448Z","shell.execute_reply.started":"2022-01-16T13:54:27.529915Z","shell.execute_reply":"2022-01-16T13:54:50.736739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Agregamos la libreria de pyAgrum.","metadata":{}},{"cell_type":"code","source":"!pip install pyAgrum pydotplus\n","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:50.738887Z","iopub.execute_input":"2022-01-16T13:54:50.739221Z","iopub.status.idle":"2022-01-16T13:54:57.997883Z","shell.execute_reply.started":"2022-01-16T13:54:50.739179Z","shell.execute_reply":"2022-01-16T13:54:57.997092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Añadimos librerias varias ","metadata":{}},{"cell_type":"code","source":"import pandas\nimport os\nimport math\nimport pyAgrum as gum\nimport pyAgrum.lib.notebook as gnb\nfrom pyAgrum.lib.bn2roc import showROC_PR\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\n\nfrom sklearn.metrics import accuracy_score, roc_auc_score, confusion_matrix\nimport pandas as pd\nimport numpy as np \n\nfrom os import listdir\nfrom os.path import isfile, join\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Model, load_model\n\nimport warnings  \nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:58.001329Z","iopub.execute_input":"2022-01-16T13:54:58.002019Z","iopub.status.idle":"2022-01-16T13:54:58.009132Z","shell.execute_reply.started":"2022-01-16T13:54:58.001978Z","shell.execute_reply":"2022-01-16T13:54:58.008344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train y Test","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv('../input/siim-isic-melanoma-classification/train.csv')\ntest=pd.read_csv('../input/siim-isic-melanoma-classification/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:58.011584Z","iopub.execute_input":"2022-01-16T13:54:58.011863Z","iopub.status.idle":"2022-01-16T13:54:58.081776Z","shell.execute_reply.started":"2022-01-16T13:54:58.011826Z","shell.execute_reply":"2022-01-16T13:54:58.081108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Informacion del Dataset","metadata":{}},{"cell_type":"markdown","source":"# ATRIBUTOS DEL DATASET\n\n* image_name - unique identifier, points to filename of related DICOM image\n* patient_id - unique patient identifier\n* sex - the sex of the patient (when unknown, will be blank)\n* age_approx - approximate patient age at time of imaging\n* anatom_site_general_challenge - location of imaged site\n* diagnosis - detailed diagnosis information (train only)\n* benign_malignant - indicator of malignancy of imaged lesion\n* target - binarized version of the target variable","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:58.082900Z","iopub.execute_input":"2022-01-16T13:54:58.083173Z","iopub.status.idle":"2022-01-16T13:54:58.102357Z","shell.execute_reply.started":"2022-01-16T13:54:58.083138Z","shell.execute_reply":"2022-01-16T13:54:58.101416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:58.103530Z","iopub.execute_input":"2022-01-16T13:54:58.104232Z","iopub.status.idle":"2022-01-16T13:54:58.136841Z","shell.execute_reply.started":"2022-01-16T13:54:58.104186Z","shell.execute_reply":"2022-01-16T13:54:58.136124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Seguimos realizando un preratamiento, ahora filtramos las variables con diversa cantidad de valores, al ser las redes bayesianas redes discretas filtramos cualquier variable con mas de 15resultados diferentes.","metadata":{}},{"cell_type":"code","source":"for k in train.keys():\n    print('{0}: {1}'.format(k, len(train[k].unique())))\n   ","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:58.138018Z","iopub.execute_input":"2022-01-16T13:54:58.138450Z","iopub.status.idle":"2022-01-16T13:54:58.163405Z","shell.execute_reply.started":"2022-01-16T13:54:58.138403Z","shell.execute_reply":"2022-01-16T13:54:58.162747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for k in train.keys():\n    if len(train[k].unique())<=15:\n        print(k)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:58.164746Z","iopub.execute_input":"2022-01-16T13:54:58.165016Z","iopub.status.idle":"2022-01-16T13:54:58.188203Z","shell.execute_reply.started":"2022-01-16T13:54:58.164969Z","shell.execute_reply":"2022-01-16T13:54:58.187437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Categorizamos la edad para poder tratarla.","metadata":{}},{"cell_type":"code","source":"def for_age(row):\n    try:\n        age = float(row['age_approx'])\n        if age < 18:\n            return 'teen'\n        elif age < 30:\n            return 'twenties'\n        elif age < 40:\n            return 'thirties'\n        elif age < 50:\n            return 'forties'\n        elif age < 60:\n            return 'fifties'\n        elif age < 70:\n            return 'sixties'\n        else:\n            return 'old'\n    except ValueError:\n        return np.nan\n    \ndef for_boolean(row, col):\n    try:\n        val = int(row[col])\n        if row[col] >= 1:\n            return \"True\"\n        else:\n            return \"False\"\n    except ValueError:\n        return \"False\"","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:58.189579Z","iopub.execute_input":"2022-01-16T13:54:58.189829Z","iopub.status.idle":"2022-01-16T13:54:58.196827Z","shell.execute_reply.started":"2022-01-16T13:54:58.189795Z","shell.execute_reply":"2022-01-16T13:54:58.196093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pretreat(df):\n    if 'target' in df.columns:\n        df['target'] = df.apply(lambda row: for_boolean(row, 'target'), axis=1)\n    df['age_approx'] = df.apply(for_age, axis=1)\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:58.200684Z","iopub.execute_input":"2022-01-16T13:54:58.201157Z","iopub.status.idle":"2022-01-16T13:54:58.206791Z","shell.execute_reply.started":"2022-01-16T13:54:58.201119Z","shell.execute_reply":"2022-01-16T13:54:58.206088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pretreat(train)\ntest_df=pretreat(test)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:58.208200Z","iopub.execute_input":"2022-01-16T13:54:58.208562Z","iopub.status.idle":"2022-01-16T13:54:59.282294Z","shell.execute_reply.started":"2022-01-16T13:54:58.208526Z","shell.execute_reply":"2022-01-16T13:54:59.281586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.283679Z","iopub.execute_input":"2022-01-16T13:54:59.283920Z","iopub.status.idle":"2022-01-16T13:54:59.299922Z","shell.execute_reply.started":"2022-01-16T13:54:59.283887Z","shell.execute_reply":"2022-01-16T13:54:59.299007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.301425Z","iopub.execute_input":"2022-01-16T13:54:59.301707Z","iopub.status.idle":"2022-01-16T13:54:59.313438Z","shell.execute_reply.started":"2022-01-16T13:54:59.301671Z","shell.execute_reply":"2022-01-16T13:54:59.312649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Bayes Theorem\nP(A|B)=P(B|A)P(A)P(B)(1)\nThe above formula can be reinterpreted in the machine learning terms of features and class label. Classification is a problem of assigning classes based on the features provided.\n\nP(class|features)=P(features|class)P(class)P(features)(2)\nFor example we need to classify a person's sex based on the height and weight. So here the class={male,female} and features={height,weight}, and the formula can he re-written as :\n\nP(sex|height,weight)=P(height,weight|sex)P(sex)P(height,weight)(3)\nBased on these information lets go ahead and implement the algorithm on a discretized problem using the vectorization properties of the problem. To understand what is vectorization of problem please read From Python to Numpy.\nREFERENCIA: http://deebuls.github.io/Naive-Bayes-Pandas.html","metadata":{}},{"cell_type":"markdown","source":"Calculamos primero los apriori \n\nCalculating the prior\nprior = P(maligno/benigno)","metadata":{}},{"cell_type":"code","source":"prior = train.groupby('benign_malignant').size().div(len(train)) #count()['Age']/len(data)\nprior","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.314951Z","iopub.execute_input":"2022-01-16T13:54:59.315238Z","iopub.status.idle":"2022-01-16T13:54:59.328415Z","shell.execute_reply.started":"2022-01-16T13:54:59.315181Z","shell.execute_reply":"2022-01-16T13:54:59.327359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Ahora calculamos la probabilidad(likelihood).\n\nLikelihood is generated for each of the features of the dataset. Basicallay likelihood is probability of finding each feature given class label.\n\nP(age_approx|benign_malignant)(6)\nP(sex|benign_malignant)(7)\nP(anatom_site_general_challenge|benign_malignant)(8)\nP(diagnosis|benign_malignant)(9)","metadata":{}},{"cell_type":"code","source":"likelihood = {}\nlikelihood['age_approx'] = train.groupby(['benign_malignant', 'age_approx']).size().div(len(train)).div(prior)\nlikelihood['sex'] = train.groupby(['benign_malignant', 'sex']).size().div(len(train)).div(prior)\nlikelihood['anatom_site_general_challenge'] = train.groupby(['benign_malignant', 'anatom_site_general_challenge']).size().div(len(train)).div(prior)\n\nlikelihood","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.330116Z","iopub.execute_input":"2022-01-16T13:54:59.330394Z","iopub.status.idle":"2022-01-16T13:54:59.370043Z","shell.execute_reply.started":"2022-01-16T13:54:59.330356Z","shell.execute_reply":"2022-01-16T13:54:59.369137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Ahora calculamos el posteriori.Necesitamos saber si una persona tendra cancer benigno o maligno en base a la nueva informacion\n\n","metadata":{}},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER MALIGNO EN EL TORSO SEA HOMBRE \n#p_maligno = likelihood['age_approx']['malignant']['twenties'] * likelihood['sex']['malignant']['male'] * \\likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\ * prior['malignant']\np_maligno_m_torso = likelihood['age_approx']['malignant'] * likelihood['sex']['malignant']['male'] * \\\n        likelihood['anatom_site_general_challenge']['malignant']['torso'] \\\n        * prior['malignant']\n\nprint('Posteriori 1 : Torso maligno hombre',p_maligno_m_torso)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.371429Z","iopub.execute_input":"2022-01-16T13:54:59.371753Z","iopub.status.idle":"2022-01-16T13:54:59.383640Z","shell.execute_reply.started":"2022-01-16T13:54:59.371714Z","shell.execute_reply":"2022-01-16T13:54:59.382835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER BENIGNO EN EL TORSO SEA HOMBRE \np_benigno_m_torso = likelihood['age_approx']['benign'] * likelihood['sex']['benign']['male'] * \\\n        likelihood['anatom_site_general_challenge']['benign']['torso'] \\\n        * prior['benign']\n\nprint('Posteriori 2: Torso benigno hombre',p_benigno_m_torso)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.384985Z","iopub.execute_input":"2022-01-16T13:54:59.385361Z","iopub.status.idle":"2022-01-16T13:54:59.393914Z","shell.execute_reply.started":"2022-01-16T13:54:59.385320Z","shell.execute_reply":"2022-01-16T13:54:59.392940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER MALIGNO EN EL TORSO SEA MUJER \np_maligno_f_torso = likelihood['age_approx']['malignant'] * likelihood['sex']['malignant']['female'] * \\\n        likelihood['anatom_site_general_challenge']['malignant']['torso'] \\\n        * prior['malignant']\n\nprint('Posteriori 3 Torso mujer maligno',p_maligno_f_torso)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.395485Z","iopub.execute_input":"2022-01-16T13:54:59.396118Z","iopub.status.idle":"2022-01-16T13:54:59.406797Z","shell.execute_reply.started":"2022-01-16T13:54:59.396074Z","shell.execute_reply":"2022-01-16T13:54:59.405827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER BENIGNO EN EL TORSO SEA MUJER\np_benigno_f_torso = likelihood['age_approx']['benign'] * likelihood['sex']['benign']['female'] * \\\n        likelihood['anatom_site_general_challenge']['benign']['torso'] \\\n        * prior['benign']\n\nprint('Posteriori 4 Torso mujer benigno',p_benigno_f_torso)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.408332Z","iopub.execute_input":"2022-01-16T13:54:59.408671Z","iopub.status.idle":"2022-01-16T13:54:59.418306Z","shell.execute_reply.started":"2022-01-16T13:54:59.408630Z","shell.execute_reply":"2022-01-16T13:54:59.417330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER MALIGNO EN LA CABEZA SEA HOMBRE \n#p_maligno = likelihood['age_approx']['malignant']['twenties'] * likelihood['sex']['malignant']['male'] * \\likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\ * prior['malignant']\np_maligno_m_head = likelihood['age_approx']['malignant'] * likelihood['sex']['malignant']['male'] * \\\n        likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\\n        * prior['malignant']\n\nprint('Posteriori 5: Cabeza maligno hombre',p_maligno_m_head)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.419847Z","iopub.execute_input":"2022-01-16T13:54:59.420168Z","iopub.status.idle":"2022-01-16T13:54:59.432978Z","shell.execute_reply.started":"2022-01-16T13:54:59.420127Z","shell.execute_reply":"2022-01-16T13:54:59.432264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER BENIGNO EN EL TORSO SEA HOMBRE \np_benigno_m_head = likelihood['age_approx']['benign'] * likelihood['sex']['benign']['male'] * \\\n        likelihood['anatom_site_general_challenge']['benign']['head/neck'] \\\n        * prior['benign']\n\nprint('Posteriori 6 cabeza benigno hombre',p_benigno_m_head)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.434597Z","iopub.execute_input":"2022-01-16T13:54:59.434905Z","iopub.status.idle":"2022-01-16T13:54:59.447787Z","shell.execute_reply.started":"2022-01-16T13:54:59.434854Z","shell.execute_reply":"2022-01-16T13:54:59.446733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER MALIGNO EN LA CABEZA SEA MUJER \n#p_maligno = likelihood['age_approx']['malignant']['twenties'] * likelihood['sex']['malignant']['male'] * \\likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\ * prior['malignant']\np_maligno_f_head = likelihood['age_approx']['malignant'] * likelihood['sex']['malignant']['female'] * \\\n        likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\\n        * prior['malignant']\n\nprint('Posteriori 7 cabeza maligno mujer',p_maligno_f_head)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.449431Z","iopub.execute_input":"2022-01-16T13:54:59.449878Z","iopub.status.idle":"2022-01-16T13:54:59.459672Z","shell.execute_reply.started":"2022-01-16T13:54:59.449727Z","shell.execute_reply":"2022-01-16T13:54:59.458715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER BENIGNO EN LA CABEZA SEA MUJER \np_benigno_f_head = likelihood['age_approx']['benign'] * likelihood['sex']['benign']['female'] * \\\n        likelihood['anatom_site_general_challenge']['benign']['head/neck'] \\\n        * prior['benign']\n\nprint('Posteriori 8 cabeza benigno mujer',p_benigno_f_head)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.461737Z","iopub.execute_input":"2022-01-16T13:54:59.462128Z","iopub.status.idle":"2022-01-16T13:54:59.470199Z","shell.execute_reply.started":"2022-01-16T13:54:59.462090Z","shell.execute_reply":"2022-01-16T13:54:59.469367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER MALIGNO EN LAS EXTREMIDADES INFERIORES SEA HOMBRE \n#p_maligno = likelihood['age_approx']['malignant']['twenties'] * likelihood['sex']['malignant']['male'] * \\likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\ * prior['malignant']\np_maligno_m_l_e = likelihood['age_approx']['malignant'] * likelihood['sex']['malignant']['male'] * \\\n        likelihood['anatom_site_general_challenge']['malignant']['lower extremity'] \\\n        * prior['malignant']\n\nprint('Posteriori 9 extremidades inferiores maligno hombre',p_maligno_m_l_e)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.472293Z","iopub.execute_input":"2022-01-16T13:54:59.472642Z","iopub.status.idle":"2022-01-16T13:54:59.482984Z","shell.execute_reply.started":"2022-01-16T13:54:59.472576Z","shell.execute_reply":"2022-01-16T13:54:59.482127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER BENIGNO EN LAS EXTREMIDADES INFERIORES SEA HOMBRE \np_benigno_m_l_e = likelihood['age_approx']['benign'] * likelihood['sex']['benign']['male'] * \\\n        likelihood['anatom_site_general_challenge']['benign']['lower extremity'] \\\n        * prior['benign']\n\nprint('Posteriori 10 extremidades inferiores benigno hombre',p_benigno_m_l_e)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.484276Z","iopub.execute_input":"2022-01-16T13:54:59.484996Z","iopub.status.idle":"2022-01-16T13:54:59.494491Z","shell.execute_reply.started":"2022-01-16T13:54:59.484953Z","shell.execute_reply":"2022-01-16T13:54:59.493544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER MALIGNO EN LAS EXTREMIDADES INFERIORES SEA MUJER \n#p_maligno = likelihood['age_approx']['malignant']['twenties'] * likelihood['sex']['malignant']['male'] * \\likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\ * prior['malignant']\np_maligno_f_l_e = likelihood['age_approx']['malignant'] * likelihood['sex']['malignant']['female'] * \\\n        likelihood['anatom_site_general_challenge']['malignant']['lower extremity'] \\\n        * prior['malignant']\n\nprint('Posteriori 11, maligno mujer extremidades inferiores',p_maligno_f_l_e)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.495725Z","iopub.execute_input":"2022-01-16T13:54:59.496304Z","iopub.status.idle":"2022-01-16T13:54:59.506329Z","shell.execute_reply.started":"2022-01-16T13:54:59.496265Z","shell.execute_reply":"2022-01-16T13:54:59.505315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER BENIGNO EN LAS EXTREMIDADES INFERIORES SEA MUJER \np_benigno_f_l_e = likelihood['age_approx']['benign'] * likelihood['sex']['benign']['female'] * \\\n        likelihood['anatom_site_general_challenge']['benign']['lower extremity'] \\\n        * prior['benign']\n\nprint('Posteriori 12 benigno mujer extremidades inferiores',p_benigno_f_l_e)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.508087Z","iopub.execute_input":"2022-01-16T13:54:59.508378Z","iopub.status.idle":"2022-01-16T13:54:59.518260Z","shell.execute_reply.started":"2022-01-16T13:54:59.508337Z","shell.execute_reply":"2022-01-16T13:54:59.517303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER MALIGNO EN LAS EXTREMIDADES INFERIORES SEA HOMBRE \n#p_maligno = likelihood['age_approx']['malignant']['twenties'] * likelihood['sex']['malignant']['male'] * \\likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\ * prior['malignant']\np_maligno_m_oral = likelihood['age_approx']['malignant'] * likelihood['sex']['malignant']['male'] * \\\n        likelihood['anatom_site_general_challenge']['malignant']['oral/genital'] \\\n        * prior['malignant']\n\nprint('Posteriori 13 maligno hombre en boca',p_maligno_m_oral)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.523357Z","iopub.execute_input":"2022-01-16T13:54:59.524216Z","iopub.status.idle":"2022-01-16T13:54:59.532107Z","shell.execute_reply.started":"2022-01-16T13:54:59.524172Z","shell.execute_reply":"2022-01-16T13:54:59.531164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER BENIGNO EN LA BOCA INFERIORES SEA HOMBRE \np_benigno_m_oral = likelihood['age_approx']['benign'] * likelihood['sex']['benign']['male'] * \\\n        likelihood['anatom_site_general_challenge']['benign']['oral/genital'] \\\n        * prior['benign']\n\nprint('Posteriori 14 benigno hombre en boca',p_benigno_m_oral)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.533550Z","iopub.execute_input":"2022-01-16T13:54:59.535274Z","iopub.status.idle":"2022-01-16T13:54:59.547157Z","shell.execute_reply.started":"2022-01-16T13:54:59.535232Z","shell.execute_reply":"2022-01-16T13:54:59.546026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER MALIGNO EN LAS BOCA  SEA MUJER \n#p_maligno = likelihood['age_approx']['malignant']['twenties'] * likelihood['sex']['malignant']['male'] * \\likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\ * prior['malignant']\np_maligno_f_oral = likelihood['age_approx']['malignant'] * likelihood['sex']['malignant']['female'] * \\\n        likelihood['anatom_site_general_challenge']['malignant']['lower extremity'] \\\n        * prior['malignant']\n\nprint('Posteriori 15, maligno mujer oral',p_maligno_f_oral)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.548854Z","iopub.execute_input":"2022-01-16T13:54:59.549163Z","iopub.status.idle":"2022-01-16T13:54:59.559567Z","shell.execute_reply.started":"2022-01-16T13:54:59.549126Z","shell.execute_reply":"2022-01-16T13:54:59.558750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER BENIGNO EN LA BOCA SEA MUJER \np_benigno_f_oral = likelihood['age_approx']['benign'] * likelihood['sex']['benign']['female'] * \\\n        likelihood['anatom_site_general_challenge']['benign']['oral/genital'] \\\n        * prior['benign']\n\nprint('Posteriori 16 benigno mujer, oral',p_benigno_f_oral)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.561154Z","iopub.execute_input":"2022-01-16T13:54:59.561684Z","iopub.status.idle":"2022-01-16T13:54:59.571993Z","shell.execute_reply.started":"2022-01-16T13:54:59.561644Z","shell.execute_reply":"2022-01-16T13:54:59.571231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER MALIGNO EN LAS EXTREMIDADES INFERIORES SEA HOMBRE \n#p_maligno = likelihood['age_approx']['malignant']['twenties'] * likelihood['sex']['malignant']['male'] * \\likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\ * prior['malignant']\np_maligno_m_u_e = likelihood['age_approx']['malignant'] * likelihood['sex']['malignant']['male'] * \\\n        likelihood['anatom_site_general_challenge']['malignant']['upper extremity'] \\\n        * prior['malignant']\n\nprint('Posteriori 17 maligno hombre extremidades superiores',p_maligno_m_u_e)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.574523Z","iopub.execute_input":"2022-01-16T13:54:59.574718Z","iopub.status.idle":"2022-01-16T13:54:59.585764Z","shell.execute_reply.started":"2022-01-16T13:54:59.574694Z","shell.execute_reply":"2022-01-16T13:54:59.584659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER BENIGNO EN LA BOCA INFERIORES SEA HOMBRE \np_benigno_m_u_e = likelihood['age_approx']['benign'] * likelihood['sex']['benign']['male'] * \\\n        likelihood['anatom_site_general_challenge']['benign']['upper extremity'] \\\n        * prior['benign']\n\nprint('Posteriori 18, benigno hombre, extremidades superiores',p_benigno_m_u_e)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.587110Z","iopub.execute_input":"2022-01-16T13:54:59.587492Z","iopub.status.idle":"2022-01-16T13:54:59.598334Z","shell.execute_reply.started":"2022-01-16T13:54:59.587453Z","shell.execute_reply":"2022-01-16T13:54:59.597183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER MALIGNO EN LAS BOCA  SEA MUJER \n#p_maligno = likelihood['age_approx']['malignant']['twenties'] * likelihood['sex']['malignant']['male'] * \\likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\ * prior['malignant']\np_maligno_f_u_e = likelihood['age_approx']['malignant'] * likelihood['sex']['malignant']['female'] * \\\n        likelihood['anatom_site_general_challenge']['malignant']['upper extremity'] \\\n        * prior['malignant']\n\nprint('Posteriori 19 maligno mujer extremidades superiores',p_maligno_f_u_e)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.600189Z","iopub.execute_input":"2022-01-16T13:54:59.600471Z","iopub.status.idle":"2022-01-16T13:54:59.611092Z","shell.execute_reply.started":"2022-01-16T13:54:59.600436Z","shell.execute_reply":"2022-01-16T13:54:59.610384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER BENIGNO EN LAS EXTREMIDADES SUPERIORES SEA MUJER \np_benigno_f_u_e = likelihood['age_approx']['benign'] * likelihood['sex']['benign']['female'] * \\\n        likelihood['anatom_site_general_challenge']['benign']['upper extremity'] \\\n        * prior['benign']\n\nprint('Posteriori 20 benigno mujer extremidades superiores',p_benigno_f_u_e)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.612719Z","iopub.execute_input":"2022-01-16T13:54:59.613316Z","iopub.status.idle":"2022-01-16T13:54:59.624657Z","shell.execute_reply.started":"2022-01-16T13:54:59.613274Z","shell.execute_reply":"2022-01-16T13:54:59.623910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER MALIGNO EN LAS MANOS SEA HOMBRE \n#p_maligno = likelihood['age_approx']['malignant']['twenties'] * likelihood['sex']['malignant']['male'] * \\likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\ * prior['malignant']\np_maligno_m_manos = likelihood['age_approx']['malignant'] * likelihood['sex']['malignant']['male'] * \\\n        likelihood['anatom_site_general_challenge']['malignant']['palms/soles'] \\\n        * prior['malignant']\n\nprint('Posteriori 21 maligno hombre manos',p_maligno_m_manos)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.625672Z","iopub.execute_input":"2022-01-16T13:54:59.625862Z","iopub.status.idle":"2022-01-16T13:54:59.635019Z","shell.execute_reply.started":"2022-01-16T13:54:59.625839Z","shell.execute_reply":"2022-01-16T13:54:59.634221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER BENIGNO EN LA MANOS INFERIORES SEA HOMBRE \np_benigno_m_manos = likelihood['age_approx']['benign'] * likelihood['sex']['benign']['male'] * \\\n        likelihood['anatom_site_general_challenge']['benign']['palms/soles'] \\\n        * prior['benign']\n\nprint('Posteriori 22 benigno hombre manos' ,p_benigno_m_manos)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.636243Z","iopub.execute_input":"2022-01-16T13:54:59.637150Z","iopub.status.idle":"2022-01-16T13:54:59.647428Z","shell.execute_reply.started":"2022-01-16T13:54:59.637113Z","shell.execute_reply":"2022-01-16T13:54:59.646565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER MALIGNO EN LAS MANOS  SEA MUJER \n#p_maligno = likelihood['age_approx']['malignant']['twenties'] * likelihood['sex']['malignant']['male'] * \\likelihood['anatom_site_general_challenge']['malignant']['head/neck'] \\ * prior['malignant']\np_maligno_f_manos = likelihood['age_approx']['malignant'] * likelihood['sex']['malignant']['female'] * \\\n        likelihood['anatom_site_general_challenge']['malignant']['palms/soles'] \\\n        * prior['malignant']\n\nprint('Posteriori 23 maligno mujer manos',p_maligno_f_manos)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.648725Z","iopub.execute_input":"2022-01-16T13:54:59.649437Z","iopub.status.idle":"2022-01-16T13:54:59.659417Z","shell.execute_reply.started":"2022-01-16T13:54:59.649399Z","shell.execute_reply":"2022-01-16T13:54:59.658423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PROBABILIDAD DE QUE UNA PERSONA TENGA CANCER BENIGNO EN LAS MANOS SEA MUJER \np_benigno_f_manos = likelihood['age_approx']['benign'] * likelihood['sex']['benign']['female'] * \\\n        likelihood['anatom_site_general_challenge']['benign']['palms/soles'] \\\n        * prior['benign']\n\nprint('Posteriori 24 benigno mujer manos',p_benigno_f_manos)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.662358Z","iopub.execute_input":"2022-01-16T13:54:59.662589Z","iopub.status.idle":"2022-01-16T13:54:59.672234Z","shell.execute_reply.started":"2022-01-16T13:54:59.662564Z","shell.execute_reply":"2022-01-16T13:54:59.671410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creamos la red bayesiana","metadata":{}},{"cell_type":"code","source":"import pyAgrum as gum\nimport pyAgrum.lib.notebook as gnb\n%matplotlib inline \n\n\nbn = gum.BayesNet(\"Melanoma\")\nbn =gum.fastBN(\"target{False|True}->age{teen|twenties|thirties|forties|fifties|sixties|old}->location{head/neck|lower extremity|oral/genital|palms/soles|torso|upper extremity}->sex{female|male}\")\nprint(bn.variable(\"target\"))\nprint(bn.variable(\"age\"))\nprint(bn.variable(\"sex\"))\nprint(bn.variable(\"location\"))\n\nbn","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.673696Z","iopub.execute_input":"2022-01-16T13:54:59.674145Z","iopub.status.idle":"2022-01-16T13:54:59.923654Z","shell.execute_reply.started":"2022-01-16T13:54:59.674106Z","shell.execute_reply":"2022-01-16T13:54:59.922862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prevalencia de benigno frente a maligno..","metadata":{}},{"cell_type":"code","source":"train_df.groupby(['target']).size()/len(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.926109Z","iopub.execute_input":"2022-01-16T13:54:59.926593Z","iopub.status.idle":"2022-01-16T13:54:59.939981Z","shell.execute_reply.started":"2022-01-16T13:54:59.926546Z","shell.execute_reply":"2022-01-16T13:54:59.938988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Podemos decir que la posibilad de tener un cancer maligno es de 98:2, con lo que partimos de esa hipotemos.","metadata":{}},{"cell_type":"code","source":"bn.cpt('target')[:] = [0.98237, 0.01763]\nbn.cpt('target').normalizeAsCPT()\nbn.cpt('target')","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.941639Z","iopub.execute_input":"2022-01-16T13:54:59.942485Z","iopub.status.idle":"2022-01-16T13:54:59.950788Z","shell.execute_reply.started":"2022-01-16T13:54:59.942445Z","shell.execute_reply":"2022-01-16T13:54:59.949995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Obtenemos la prevalencia y agrumanos segun edades.","metadata":{}},{"cell_type":"code","source":"train_df.groupby(['age_approx', 'target']).size()/len(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.952330Z","iopub.execute_input":"2022-01-16T13:54:59.952615Z","iopub.status.idle":"2022-01-16T13:54:59.973463Z","shell.execute_reply.started":"2022-01-16T13:54:59.952578Z","shell.execute_reply":"2022-01-16T13:54:59.972767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bn.cpt('age')[{'target':0}] = [0.004498, 0.065719, 0.155739,0.240415,  0.240808, 0.170018,0.105174]\nbn.cpt('age')[{'target':1}] = [0.000060,0.000664, 0.001479, 0.002355, 0.003532,0.004075, 0.005464]\nbn.cpt('age')","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.974879Z","iopub.execute_input":"2022-01-16T13:54:59.975367Z","iopub.status.idle":"2022-01-16T13:54:59.984754Z","shell.execute_reply.started":"2022-01-16T13:54:59.975329Z","shell.execute_reply":"2022-01-16T13:54:59.984007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Obtenemos la prevalencia en la relacion entre el sexo y la localizacion.\n","metadata":{}},{"cell_type":"code","source":"train_df.groupby(['sex', 'anatom_site_general_challenge']).size()/len(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:54:59.986251Z","iopub.execute_input":"2022-01-16T13:54:59.986702Z","iopub.status.idle":"2022-01-16T13:55:00.003736Z","shell.execute_reply.started":"2022-01-16T13:54:59.986667Z","shell.execute_reply":"2022-01-16T13:55:00.002827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# location vs target","metadata":{}},{"cell_type":"code","source":"bn.cpt('sex').fillWith = [[0.025177, 0.030580], [0.134305, 0.119242], [0.001358,  0.002385],\n                          [0.004558,  0.006762],[0.228823, 0.279116], [0.081115,  0.068707],]\nbn.cpt('sex')","metadata":{"execution":{"iopub.status.busy":"2022-01-16T13:55:00.005325Z","iopub.execute_input":"2022-01-16T13:55:00.005823Z","iopub.status.idle":"2022-01-16T13:55:00.013828Z","shell.execute_reply.started":"2022-01-16T13:55:00.005784Z","shell.execute_reply":"2022-01-16T13:55:00.013124Z"},"trusted":true},"execution_count":null,"outputs":[]}]}