{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Collect difference ways to generate the tabular from the meta only\n\n- meta                                                    : 0.692\n- meta_xgbc                                               : 0.697\n- meta_xgbr                                               : 0.728\n\nThank to \n    - https://www.kaggle.com/titericz/simple-baseline\n    - https://www.kaggle.com/anshuls235/siim-isic-melanoma-analysis-eda-prediction\n    - https://www.kaggle.com/awsaf49/xgboost-tabular-data-ml-cv-85-lb-787","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport xgboost as xgb\n\nfrom sklearn.preprocessing import MinMaxScaler","execution_count":null,"outputs":[]},{"metadata":{"executionInfo":{"elapsed":63838,"status":"ok","timestamp":1595047546578,"user":{"displayName":"Truong Hoang","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Giujxqai7V-K3i8Z6Dnak8aIEHpz5bJAvIbW5wC=s64","userId":"17815344986200266242"},"user_tz":-420},"id":"Mcn7aBZ0EeHl","trusted":true},"cell_type":"code","source":"dir_file = '../input/siim-isic-melanoma-classification/jpeg'\n\ntrain = 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')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 1. Base","execution_count":null},{"metadata":{"executionInfo":{"elapsed":63833,"status":"ok","timestamp":1595047546579,"user":{"displayName":"Truong Hoang","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Giujxqai7V-K3i8Z6Dnak8aIEHpz5bJAvIbW5wC=s64","userId":"17815344986200266242"},"user_tz":-420},"id":"xj2UQaXQEeHo","outputId":"7e50b5de-0eab-488e-d3e2-c4ae8b8a9c89","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')\n\nL = 15\nfeat = ['sex','age_approx','anatom_site_general_challenge']\n\nM = train.target.mean()\nte = train.groupby(feat)['target'].agg(['mean','count']).reset_index()\nte['ll'] = ((te['mean']*te['count'])+(M*L))/(te['count']+L)\ndel te['mean'], te['count']\n\ntest = test.merge( te, on=feat, how='left' )\ntest['ll'] = test['ll'].fillna(M)\n\nmeta = test.ll.values\nsub['target'] = meta\nsub.to_csv('submission_meta.csv', index=False)\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 2. XGBClassifier","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train['sex'] = train['sex'].fillna('na')\ntrain['anatom_site_general_challenge'] = train['anatom_site_general_challenge'].fillna('na')\n\ntest['sex'] = test['sex'].fillna('na')\ntest['anatom_site_general_challenge'] = test['anatom_site_general_challenge'].fillna('na')\n\ntrain['sex'] = train['sex'].astype(\"category\").cat.codes +1\ntrain['anatom_site_general_challenge'] = train['anatom_site_general_challenge'].astype(\"category\").cat.codes +1\n\ntest['sex'] = test['sex'].astype(\"category\").cat.codes +1\ntest['anatom_site_general_challenge'] = test['anatom_site_general_challenge'].astype(\"category\").cat.codes +1\n\nage_approx = np.nanmean(np.concatenate([np.array(train['age_approx']), np.array(test['age_approx'])]))\ntrain['age_approx'].fillna(age_approx, inplace = True)\ntest['age_approx'].fillna(age_approx, inplace = True)\n\n\nx_train = train[['sex', 'age_approx','anatom_site_general_challenge']]\ny_train = train['target']\n\nx_test = test[['sex', 'age_approx','anatom_site_general_challenge']]\n\ntrain_DMatrix = xgb.DMatrix(x_train, label= y_train)\ntest_DMatrix = xgb.DMatrix(x_test)\n\nparam = {\n    'booster':'gbtree', \n    'eta': 0.3,\n    'num_class': 2,\n}\n\nclf = xgb.XGBClassifier(n_estimators=1000, \n                        max_depth=8, \n                        objective='multi:softprob',\n                        seed=0,  \n                        nthread=-1, \n                        learning_rate=0.1, \n                        num_class = 2)\n\nclf.fit(x_train, y_train)\nmeta_xgbc = clf.predict_proba(x_test)[:,1]\n\nsub['target'] = meta_xgbc\nsub.to_csv('submission_meta_xgbc.csv', index=False)\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 3. XGBRegressor","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# The mode of a set of values is the value that appears most often.\ntrain['age_approx'].fillna(train['age_approx'].mode().values[0], inplace = True)\ntest['age_approx'].fillna(test['age_approx'].mode().values[0], inplace = True)\n\n# age_id\ntrain['age_id_min']  = train['patient_id'].map(train.groupby(['patient_id']).age_approx.min())\ntrain['age_id_max']  = train['patient_id'].map(train.groupby(['patient_id']).age_approx.max())\n\ntest['age_id_min']  = test['patient_id'].map(test.groupby(['patient_id']).age_approx.min())\ntest['age_id_max']  = test['patient_id'].map(test.groupby(['patient_id']).age_approx.max())\n\n# sex_enc\ntrain['sex'] = train['sex'].fillna('unknown')\ntrain['sex'] = train['sex'].astype(\"category\").cat.codes +1\ntest['sex'] = test['sex'].fillna('unknown')\ntest['sex'] = test['sex'].astype(\"category\").cat.codes +1\n\n# anatom_enc\ntrain['anatom_site_general_challenge'] = train['anatom_site_general_challenge'].fillna('na')\ntrain['anatom_site_general_challenge'] = train['anatom_site_general_challenge'].astype(\"category\").cat.codes +1\ntest['anatom_site_general_challenge'] = test['anatom_site_general_challenge'].fillna('na')\ntest['anatom_site_general_challenge'] = test['anatom_site_general_challenge'].astype(\"category\").cat.codes +1\n\n# n_images\ntrain['n_images'] = train.patient_id.map(train.groupby(['patient_id']).image_name.count())\ntest['n_images'] = test.patient_id.map(test.groupby(['patient_id']).image_name.count())\n\n# image_size_scaled\ntrain_images = train['image_name'].values\ntrain_sizes = np.zeros(train_images.shape[0])\nfor i, img_path in enumerate(train_images):\n    train_sizes[i] = os.path.getsize(os.path.join(dir_file, 'train', f'{img_path}.jpg'))\ntrain['image_size'] = train_sizes\n\n\ntest_images = test['image_name'].values\ntest_sizes = np.zeros(test_images.shape[0])\nfor i, img_path in enumerate(test_images):\n    test_sizes[i] = os.path.getsize(os.path.join(dir_file, 'test', f'{img_path}.jpg'))\ntest['image_size'] = test_sizes\n\nscale = MinMaxScaler()\ntrain['image_size_scaled'] = scale.fit_transform(train['image_size'].values.reshape(-1, 1))\ntest['image_size_scaled'] = scale.transform(test['image_size'].values.reshape(-1, 1))\n\n# corr = train.corr(method = 'pearson')\n# corr = corr.abs()\n# corr.style.background_gradient(cmap='inferno')\n\nfeatures = [\n    'age_approx',\n    'age_id_min',\n    'age_id_max',\n    'sex',\n    'anatom_site_general_challenge',\n    'n_images',\n    'image_size_scaled'\n]\n\nx_train = train[features]\ny_train = train['target']\n\nx_test = test[features]\n\n# model = xgb.XGBRegressor(base_score=0.5, booster=None, colsample_bylevel=1,\n#              colsample_bynode=1, colsample_bytree=0.8, gamma=1, gpu_id=-1,\n#              importance_type='gain', interaction_constraints=None,\n#              learning_rate=0.002, max_delta_step=0, max_depth=10,\n#              min_child_weight=1, missing=None, monotone_constraints=None,\n#              n_estimators=700, n_jobs=-1, nthread=-1, num_parallel_tree=1,\n#              objective='binary:logistic', random_state=0, reg_alpha=0,\n#              reg_lambda=1, scale_pos_weight=1, silent=True, subsample=0.8,\n#              tree_method=None, validate_parameters=False, verbosity=None)\n\n# kfold = StratifiedKFold(n_splits=5, random_state=1001, shuffle=True)\n# cv_results = cross_val_score(model, x_train, y_train, cv=kfold, scoring='roc_auc', verbose = 3)\n# print(cv_results.mean())\n\nxgb = xgb.XGBRegressor(base_score=0.5, booster=None, colsample_bylevel=1,\n             colsample_bynode=1, colsample_bytree=0.8, gamma=1, gpu_id=-1,\n             importance_type='gain', interaction_constraints=None,\n             learning_rate=0.002, max_delta_step=0, max_depth=10,\n             min_child_weight=1, missing=None, monotone_constraints=None,\n             n_estimators=700, n_jobs=-1, nthread=-1, num_parallel_tree=1,\n             objective='binary:logistic', random_state=0, reg_alpha=0,\n             reg_lambda=1, scale_pos_weight=1, silent=True, subsample=0.8,\n             tree_method=None, validate_parameters=False, verbosity=None)\n\nxgb.fit(x_train, y_train)\nmeta_xgbr = xgb.predict(x_test)\n\nsub = pd.DataFrame({'image_name': test.image_name.values, 'target': meta_xgbr})\nsub.to_csv('submission_meta_xgbr.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}