{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Meta info look like useless  \nWhen I train lgb model to fit the meta feature,I get local cv:0.016 and lb:0.01  \nHowever random result get local cv:0.04 and lb:0.03  \nAnd when I submit (1-pred) result,I get local cv:0.04 and lb:0.04  ","metadata":{}},{"cell_type":"code","source":"import sys\nimport numpy as np \nimport pandas as pd \n\n\nimport os\nsys.path.append('/kaggle/input/lianyhaii/lianyhaii-0.0.143/')","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:14:36.083191Z","iopub.execute_input":"2022-12-02T09:14:36.083972Z","iopub.status.idle":"2022-12-02T09:14:36.111663Z","shell.execute_reply.started":"2022-12-02T09:14:36.08384Z","shell.execute_reply":"2022-12-02T09:14:36.11053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\ntest = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:14:36.14131Z","iopub.execute_input":"2022-12-02T09:14:36.141915Z","iopub.status.idle":"2022-12-02T09:14:36.293665Z","shell.execute_reply.started":"2022-12-02T09:14:36.14188Z","shell.execute_reply":"2022-12-02T09:14:36.292511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:14:36.295671Z","iopub.execute_input":"2022-12-02T09:14:36.296019Z","iopub.status.idle":"2022-12-02T09:14:36.311003Z","shell.execute_reply.started":"2022-12-02T09:14:36.295986Z","shell.execute_reply":"2022-12-02T09:14:36.309394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pfbeta(labels, predictions, beta=1):\n    y_true_count = 0\n    ctp = 0\n    cfp = 0\n\n    for idx in range(len(labels)):\n        prediction = min(max(predictions[idx], 0), 1)\n        if (labels[idx]):\n            y_true_count += 1\n            ctp += prediction\n            cfp += 1 - prediction\n        else:\n            cfp += prediction\n\n    beta_squared = beta * beta\n    c_precision = ctp / (ctp + cfp)\n    c_recall = ctp / y_true_count\n    if (c_precision > 0 and c_recall > 0):\n        result = (1 + beta_squared) * (c_precision * c_recall) / (beta_squared * c_precision + c_recall)\n        return result\n    else:\n        return 0","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:14:36.313351Z","iopub.execute_input":"2022-12-02T09:14:36.314074Z","iopub.status.idle":"2022-12-02T09:14:36.324306Z","shell.execute_reply.started":"2022-12-02T09:14:36.314031Z","shell.execute_reply":"2022-12-02T09:14:36.322795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd \nimport numpy  as np \nimport lianyhaii\nimport warnings \nimport sys \nwarnings.filterwarnings('ignore')\n\n\nlabel = 'cancer'\nlgb_params = {\n    'objective': 'binary',\n    'boosting_type': 'gbdt',\n    'metric': 'auc',\n    'learning_rate': 0.1,\n    'num_leaves': 2**4-1,\n    'max_depth': 4,\n    'tree_learner': 'serial',\n    'colsample_bytree': 0.8,\n    'subsample_freq': 1,\n    'subsample': 0.8,\n    'num_boost_round': 5000,\n    # 'max_bin': 100,\n    'verbose': -1,\n    # 'min_child_samples': 30,\n    'seed': 10,\n    'early_stopping_rounds': 20,\n    'n_jobs': 4,\n    'verbose':-1,\n}\nbase_features = ['site_id','age','implant','laterality']\nnew_features = []","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:14:36.327609Z","iopub.execute_input":"2022-12-02T09:14:36.328053Z","iopub.status.idle":"2022-12-02T09:14:40.676679Z","shell.execute_reply.started":"2022-12-02T09:14:36.328013Z","shell.execute_reply":"2022-12-02T09:14:40.67484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop_duplicates(subset=['patient_id','laterality'],keep='last',inplace=True,ignore_index=True)\ntest.drop_duplicates(subset=['patient_id','laterality'],keep='last',inplace=True,ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:14:40.678636Z","iopub.execute_input":"2022-12-02T09:14:40.679629Z","iopub.status.idle":"2022-12-02T09:14:40.718743Z","shell.execute_reply.started":"2022-12-02T09:14:40.679569Z","shell.execute_reply":"2022-12-02T09:14:40.717497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lianyhaii import feature_tools\necf = feature_tools.encode_cat_feats(train,test,cols=base_features,label=label)\necf.encode_ctg(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:14:40.720513Z","iopub.execute_input":"2022-12-02T09:14:40.7212Z","iopub.status.idle":"2022-12-02T09:14:40.775311Z","shell.execute_reply.started":"2022-12-02T09:14:40.721153Z","shell.execute_reply":"2022-12-02T09:14:40.773785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lianyhaii.model import make_test\nmt = make_test(train,test,base_features=base_features,new_features=[],\n            m_score=[[0.0,0.0]],label=label,metrices=['auc','f1'],log_tool=None)\nmt.init_CV(seed=42,CV_type='skFold',n_split=5,)\noof,pred = mt.lgb_test(lgb_params=lgb_params)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:14:40.777125Z","iopub.execute_input":"2022-12-02T09:14:40.777598Z","iopub.status.idle":"2022-12-02T09:14:42.844139Z","shell.execute_reply.started":"2022-12-02T09:14:40.777559Z","shell.execute_reply":"2022-12-02T09:14:42.842418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['oof'] = oof ","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:14:42.846413Z","iopub.execute_input":"2022-12-02T09:14:42.8491Z","iopub.status.idle":"2022-12-02T09:14:42.859499Z","shell.execute_reply.started":"2022-12-02T09:14:42.849047Z","shell.execute_reply":"2022-12-02T09:14:42.857342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pfbeta(labels=train[label].values,predictions=(train['oof'].values))","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:14:42.862626Z","iopub.execute_input":"2022-12-02T09:14:42.863275Z","iopub.status.idle":"2022-12-02T09:14:42.904092Z","shell.execute_reply.started":"2022-12-02T09:14:42.863225Z","shell.execute_reply":"2022-12-02T09:14:42.902759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pfbeta(labels=train[label].values,predictions=np.random.rand(train.shape[0]))","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:14:42.905754Z","iopub.execute_input":"2022-12-02T09:14:42.906253Z","iopub.status.idle":"2022-12-02T09:14:42.943337Z","shell.execute_reply.started":"2022-12-02T09:14:42.906191Z","shell.execute_reply":"2022-12-02T09:14:42.941529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pfbeta(labels=train[label].values,predictions=(1-train['oof'].values))","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:14:42.947636Z","iopub.execute_input":"2022-12-02T09:14:42.948884Z","iopub.status.idle":"2022-12-02T09:14:42.990289Z","shell.execute_reply.started":"2022-12-02T09:14:42.948833Z","shell.execute_reply":"2022-12-02T09:14:42.989292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pfbeta(labels=train[label].values,predictions=((train['oof'].values)>0.026).astype(int))","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:17:50.806283Z","iopub.execute_input":"2022-12-02T09:17:50.807255Z","iopub.status.idle":"2022-12-02T09:17:50.839345Z","shell.execute_reply.started":"2022-12-02T09:17:50.807186Z","shell.execute_reply":"2022-12-02T09:17:50.838008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pfbeta(labels=train[label].values,predictions=((1-train['oof'].values)<0.98).astype(int))","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:19:14.713843Z","iopub.execute_input":"2022-12-02T09:19:14.714515Z","iopub.status.idle":"2022-12-02T09:19:14.746038Z","shell.execute_reply.started":"2022-12-02T09:19:14.714473Z","shell.execute_reply":"2022-12-02T09:19:14.744932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_sub = pd.DataFrame()\nfinal_sub[\"prediction_id\"] = test['prediction_id']\nfinal_sub[\"cancer\"] = ((pred) > 0.020).astype(int)\n\nfinal_sub.to_csv('submission.csv', index=False)\nfinal_sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-02T09:18:33.476668Z","iopub.execute_input":"2022-12-02T09:18:33.477992Z","iopub.status.idle":"2022-12-02T09:18:33.497418Z","shell.execute_reply.started":"2022-12-02T09:18:33.477902Z","shell.execute_reply":"2022-12-02T09:18:33.495783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}