{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-02T21:23:27.610994Z","iopub.execute_input":"2023-01-02T21:23:27.611548Z","iopub.status.idle":"2023-01-02T21:23:27.634308Z","shell.execute_reply.started":"2023-01-02T21:23:27.611490Z","shell.execute_reply":"2023-01-02T21:23:27.633035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install swifter","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:23:27.637689Z","iopub.execute_input":"2023-01-02T21:23:27.638116Z","iopub.status.idle":"2023-01-02T21:23:38.971639Z","shell.execute_reply.started":"2023-01-02T21:23:27.638082Z","shell.execute_reply":"2023-01-02T21:23:38.970440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\nimport lightgbm as lgb\nimport swifter\n\nfrom sklearn.preprocessing import LabelEncoder, MinMaxScaler, StandardScaler\nfrom sklearn.model_selection import KFold, ShuffleSplit, StratifiedKFold\nfrom sklearn.metrics import roc_auc_score, log_loss","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:23:38.973507Z","iopub.execute_input":"2023-01-02T21:23:38.974049Z","iopub.status.idle":"2023-01-02T21:23:38.981216Z","shell.execute_reply.started":"2023-01-02T21:23:38.973994Z","shell.execute_reply":"2023-01-02T21:23:38.980017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/emsi-5iir-ds/train.csv')\ntest_df = pd.read_csv('/kaggle/input/emsi-5iir-ds/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:23:38.982846Z","iopub.execute_input":"2023-01-02T21:23:38.983285Z","iopub.status.idle":"2023-01-02T21:23:47.762832Z","shell.execute_reply.started":"2023-01-02T21:23:38.983241Z","shell.execute_reply":"2023-01-02T21:23:47.761361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([train_df, test_df],axis=0).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:23:47.765766Z","iopub.execute_input":"2023-01-02T21:23:47.766231Z","iopub.status.idle":"2023-01-02T21:23:48.501882Z","shell.execute_reply.started":"2023-01-02T21:23:47.766198Z","shell.execute_reply":"2023-01-02T21:23:48.500839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:23:48.503224Z","iopub.execute_input":"2023-01-02T21:23:48.503568Z","iopub.status.idle":"2023-01-02T21:23:48.531938Z","shell.execute_reply.started":"2023-01-02T21:23:48.503537Z","shell.execute_reply":"2023-01-02T21:23:48.530773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.tail()","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:23:48.533544Z","iopub.execute_input":"2023-01-02T21:23:48.533924Z","iopub.status.idle":"2023-01-02T21:23:48.560817Z","shell.execute_reply.started":"2023-01-02T21:23:48.533893Z","shell.execute_reply":"2023-01-02T21:23:48.559670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info(verbose=True, null_counts=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:23:48.562806Z","iopub.execute_input":"2023-01-02T21:23:48.563291Z","iopub.status.idle":"2023-01-02T21:23:48.740245Z","shell.execute_reply.started":"2023-01-02T21:23:48.563233Z","shell.execute_reply":"2023-01-02T21:23:48.739051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):\n    df[f'ch_{i}'] = df['f_27'].str.get(i).apply(ord) - ord('A')","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:23:48.741794Z","iopub.execute_input":"2023-01-02T21:23:48.742936Z","iopub.status.idle":"2023-01-02T21:24:01.829030Z","shell.execute_reply.started":"2023-01-02T21:23:48.742897Z","shell.execute_reply":"2023-01-02T21:24:01.827845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_cols  =  ['f_00',\n             'f_01',\n             'f_02',\n             'f_03',\n             'f_04',\n             'f_05',\n             'f_06',\n             'f_19',\n             'f_20',\n             'f_21',\n             'f_22',\n             'f_23',\n             'f_24',\n             'f_25',\n             'f_26',\n             'f_28',]\ncate_cols = ['f_07',\n             'f_08',\n             'f_09',\n             'f_10',\n             'f_11',\n             'f_12',\n             'f_13',\n             'f_14',\n             'f_15',\n             'f_16',\n             'f_17',\n             'f_18',\n#              'f_27',\n             'f_29',\n             'f_30'] + [f'ch_{i}' for i in range(10)]","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:24:01.830487Z","iopub.execute_input":"2023-01-02T21:24:01.830843Z","iopub.status.idle":"2023-01-02T21:24:01.837903Z","shell.execute_reply.started":"2023-01-02T21:24:01.830812Z","shell.execute_reply":"2023-01-02T21:24:01.836612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in tqdm(num_cols):\n    plt.figure(dpi=150)\n    sns.distplot(df[col])","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:24:01.839225Z","iopub.execute_input":"2023-01-02T21:24:01.839626Z","iopub.status.idle":"2023-01-02T21:25:52.386753Z","shell.execute_reply.started":"2023-01-02T21:24:01.839571Z","shell.execute_reply":"2023-01-02T21:25:52.385647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in tqdm(num_cols):\n    df[col] = (df[col] - df[col].mean()) / df[col].std()","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:25:52.388166Z","iopub.execute_input":"2023-01-02T21:25:52.388496Z","iopub.status.idle":"2023-01-02T21:25:52.894968Z","shell.execute_reply.started":"2023-01-02T21:25:52.388466Z","shell.execute_reply":"2023-01-02T21:25:52.893941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:25:52.896119Z","iopub.execute_input":"2023-01-02T21:25:52.896433Z","iopub.status.idle":"2023-01-02T21:25:55.635527Z","shell.execute_reply.started":"2023-01-02T21:25:52.896405Z","shell.execute_reply":"2023-01-02T21:25:55.634395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in tqdm(cate_cols):\n    plt.figure(dpi=150)\n    sns.countplot(df[col])","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:25:55.640405Z","iopub.execute_input":"2023-01-02T21:25:55.640781Z","iopub.status.idle":"2023-01-02T21:26:04.823095Z","shell.execute_reply.started":"2023-01-02T21:25:55.640750Z","shell.execute_reply":"2023-01-02T21:26:04.822243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in tqdm(cate_cols):\n    map_dict = dict(zip(df[col].unique(), range(df[col].nunique())))\n    df[col] = df[col].map(map_dict)\n    df[f'{col}_count'] = df[col].map(df[col].value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:26:04.824432Z","iopub.execute_input":"2023-01-02T21:26:04.824802Z","iopub.status.idle":"2023-01-02T21:26:06.548897Z","shell.execute_reply.started":"2023-01-02T21:26:04.824771Z","shell.execute_reply":"2023-01-02T21:26:06.547815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = df[df['target'].notna()].reset_index(drop=True)\ntest_df = df[df['target'].isna()].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:26:06.550341Z","iopub.execute_input":"2023-01-02T21:26:06.550700Z","iopub.status.idle":"2023-01-02T21:26:07.892813Z","shell.execute_reply.started":"2023-01-02T21:26:06.550669Z","shell.execute_reply":"2023-01-02T21:26:07.891693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_feature = ['id','target', 'f_27']\nfeature = [x for x in train_df.columns if x not in drop_feature]\nprint(len(feature),feature)","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:26:07.894511Z","iopub.execute_input":"2023-01-02T21:26:07.895008Z","iopub.status.idle":"2023-01-02T21:26:07.901607Z","shell.execute_reply.started":"2023-01-02T21:26:07.894962Z","shell.execute_reply":"2023-01-02T21:26:07.900415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_valid, y_train, y_valid = train_test_split(train_df[feature], train_df['target'], random_state=666)","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:26:07.903037Z","iopub.execute_input":"2023-01-02T21:26:07.903403Z","iopub.status.idle":"2023-01-02T21:26:08.748461Z","shell.execute_reply.started":"2023-01-02T21:26:07.903372Z","shell.execute_reply":"2023-01-02T21:26:08.747414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.naive_bayes import BernoulliNB, MultinomialNB, GaussianNB\nfrom sklearn.svm import SVC\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.neural_network import MLPClassifier\nfrom sklearn.ensemble import AdaBoostClassifier, RandomForestClassifier\n\n\nmodel = RandomForestClassifier()\n\nmodel.fit(X_train, y_train)\n\ny_valid_pre = model.predict_proba(X_valid)[:,1]\n\nprint(f'{str(model)} AUC :{roc_auc_score(y_valid, y_valid_pre)}')\n\nprint(f'{str(model)} LogLoss :{log_loss(y_valid, y_valid_pre)}')\n\ny_pre = model.predict(test_df[feature])","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:26:08.749643Z","iopub.execute_input":"2023-01-02T21:26:08.749969Z","iopub.status.idle":"2023-01-02T21:37:35.299233Z","shell.execute_reply.started":"2023-01-02T21:26:08.749940Z","shell.execute_reply":"2023-01-02T21:37:35.298094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = {'num_leaves': 60, \n          'min_data_in_leaf': 30,\n          'objective': 'binary', \n          'max_depth': -1,\n          'learning_rate': 0.1,\n          \"min_sum_hessian_in_leaf\": 6,\n          \"boosting\": \"gbdt\",\n          \"feature_fraction\": 0.9, \n          \"bagging_freq\": 1,\n          \"bagging_fraction\": 0.8,\n          \"bagging_seed\": 11,\n          \"lambda_l1\": 0.1,             \n          \"verbosity\": -1,\n          \"nthread\": -1,               \n          'metric': {'binary_logloss', 'auc'}, \n          \"random_state\": 2019, \n          }\n\nn_fold = 5\noof_pre = np.zeros(len(train_df))\ny_pre = np.zeros(len(test_df))\n\nkf = KFold(n_splits=n_fold)\nfor fold_, (trn_idx, val_idx) in enumerate(kf.split(train_df)):\n    trn_data = lgb.Dataset(train_df[feature].iloc[trn_idx], label=train_df['target'].iloc[trn_idx])\n    val_data = lgb.Dataset(train_df[feature].iloc[val_idx], label=train_df['target'].iloc[val_idx])\n\n    clf = lgb.train(params,\n                    trn_data,\n                    100000,\n                    valid_sets=[trn_data, val_data],\n                    verbose_eval=50,\n                    early_stopping_rounds=50)\n    \n    oof_pre[val_idx] = clf.predict(train_df[feature].iloc[val_idx], num_iteration=clf.best_iteration)\n\n    y_pre += clf.predict(test_df[feature], num_iteration=clf.best_iteration) / n_fold","metadata":{"execution":{"iopub.status.busy":"2023-01-02T21:37:35.300758Z","iopub.execute_input":"2023-01-02T21:37:35.301353Z","iopub.status.idle":"2023-01-02T22:36:14.833607Z","shell.execute_reply.started":"2023-01-02T21:37:35.301321Z","shell.execute_reply":"2023-01-02T22:36:14.832669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_df = pd.DataFrame()\nres_df['id'] = test_df['id']\nres_df['target'] = y_pre\nres_df.to_csv('/kaggle/working/submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-02T22:36:14.838510Z","iopub.execute_input":"2023-01-02T22:36:14.840052Z","iopub.status.idle":"2023-01-02T22:36:16.553291Z","shell.execute_reply.started":"2023-01-02T22:36:14.840006Z","shell.execute_reply":"2023-01-02T22:36:16.552187Z"},"trusted":true},"execution_count":null,"outputs":[]}]}