{"cells":[{"metadata":{"_uuid":"3ae7b844-7edb-4047-8375-fc21df004e96","_cell_guid":"7e448059-7873-4cc6-8e5b-4d152f3b33ea","trusted":true},"cell_type":"markdown","source":"# Introduction\n\nThis is a very beginner friendly sample submission notebook. This notebook only uses the CSV files provided but not the image dataset(will be used in future submissions). In here, you'll learn basic feature engineering techniques, EDA as well as training and cross validation(or) resampling techniques.<br>\n**This is my first notebook for kaggle, any suggestions or updates are appreciated. Thank you!**","execution_count":null},{"metadata":{"_uuid":"4594e7d7-cda9-4b17-8b60-0aa449443c4d","_cell_guid":"eca2b0d1-4ed5-4cae-9e5c-73a0dd7cf1d0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nimport os","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f85967e4-befa-497c-bfa6-8c5db40923f8","_cell_guid":"ddab85fc-2fb7-4df3-8a54-42d672ccd24e","trusted":true},"cell_type":"code","source":"DATA_DIR = '../input/siim-isic-melanoma-classification/'\n# os.listdir(DATA_DIR)\n\ntrain = pd.read_csv(os.path.join(DATA_DIR, 'train.csv'))\ntrain = train.sample(frac=1).reset_index(drop=True)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"76d0c257-9684-438b-948e-b1d87adec9b7","_cell_guid":"fa5b48f9-87c4-4a56-a11b-909df53a89bd","trusted":true},"cell_type":"code","source":"test = pd.read_csv(os.path.join(DATA_DIR, 'test.csv'))\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"80edd5ca-f080-4a68-acd0-f39c032b95eb","_cell_guid":"0ce83596-545a-4595-a04c-602803daf479","trusted":true},"cell_type":"code","source":"# Checking null values in each column\ntest.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"988a183d-7530-4df0-924b-aab5c85ed0b7","_cell_guid":"b21ae398-de0e-4433-8541-3d7de7c817e6","trusted":true},"cell_type":"code","source":"train.columns","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8dd5459d-4658-4e07-b008-4426dd15871c","_cell_guid":"f7d08732-9004-41a4-9d1d-0ecf442eef0d","trusted":true},"cell_type":"code","source":"train.diagnosis.unique()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"74433b48-ac0c-4855-80d4-d33cc5b0b4d4","_cell_guid":"b71a4eb2-73ab-4320-be6b-8304e6e8eb53","trusted":true},"cell_type":"code","source":"# Checking the histogram of Diagnosis values\n\nplt.figure(figsize=(30,7))\nplt.xlabel('Diagnosis')\nplt.ylabel('Count')\nplt.hist(train.diagnosis)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"77c8584e-ee76-4c5b-8546-9945a1a90456","_cell_guid":"06532e3a-8764-4af7-af42-b1594d287040","trusted":true},"cell_type":"code","source":"train.diagnosis[train.diagnosis == 'melanoma'].count()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d2a20151-f444-46e7-acfa-fd14f16ef5ac","_cell_guid":"e660b446-5a8a-4e59-8d87-859eb2e3d476","trusted":true},"cell_type":"code","source":"train.anatom_site_general_challenge.unique()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e4c6f3b1-8e74-43da-acec-b6f09abdabe3","_cell_guid":"e82dd4e8-b41c-4bb3-9e99-d6d078edd5a2","trusted":true},"cell_type":"code","source":"# Checking the histogram of imaged sites \n\n\nplt.figure(figsize=(30,7))\nplt.xlabel('Location of imaged site')\nplt.ylabel('Count')\n\nplt.hist(train.anatom_site_general_challenge[train.anatom_site_general_challenge=='torso'])\nplt.hist(train.anatom_site_general_challenge[train.anatom_site_general_challenge=='head/neck'])\nplt.hist(train.anatom_site_general_challenge[train.anatom_site_general_challenge=='upper extremity'])\nplt.hist(train.anatom_site_general_challenge[train.anatom_site_general_challenge=='lower extremity'])\nplt.hist(train.anatom_site_general_challenge[train.anatom_site_general_challenge=='palms/soles'])\nplt.hist(train.anatom_site_general_challenge[train.anatom_site_general_challenge=='oral/genital'])\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ab460845-1cc0-4d6d-9751-0f8c548c72f1","_cell_guid":"c855c4ee-f671-4f10-9d31-b6032517ba62","trusted":true},"cell_type":"markdown","source":"# Null Values Imputation","execution_count":null},{"metadata":{"_uuid":"8be18bc4-3832-4762-9018-ea608d801e21","_cell_guid":"7c0d0db6-0ec9-48d3-92b4-75085c27359c","trusted":true},"cell_type":"code","source":"train.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"393aeb39-31cd-4809-92ee-b083416bf0ac","_cell_guid":"52271185-e4ed-4811-9e63-9bd750839b58","trusted":true},"cell_type":"code","source":"train.sex.fillna(method='ffill',inplace=True)\ntrain.age_approx.fillna(method='ffill',inplace=True)\ntrain.anatom_site_general_challenge.fillna(method='bfill',inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ff604caf-d29d-483f-a039-8bd7de96e0bf","_cell_guid":"3b9c7240-d72b-473c-8e0e-1f275ed70f2d","trusted":true},"cell_type":"code","source":"train.sex.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"981ed050-9bca-4a57-aaa7-6dd0720fb509","_cell_guid":"3710feab-8220-4f89-8500-85cc85164a29","trusted":true},"cell_type":"markdown","source":"# Preparing xtrain and ytrain","execution_count":null},{"metadata":{"_uuid":"43ca5c19-d675-43bb-b0fd-bda777cb529e","_cell_guid":"abc3b2ec-6c4a-41e4-808b-7e7803aedd4d","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nfeatures = train.drop('target',axis=1)\ntarget = train['target']\n\nxtrain,xtest,ytrain,ytest = train_test_split(features,target,test_size=0.3)\n\n\nprint('Shape of xtrain : ',xtrain.shape)\nprint('Shape of ytrain : ',ytrain.shape)\n\nprint('Shape of xtest : ',xtest.shape)\nprint('Shape of ytest : ',ytest.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7be669ad-d620-45f0-b7e6-355209146b11","_cell_guid":"2383ab0e-5641-4edf-af84-d68b3a081220","trusted":true},"cell_type":"markdown","source":"# Dropping Unnecessary columns","execution_count":null},{"metadata":{"_uuid":"4e1d76f2-6b02-4463-8c64-d230b63051b3","_cell_guid":"405da63d-25a7-4556-a30c-59b063632b36","trusted":true},"cell_type":"code","source":"xtrain = xtrain.drop(['image_name','patient_id'],axis=1)\nxtest = xtest.drop(['image_name','patient_id'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6d17f8db-fe1b-4b65-af41-9a46aa06df90","_cell_guid":"b8459db1-b6b5-4809-863d-0d11a16ada86","trusted":true},"cell_type":"code","source":"xtest","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b0a51265-f736-43ae-88d8-616d6a89420b","_cell_guid":"0dd434f1-62e3-4c40-9855-00b4cca70cc1","trusted":true},"cell_type":"markdown","source":"# Encoding Textual values","execution_count":null},{"metadata":{"_uuid":"9825fc1b-44fc-445b-8e74-5f8c5fdd4bf3","_cell_guid":"0a58200b-f122-4497-8930-9c6c70b622bc","trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\nle = LabelEncoder()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"07f0e500-59d8-4562-9985-9696cac62d94","_cell_guid":"00597a98-1410-47b2-aad9-ae647b4791f8","trusted":true},"cell_type":"code","source":"xtrain['sex_n']=le.fit_transform(xtrain.sex)\nxtrain['diagnosis_n']=le.fit_transform(xtrain.diagnosis)\nxtrain['benign_malignant_n']=le.fit_transform(xtrain.benign_malignant)\nxtrain['anatom_site_general_challenge_n']=le.fit_transform(xtrain.anatom_site_general_challenge)\n\nxtest['sex_n']=le.fit_transform(xtest.sex)\nxtest['diagnosis_n']=le.fit_transform(xtest.diagnosis)\nxtest['benign_malignant_n']=le.fit_transform(xtest.benign_malignant)\nxtest['anatom_site_general_challenge_n']=le.fit_transform(xtest.anatom_site_general_challenge)\n\nxtrain = xtrain.drop(['sex','diagnosis','benign_malignant','anatom_site_general_challenge'],axis=1)\nxtest = xtest.drop(['sex','diagnosis','benign_malignant','anatom_site_general_challenge'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7c9c69a0-0f0e-4933-aa65-2fa1ab3b0ff0","_cell_guid":"bc853654-c76a-4247-9d64-13e01205ab62","trusted":true},"cell_type":"markdown","source":"# Training","execution_count":null},{"metadata":{"_uuid":"c29888b1-2443-4a93-9b97-5172cf33392d","_cell_guid":"2be1587d-d1b0-4fb0-96a5-9119285dc130","trusted":true},"cell_type":"code","source":"from sklearn.svm import SVC\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.linear_model import LogisticRegression","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"026492ad-f770-4e12-9b3d-fd6b4abc5535","_cell_guid":"709b06e0-8dc1-4c33-b52b-36baf9361e86","trusted":true},"cell_type":"code","source":"mod = SVC(probability=True).fit(xtrain,ytrain)\nmod.score(xtest,ytest)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8396076b-ee56-48bf-89d1-55a2e22ddda4","_cell_guid":"2010b267-4f02-4c7a-a715-f43a3300044a","trusted":true},"cell_type":"markdown","source":"### All the 3 model's score is 1.0 except for SVC it's 0.98\n\nHence this method isn't suitable for classification, let's try Cross_val_score","execution_count":null},{"metadata":{"_uuid":"eceaf832-5304-4d4d-a115-beb755c7f0e7","_cell_guid":"8e88c6e0-de25-4b9a-b4a8-2ebffbe2048b","trusted":true},"cell_type":"code","source":"preds = mod.predict(xtest)\nprint('Number of misclassifications : %d'%(preds!=ytest).sum())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"13774705-cc86-46e3-9a2d-267d24dbdf1a","_cell_guid":"9a5fcb57-38a9-4cee-962e-3c44ef5cf362","trusted":true},"cell_type":"markdown","source":"## Cross_Val_Score","execution_count":null},{"metadata":{"_uuid":"622dd2e2-e4b1-40b3-823d-fdce5ddffc33","_cell_guid":"c6056bf8-06ff-42ed-8067-cdb9b358b4e6","trusted":true},"cell_type":"code","source":"features = features.drop(['image_name','patient_id'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"00cdb53e-1ed4-4bb2-afb3-5a1612d52373","_cell_guid":"4c8f4491-2d69-4c59-bba6-5c8689cb36af","trusted":true},"cell_type":"code","source":"features['sex_n']=le.fit_transform(features.sex)\nfeatures['diagnosis_n']=le.fit_transform(features.diagnosis)\nfeatures['benign_malignant_n']=le.fit_transform(features.benign_malignant)\nfeatures['anatom_site_general_challenge_n']=le.fit_transform(features.anatom_site_general_challenge)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c859e9ab-4c3b-481f-947a-2e11e0ee3012","_cell_guid":"44e038f6-e2d9-48ed-b413-5dc4146ede65","trusted":true},"cell_type":"code","source":"features=features.drop(['sex','anatom_site_general_challenge','diagnosis','benign_malignant'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cf2f84f7-3b5f-4157-9812-589fb66e288f","_cell_guid":"f3c572dc-b7ef-4132-8a19-cb9dbf365e52","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import cross_val_score\n\nprint(\"LogisticRegression Avg. Score : \",np.average(cross_val_score(LogisticRegression(),features,target,cv=3)))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ba5f70d4-f251-48f0-99e2-ee92894defbc","_cell_guid":"2a1c3c21-aa28-4b8b-abcf-4788cde2a2f2","trusted":true},"cell_type":"code","source":"print(\"SVM Avg. Score : \",np.average(cross_val_score(SVC(),features,target,cv=3)))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9a9208c4-6f90-4d4d-a387-51a45ae4e124","_cell_guid":"46d70ff7-f6b2-42bd-87c8-78d7b18c271f","trusted":true},"cell_type":"code","source":"print(\"DecisionTree Avg. Score : \",np.average(cross_val_score(DecisionTreeClassifier(),features,target,cv=3)))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f70810ee-4f02-4874-ac83-ec86363588f6","_cell_guid":"77a904f1-644f-4956-9879-442f93e37bfe","trusted":true},"cell_type":"code","source":"print(\"NaiveBayes Avg. Score : \",np.average(cross_val_score(GaussianNB(),features,target,cv=3)))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b97c8d43-e348-46ae-ba64-5ed1855c522c","_cell_guid":"0a2e09bb-7f61-4869-8100-2505371cd75e","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import cross_val_predict\n\npreds = cross_val_predict(SVC(),features,target)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2fa38ab0-be1c-42bf-83e7-f0da1c2048ad","_cell_guid":"cdb0f7c6-7b83-45eb-83d9-22f32889aac2","trusted":true},"cell_type":"code","source":"print(\"Number of misclassifications : %d\"%(preds!=target).sum())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"43bf1a04-4131-49b5-ba6d-d8a2a786de27","_cell_guid":"b4ef86b8-c3d8-4d76-8cef-fa74b65952d9","trusted":true},"cell_type":"markdown","source":"# Preparing Test Predictions\n\nIn this section, we'll be manually doing the cross_val method using KFold that's represented above and extract the predictions.","execution_count":null},{"metadata":{"_uuid":"c11a50f4-586a-436b-8bfe-ac8b57dd7050","_cell_guid":"60da51c6-58bf-4934-bcec-11678b23e4f0","trusted":true},"cell_type":"code","source":"test.anatom_site_general_challenge.fillna(method='bfill',inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cd17eb98-bc21-47c8-9a31-fb6968c875a1","_cell_guid":"bc7c7ba6-a2a8-4e9d-8762-19f3b0785f7a","trusted":true},"cell_type":"code","source":"test_features = test.drop(['image_name','patient_id'],axis=1)\n\ntest_features['sex_n'] = le.fit_transform(test_features.sex)\ntest_features['anatom_site_general_challenge_n'] = le.fit_transform(test_features.anatom_site_general_challenge)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e7f7928c-1c46-4047-a524-fd5ce42195d5","_cell_guid":"f4033f91-6a8b-4015-9e2c-64b4087c059d","trusted":true},"cell_type":"code","source":"test_features = test_features.drop(['sex','anatom_site_general_challenge'],axis=1)\ntest_features","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c2e68e58-5147-4193-9320-d7d93f6ff2d3","_cell_guid":"4dda7ff4-39f7-4505-91df-2f7503cb7097","trusted":true},"cell_type":"code","source":"# Making a zeros column for 2 features to make predictions\n\ntest_features['diagnosis_n'] = [i*0 for i in range(0,10982)]\ntest_features['benign_malignant_n'] = [i*0 for i in range(0,10982)]\ntest_features","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e4e3d18b-6d9b-4491-94ee-9dc5a2d9ae62","_cell_guid":"21d39408-9e26-4624-a8fc-0bc5120337fe","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import KFold\nkf = KFold(n_splits=5)\nkf","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5bad2d05-fe24-44ea-94ad-860128eafad3","_cell_guid":"e41f7845-d599-4a3f-aa5a-9bf54b7cfeef","trusted":true},"cell_type":"code","source":"n_splits = 5\nkf = KFold(n_splits=n_splits, random_state=137, shuffle=True)\n\nfor jj, (train_index, val_index) in enumerate(kf.split(xtrain)):\n    print(\"Fitting fold\", jj+1)\n    train_features = xtrain.iloc[train_index]\n    train_target = ytrain.iloc[train_index]\n    \n    val_features = xtrain.iloc[val_index]\n    val_target = ytrain.iloc[val_index]\n    \n    model = SVC(probability=True)\n    model.fit(train_features, train_target)\n    \n    print(\"Scores : \",model.score(val_features,val_target))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d40b2f37-247f-4806-8ff7-29c6dc524834","_cell_guid":"2d70ebd3-39e3-45e2-8310-a64e813cf483","trusted":true},"cell_type":"markdown","source":"In above cell, I've tested all 4 models and found out that SVM model has the ability to generalize well on unseen data.","execution_count":null},{"metadata":{"_uuid":"c7c67dba-91e3-43f8-9391-85f1d282087f","_cell_guid":"d66e92cb-178f-4eca-abc4-a6f61fdc65af","trusted":true},"cell_type":"code","source":"submission = pd.DataFrame(test.image_name)\nsubmission['target'] = np.round(model.predict_proba(test_features)[:,1])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f6f1c2c0-6d55-4dd5-a55b-185e4beddf82","_cell_guid":"272d6f49-e4eb-4f1b-aa79-5bc93d584edd","trusted":true},"cell_type":"code","source":"print('Count of number of 1s ',submission.target[submission.target == 1].count())\nprint('Count of number of 0s ',submission.target[submission.target == 0].count())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9cc5d30f-e137-40ac-be3b-c293f3928aa1","_cell_guid":"6027704f-13ee-4bba-b9bc-034469d00d48","trusted":true},"cell_type":"code","source":"submission.to_csv('submission_1.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6799f24f-7f47-4d3d-9f25-72a3f2c71294","_cell_guid":"fa6359df-ad6d-415c-9778-f1af26fbcb26","trusted":true},"cell_type":"markdown","source":"# Conclusion\n\nHence, by seeing the above scores, we can conclude that the model is highly imbalanced due to which we're achieving some imbalance in the 0s and 1s predictions.\n\n","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}