{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport os\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\n# import os\n# for 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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Objective - \n\nIdentify melanoma in images of skin lesions. In particular,images within the same patient are used. Hence, determine which are likely to represent a melanoma. <br>\n- We need to predict the probability of Melanoma given an image and a few other fields.<br>\n- The evaluation metric is ROC AUC","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"## All files\npath = '../input/siim-isic-melanoma-classification'\nprint(os.listdir(path))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df=pd.read_csv(path+'/train.csv')\ntest_df=pd.read_csv(path+'/test.csv')\nprint(\"Train -\",train_df.shape)\nprint(\"Test - \",test_df.shape)\nprint('Train Features',train_df.columns)\nprint('Test Features',test_df.columns)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Missing value count & percentage","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"missing_value_df_train = pd.DataFrame(index = train_df.keys(), data =train_df.isnull().sum(), \n                                      columns = ['Missing_Value_Count'])\nmissing_value_df_train['Missing_Value_Percentage'] = ((train_df.isnull().mean())*100)\nmissing_value_df_train.sort_values('Missing_Value_Count',ascending= False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Let's replace the missing values with text 'missing'.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# for cols in columns:\n#     train_df[cols].fillna('na',inplace=True)\n\n# Replace age na values with median\nage = train_df[train_df[\"age_approx\"]!=np.nan][\"age_approx\"]\ntrain_df[\"age_approx\"].replace(np.nan, age.median(), inplace=True)\n\n# Replace sex & anatom_site na values with mode\ncolumns=['anatom_site_general_challenge','sex']\nfor cols in columns:\n    na = train_df[train_df[cols]!=np.nan][cols]\n    train_df[cols].replace(np.nan, na.mode().values[0], inplace=True)\n\ntrain_df.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Exploring the features\n\n* The benign_malignant feature determines whether the tumor is benign or malignant (benign is harmless, malignant is harmful)\n* Anatom_site_general_challenge tells where the cancer is present.\n","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.groupby('benign_malignant')['sex'].value_counts().plot(kind='bar')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.groupby(['sex','target'])['benign_malignant'].count().to_frame().reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"The above table shows unbalanced classes. <br>\nTotal malignant cases - 584 <br>\nNon Malignant cases - 32477 ","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"#### Gender Distribution","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(data=train_df,x='sex')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Diagnosis Distribution","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.diagnosis.value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Analysing features corresponding to target value 1","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Count of patient ids ( including duplicates) - ',train_df.patient_id.count())\nprint('Count of unique patient ids in train set - ',train_df.patient_id.nunique())\ntrain_patient_unique=train_df[train_df.target==1]\nprint('Number of Patients diagnosed with melanoma - ',len(train_patient_unique))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Plot representing the location of cancer in person's body diagnosed with melanoma","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(8,8))\nsns.countplot(data=train_patient_unique,x='anatom_site_general_challenge',hue='sex')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"uniq_ids=train_patient_unique[train_patient_unique.duplicated(['patient_id'])]\nlen(np.array(uniq_ids))\ntrain_patient_unique[train_patient_unique.anatom_site_general_challenge == 'oral/genital']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#np.array(uniq_ids.patient_id)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"uniq_ids[uniq_ids.patient_id == 'IP_9086201']\n#uniq_ids[uniq_ids.patient_id == 'IP_5399626']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"Seems like there are cases where - same patient has been diagnosed with malignant in different sites,above mentioned patient has cancer in torso as well oral/genital.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"### Plot to analyse the age group of patients being diagnosed with Malignant","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"#train_df.age_approx.value_counts().plot(kind='bar')\nplt.figure(figsize=(8,8))\nsns.countplot(data=train_patient_unique,x='age_approx',hue='sex')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"There are people with as low as 15 years old (male) being malignant and as high as 90 years old (male,female) being affected.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"markdown","source":"## Visualizing few images of benign and malignent","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"benign = train_df.image_name[train_df['benign_malignant']=='benign']\nmalignant = train_df.image_name[train_df['benign_malignant']=='malignant']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def viz(images):\n    # Plot first 10 images\n    image_list=[i+'.jpg' for i in images]\n    image_list= image_list[:10]\n    img_dir = path+'/jpeg/train'\n    plt.figure(figsize=(8,8))\n    # Iterate and plot random images\n    for i in range(9):\n        plt.subplot(3,3,i+1)\n        img = plt.imread(os.path.join(img_dir, image_list[i]))\n        plt.imshow(img)\n        plt.axis('off')\n    plt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"viz(benign)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"viz(malignant)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}