{"cells":[{"metadata":{},"cell_type":"markdown","source":"## 1) Importing data and libraries","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nsns.set()\n\ndf_train = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/train.csv')\ndf_test = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/test.csv')\n\nprint('Train dataset rows:', df_train.shape[0])\ndisplay(df_train.head())\nprint('Test dataset rows:', df_test.shape[0])\ndisplay(df_test.head())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 2) Quick EDA\n\n### Missing values","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Train dataset\\n')\nprint(100*df_train.count().sort_values()/df_train.shape[0])\n\nprint('\\n\\nTest dataset\\n')\nprint(100*df_test.count().sort_values()/df_test.shape[0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Sex","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(18, 4))\ng1 = sns.barplot(x=\"sex\", y=\"age_approx\", data=df_train, estimator=lambda x: len(x) / len(df_train) * 100.0, ax = axes[0]).set(title = 'Train dataset', xlabel = 'Sex', ylabel = 'Frequency (%)', ylim = [0,60])\ng2 = sns.barplot(x=\"sex\", y=\"age_approx\", data=df_test, estimator=lambda x: len(x) / len(df_test) * 100.0, ax = axes[1]).set(title = 'Test dataset', xlabel = 'Sex', ylabel = 'Frequency (%)', ylim = [0,60])\n\nfig, axes = plt.subplots(figsize=(7.3, 4))\ng3 = sns.barplot(x='sex', y='target', data = df_train, estimator = lambda x: np.mean(x) * 100.0, ci = None).set(title = 'Target distribution by Sex', xlabel = 'Sex', ylabel = 'Malignant prob (%)' )","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Approximate patient age","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(18, 4))\ng1 = sns.barplot(x=\"age_approx\", y=\"age_approx\", data=df_train, estimator=lambda x: len(x) / len(df_train) * 100.0, ax = axes[0], color = 'orange')\ng1.set(title = 'Train dataset', xlabel = 'Age', ylabel = 'Frequency (%)', ylim = [0,16])\nplt.setp(g1.get_xticklabels(), rotation=90)\ng2 = sns.barplot(x=\"age_approx\", y=\"age_approx\", data=df_test, estimator=lambda x: len(x) / len(df_test) * 100.0, ax = axes[1], color = 'orange')\ng2.set(title = 'Test dataset', xlabel = 'Age', ylabel = 'Frequency (%)', ylim = [0,16])\nplt.setp(g2.get_xticklabels(), rotation=90)\n\nfig, axes = plt.subplots(figsize=(7.3, 4))\ng3 = sns.barplot(x='age_approx', y='target', data = df_train, estimator = lambda x: np.mean(x) * 100.0, ci = None, color = 'orange')\ng3.set(title = 'Target distribution by Age', xlabel = 'Age', ylabel = 'Malignant prob (%)' )\nplt.setp(g3.get_xticklabels(), rotation=90);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Location of imaged site","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(18, 4))\ng1 = sns.barplot(x=\"anatom_site_general_challenge\", y=\"age_approx\", data=df_train, estimator=lambda x: len(x) / len(df_train) * 100.0, ax = axes[0], color = 'orange', order=df_train.anatom_site_general_challenge.sort_values().unique())\ng1.set(title = 'Train dataset', xlabel = 'Location', ylabel = 'Frequency (%)', ylim = [0,60])\nplt.setp(g1.get_xticklabels(), rotation=30)\ng2 = sns.barplot(x=\"anatom_site_general_challenge\", y=\"age_approx\", data=df_test, estimator=lambda x: len(x) / len(df_test) * 100.0, ax = axes[1], color = 'orange', order=df_test.anatom_site_general_challenge.sort_values().unique())\ng2.set(title = 'Test dataset', xlabel = 'Location', ylabel = 'Frequency (%)', ylim = [0,60])\nplt.setp(g2.get_xticklabels(), rotation=30)\n\nfig, axes = plt.subplots(figsize=(7.3, 4))\ng3 = sns.barplot(x='anatom_site_general_challenge', y='target', data = df_train, estimator = lambda x: np.mean(x) * 100.0, ci = None, color = 'orange', order=df_train.anatom_site_general_challenge.unique())\ng3.set(title = 'Target distribution by Location', xlabel = 'Location', ylabel = 'Malignant prob (%)' )\nplt.setp(g3.get_xticklabels(), rotation=30);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Naive & Lazy Baseline based only on tabular data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from catboost import CatBoostClassifier, Pool, cv\n\ndf_train_naive = df_train[['sex', 'age_approx','anatom_site_general_challenge','target']]\ndf_train_naive.fillna({'sex':'Nan', 'age_approx':df_train['age_approx'].median(), 'anatom_site_general_challenge':'Nan'}, inplace = True)\n\ndf_test_naive = df_test[['sex', 'age_approx','anatom_site_general_challenge']]\ndf_test_naive.fillna({'sex':'Nan', 'age_approx':df_train['age_approx'].median(), 'anatom_site_general_challenge':'Nan'}, inplace = True)\n\ncat_train = Pool(\n    data = df_train_naive.drop(columns = ['target']),\n    label = df_train_naive['target'],\n    cat_features = ['sex', 'anatom_site_general_challenge'])\n\ncat_test = Pool(\n    data = df_test_naive,\n    cat_features = ['sex', 'anatom_site_general_challenge'])\n\nparams = {\n    \"iterations\": 2000,\n    \"random_seed\": 0,\n    \"od_wait\": 20,\n    \"learning_rate\": 0.001,\n    \"loss_function\": 'Logloss',\n    \"eval_metric\": 'AUC'\n    }\n\nscores = cv(cat_train, params, fold_count = 5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_cat = CatBoostClassifier(\n    iterations = 9,\n    random_seed = 0,\n    learning_rate = 0.005,\n    loss_function = 'Logloss'\n)\n\nmodel_cat.fit(cat_train)\n\ndf_submit = df_test.copy()\ndf_submit['target'] = model_cat.predict_proba(cat_test)[:,1]\ndf_submit[['image_name', 'target']].to_csv('submission.csv', index=False )","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}