{"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\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-22T03:21:02.520065Z","iopub.execute_input":"2022-10-22T03:21:02.52076Z","iopub.status.idle":"2022-10-22T03:21:05.404365Z","shell.execute_reply.started":"2022-10-22T03:21:02.520656Z","shell.execute_reply":"2022-10-22T03:21:05.402828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain = [pd.read_csv(f'../input/g2net-prepare-features/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-22T03:21:05.407148Z","iopub.execute_input":"2022-10-22T03:21:05.408421Z","iopub.status.idle":"2022-10-22T03:21:17.759117Z","shell.execute_reply.started":"2022-10-22T03:21:05.408365Z","shell.execute_reply":"2022-10-22T03:21:17.75793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', 500)\ntrain.describe()","metadata":{"execution":{"iopub.status.busy":"2022-10-22T03:21:17.761102Z","iopub.execute_input":"2022-10-22T03:21:17.761566Z","iopub.status.idle":"2022-10-22T03:21:19.737047Z","shell.execute_reply.started":"2022-10-22T03:21:17.76152Z","shell.execute_reply":"2022-10-22T03:21:19.735815Z"},"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    enn = EditedNearestNeighbours()\n    X_train, y_train = enn.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/g2net-prepare-features/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)\n#np.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-22T03:21:19.739113Z","iopub.execute_input":"2022-10-22T03:21:19.739583Z","iopub.status.idle":"2022-10-22T03:45:02.784098Z","shell.execute_reply.started":"2022-10-22T03:21:19.739529Z","shell.execute_reply":"2022-10-22T03:45:02.781096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2022-10-22T03:45:02.79139Z","iopub.status.idle":"2022-10-22T03:45:02.792493Z","shell.execute_reply.started":"2022-10-22T03:45:02.792248Z","shell.execute_reply":"2022-10-22T03:45:02.792275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}