{"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)\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 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":"Loading the data and checking shape,as shown below there are 33K+ rows and 8 columns.","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"data=pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/train.csv')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Checking the null values,as shown below sex,age_approx,anatom_site_general_challenge have null values.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"data.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Using info(),to see the type of features.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"data.info()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Filling null values","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"data['sex'].fillna(data['sex'].mode()[0],inplace=True)\ndata['sex']=data['sex'].map({'male': 1, 'female': 0})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data['age_approx'].fillna(data['age_approx'].median(),inplace=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data['anatom_site_general_challenge'].fillna(data['anatom_site_general_challenge'].mode()[0],inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.isnull().sum().sum()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As shown above,there are no missing values in th data.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Target feature","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Firstly checking the count of benign and malignant rows,and visualising them using countplot and pie chart.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"data['target'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nsns.countplot(data['target'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As shown above the data is completely imbalanced.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.pie(data['target'].value_counts(),labels=[0,1],autopct='%1.2f%%')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Sex feature","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Here 1 means Male and 0 means Female. ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(data['sex'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As shown above, there are more males than females.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.pie(data['sex'].value_counts(),labels=['male','female'],autopct='%1.2f%%')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Male percentage is 51.76 and Female ones is 48.24%.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Now visualising sex with target feature.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(data['sex'],hue='target',data=data)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As shown malignant count is more in males than female.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Age_approx feature","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"First finding the average age by sex,as shown below avg age in male is 50 and those of females is 47,by this we can conclude that females are effecting by this disease in their earlier age than males.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"data.groupby(['sex'])['age_approx'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.kdeplot(data.loc[data['sex'] ==1, 'age_approx'], label = 'Male',shade=True)\nsns.kdeplot(data.loc[data['sex'] ==0, 'age_approx'], label = 'Female',shade=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now finding avg age by target,as shown below people whos age is 58+(elders) are malignant.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"data.groupby(['target'])['age_approx'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.kdeplot(data.loc[data['target'] ==1, 'age_approx'], label = 'Malignant',shade=True)\nsns.kdeplot(data.loc[data['target'] ==0, 'age_approx'], label = 'Bening',shade=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now visualising age_approx feature using histogram,there are more people whose age are ranging between 43 and 62.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"data['age_approx'].hist(color='g')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now visualising the distribution of target with respect to age using violin plot.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.violinplot(x='target',y='age_approx',data=data)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now using barchart,we are plotting sex with age by target,as shown below in both male and female,if the age is above 50 we can consider it as malignant.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.barplot(x='sex',y='age_approx',hue='target',data=data)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# anatom_site_general_challenge feature","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"This feature indicates location of imaged site i.e location of symptoms on the body.As shown below most affected body part is torso(the trunk of the human body).","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"data['anatom_site_general_challenge'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data['anatom_site_general_challenge'].value_counts().plot(kind='bar')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now visualising the effected part by sex.In male torso part is most effected and in remaining almost every part is heavyly effected by female.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x='anatom_site_general_challenge',hue='sex',data=data)\nplt.xticks(rotation='vertical')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Diagnosis feature","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"This feature indicates detailed diagnosis information.As shown below,in most of the cases it is unknown.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"data['diagnosis'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Image data","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"First randomly visualising 9 images.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"IMAGE_PATH = \"../input/siim-isic-melanoma-classification/\"\nfrom PIL import Image\nimages = data['image_name'].values\nrandom_images = [np.random.choice(images+'.jpg') for i in range(9)]\nimg_dir = IMAGE_PATH+'/jpeg/train'\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    img = Image.open(os.path.join(img_dir, random_images[i]))\n    plt.imshow(img, cmap='gray')\n    plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we see random image by bening and malignant.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Benign images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"benign =data[data['benign_malignant']=='benign']\nmalignant =data[data['benign_malignant']=='malignant']\nimages = benign['image_name'].values\nrandom_images = [np.random.choice(images+'.jpg') for i in range(9)]\nimg_dir = IMAGE_PATH+'/jpeg/train'\nplt.figure(figsize=(10,8))\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    img = plt.imread(os.path.join(img_dir, random_images[i]))\n    plt.imshow(img, cmap='gray')\n    plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Malignant images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"images = malignant['image_name'].values\nrandom_images = [np.random.choice(images+'.jpg') for i in range(6)]\nimg_dir = IMAGE_PATH+'/jpeg/train'\nplt.figure(figsize=(10,8))\nfor i in range(6):\n    plt.subplot(2, 3, i + 1)\n    img = plt.imread(os.path.join(img_dir, random_images[i]))\n    plt.imshow(img, cmap='gray')\n    plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we will visualise images by sex.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Male images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"Male =data[data['sex']==1]\nFemale =data[data['sex']==0]\nimages = Male['image_name'].values\nrandom_images = [np.random.choice(images+'.jpg') for i in range(6)]\nimg_dir = IMAGE_PATH+'/jpeg/train'\nplt.figure(figsize=(10,8))\nfor i in range(6):\n    plt.subplot(2, 3, i + 1)\n    img = plt.imread(os.path.join(img_dir, random_images[i]))\n    plt.imshow(img, cmap='gray')\n    plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Female images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"images = Female['image_name'].values\nrandom_images = [np.random.choice(images+'.jpg') for i in range(6)]\nimg_dir = IMAGE_PATH+'/jpeg/train'\nplt.figure(figsize=(10,8))\nfor i in range(6):\n    plt.subplot(2, 3, i + 1)\n    img = plt.imread(os.path.join(img_dir, random_images[i]))\n    plt.imshow(img, cmap='gray')\n    plt.axis('off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Please upvote if you like,any suggestions and mistakes put it in comments,Thank you.","execution_count":null}],"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}