{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.metrics import roc_auc_score\nimport matplotlib.pyplot as plt\n\nimport os","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"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')\nsub   = pd.read_csv('../input/siim-isic-melanoma-classification/sample_submission.csv')\n\ntrain.shape, test.shape, sub.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Imputing missing values","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train['sex'] = train['sex'].fillna('na')\ntrain['age_approx'] = train['age_approx'].fillna(0)\ntrain['anatom_site_general_challenge'] = train['anatom_site_general_challenge'].fillna('na')\n\ntest['sex'] = test['sex'].fillna('na')\ntest['age_approx'] = test['age_approx'].fillna(0)\ntest['anatom_site_general_challenge'] = test['anatom_site_general_challenge'].fillna('na')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Grouped target mean by features in training set","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"feat = ['sex','age_approx','anatom_site_general_challenge']\ngrp_mean_train = train.groupby(feat)['target'].agg(['mean']) \\\n                .reset_index().rename(columns={'mean': 'baseline'})\n\ngrp_mean_train","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Join grouped mean values with test set","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"test = test.merge(grp_mean_train, on=feat, how='left' )\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Impute missing values for test set baseline\nUsing mean value of target variable in the training set","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"M = train.target.mean()\nM","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test['baseline'] = test['baseline'].fillna(M)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Make submission using baseline predictions","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.target = test.baseline.values\nsub.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.hist(sub.target,bins=100)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.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}