{"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":"Modify from https://www.kaggle.com/code/ahmedelfazouan/g2net-lgbm?scriptVersionId=108222330","metadata":{}},{"cell_type":"markdown","source":"## Use SMOTE and EditedNearestNeighbours to Imporve performance","metadata":{}},{"cell_type":"code","source":"import gc\nimport os\nimport joblib\nimport numpy as np\nimport pandas as pd\nfrom lightgbm import LGBMClassifier\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import precision_recall_fscore_support, accuracy_score\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.preprocessing import QuantileTransformer\nfrom xgboost import XGBClassifier\nfrom catboost import CatBoostClassifier,CatBoostRegressor, Pool, EShapCalcType, EFeaturesSelectionAlgorithm\nfrom sklearn.ensemble import RandomForestRegressor,RandomForestClassifier\nfrom imblearn.over_sampling import KMeansSMOTE,SMOTE,SVMSMOTE\nfrom imblearn.under_sampling import EditedNearestNeighbours","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-24T10:06:34.727181Z","iopub.execute_input":"2022-10-24T10:06:34.727872Z","iopub.status.idle":"2022-10-24T10:06:34.73469Z","shell.execute_reply.started":"2022-10-24T10:06:34.727836Z","shell.execute_reply":"2022-10-24T10:06:34.733502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = [pd.read_csv(f'../input/g2prep/train{i}.csv') for i in range(1,8)]\ntrain = pd.concat(train)\ntrain = train[train['target']!=-1]\ntrain.reset_index(inplace = True, drop = True)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-24T10:06:34.736913Z","iopub.execute_input":"2022-10-24T10:06:34.737553Z","iopub.status.idle":"2022-10-24T10:06:39.768236Z","shell.execute_reply.started":"2022-10-24T10:06:34.737516Z","shell.execute_reply":"2022-10-24T10:06:39.767227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-10-24T10:06:39.770215Z","iopub.execute_input":"2022-10-24T10:06:39.770711Z","iopub.status.idle":"2022-10-24T10:06:41.7238Z","shell.execute_reply.started":"2022-10-24T10:06:39.770673Z","shell.execute_reply":"2022-10-24T10:06:41.722807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_name = 'LGBM'\nFEATURES = [i for i in train.columns if i not in ['id', 'target']]\nskf = KFold(n_splits = 5, shuffle = True, random_state = 42)\noof = np.zeros((len(train),))\npredictions = []\ntest_indexs = []\nmodel_list = []\nfor fold,(train_idx, valid_idx) in enumerate(skf.split(train, train['target'] )):\n    X_train = train.loc[train_idx, FEATURES]\n    y_train = train.loc[train_idx, 'target']\n    sm = SMOTE(random_state=42)\n    X_train, y_train = sm.fit_resample(X_train, y_train)\n    X_val = train.loc[valid_idx, FEATURES]\n    y_val = train.loc[valid_idx, 'target']\n    model = XGBClassifier(n_estimators = 10000)  \n    model.fit(X_train,y_train,eval_set=[(X_val,y_val)],early_stopping_rounds=200, verbose = 100)\n    joblib.dump(model,model_name+'_'+str(fold)+'.pkl')\n    oof[valid_idx] = [i[1] for i in model.predict_proba(X_val)]\n    model_list.append(model)\n\nfor i in range(1,81):\n    test = pd.read_csv(f'../input/g2prep/test{i}.csv')\n    preds = []\n    test_indexs += test['id'].tolist()\n    for model in model_list:\n        preds.append(model.predict_proba(test[FEATURES])[:,1])\n    del test\n    gc.collect()\n    preds =  np.average(preds, axis = 0)\n    predictions+=list(preds)\n    del preds\n    gc.collect()\n\n\n#predictions = np.average(predictions, axis = 0)\nnp.save('oof.npy',oof)\nsub = pd.DataFrame(list(zip(test_indexs, predictions)), columns = ['id','target'])\nsub = sub.groupby('id').mean().reset_index()\nsub.to_csv('submission.csv', index = False)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-24T10:06:41.725199Z","iopub.execute_input":"2022-10-24T10:06:41.725659Z","iopub.status.idle":"2022-10-24T10:06:41.753976Z","shell.execute_reply.started":"2022-10-24T10:06:41.725622Z","shell.execute_reply":"2022-10-24T10:06:41.752297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame(list(zip(set(test_indexs), predictions)), columns = ['id','target'])","metadata":{"execution":{"iopub.status.busy":"2022-10-24T10:06:41.755094Z","iopub.status.idle":"2022-10-24T10:06:41.756Z","shell.execute_reply.started":"2022-10-24T10:06:41.75574Z","shell.execute_reply":"2022-10-24T10:06:41.755765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = sub.groupby('id').mean().reset_index()\nsub.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-10-24T10:06:41.758427Z","iopub.status.idle":"2022-10-24T10:06:41.758818Z","shell.execute_reply.started":"2022-10-24T10:06:41.75865Z","shell.execute_reply":"2022-10-24T10:06:41.758668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2022-10-24T10:06:41.761278Z","iopub.status.idle":"2022-10-24T10:06:41.762166Z","shell.execute_reply.started":"2022-10-24T10:06:41.761885Z","shell.execute_reply":"2022-10-24T10:06:41.761909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}