{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71969,"databundleVersionId":7884949,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom tqdm import tqdm\nimport scipy.stats as stats\n\nimport lightgbm as lgb\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import f1_score","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:47:30.518754Z","iopub.execute_input":"2024-05-04T11:47:30.519980Z","iopub.status.idle":"2024-05-04T11:47:34.968246Z","shell.execute_reply.started":"2024-05-04T11:47:30.519907Z","shell.execute_reply":"2024-05-04T11:47:34.967225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/binary-classification-offers-on-megamarket/train.csv')\ntest_data = pd.read_csv('/kaggle/input/binary-classification-offers-on-megamarket/test.csv')\n\ntrain_data.goods_price.fillna(np.median(train_data.goods_price.dropna()), inplace=True)\ntemp_1 = np.delete(train_data.goods_category_id.unique(), np.where(np.isnan(train_data.goods_category_id.unique()))).tolist()\n# train_data.goods_category_id.fillna(np.random.choice(temp_1, 1)[0], inplace=True)\ntrain_data.offer_price.fillna(np.median(train_data.offer_price.dropna()), inplace=True)\ntrain_data.goods_category_id.fillna(stats.mode(temp_1)[0], inplace=True)\n\ntest_data.goods_price.fillna(np.median(test_data.goods_price.dropna()), inplace=True)\ntemp_2 = np.delete(test_data.goods_category_id.unique(), np.where(np.isnan(test_data.goods_category_id.unique()))).tolist()\n# test_data.goods_category_id.fillna(np.random.choice(temp_2, 1)[0], inplace=True)\ntest_data.offer_price.fillna(np.median(test_data.offer_price.dropna()), inplace=True)\ntest_data.goods_category_id.fillna(stats.mode(temp_2)[0], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:47:34.970849Z","iopub.execute_input":"2024-05-04T11:47:34.971638Z","iopub.status.idle":"2024-05-04T11:47:43.012908Z","shell.execute_reply.started":"2024-05-04T11:47:34.971571Z","shell.execute_reply":"2024-05-04T11:47:43.011646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"goods_images_items = np.load('/kaggle/input/binary-classification-offers-on-megamarket/goods_image_vectors/goods_image_vectors/items_deperson.npy')\ngoods_images_embeds = np.load('/kaggle/input/binary-classification-offers-on-megamarket/goods_image_vectors/goods_image_vectors/embed_deperson.npy')\ngoods_titles_items = np.load('/kaggle/input/binary-classification-offers-on-megamarket/goods_title_vectors/goods_title_vectors/items_deperson.npy')\ngoods_titles_embeds = np.load('/kaggle/input/binary-classification-offers-on-megamarket/goods_title_vectors/goods_title_vectors/embed_deperson.npy')\n\noffers_images_items = np.load('/kaggle/input/binary-classification-offers-on-megamarket/offer_image_vectors/offer_image_vectors/items_deperson.npy')\noffers_images_embeds = np.load('/kaggle/input/binary-classification-offers-on-megamarket/offer_image_vectors/offer_image_vectors/embed_deperson.npy')\noffers_titles_items = np.load('/kaggle/input/binary-classification-offers-on-megamarket/offer_title_vectors/offer_title_vectors/items_deperson.npy')\noffers_titles_embeds = np.load('/kaggle/input/binary-classification-offers-on-megamarket/offer_title_vectors/offer_title_vectors/embed_deperson.npy')","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:47:43.014420Z","iopub.execute_input":"2024-05-04T11:47:43.014791Z","iopub.status.idle":"2024-05-04T11:47:59.776178Z","shell.execute_reply.started":"2024-05-04T11:47:43.014757Z","shell.execute_reply":"2024-05-04T11:47:59.775152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"goods_titles_list = []\ngoods_images_list = []\noffers_titles_list = []\noffers_images_list = []\n\nfor i in range(len(goods_images_items)):\n    goods_titles_list.append({'goods_depersanalised': goods_images_items[i].astype('int64'), 'goods_title_vector': goods_titles_embeds[i]})\n    goods_images_list.append({'goods_depersanalised': goods_images_items[i].astype('int64'), 'goods_image_vector': goods_images_embeds[i]})\n    offers_titles_list.append({'offer_depersanalised': goods_images_items[i].astype('int64'), 'offer_title_vector': offers_titles_embeds[i]})\n    offers_images_list.append({'offer_depersanalised': goods_images_items[i].astype('int64'), 'offer_image_vector': offers_images_embeds[i]})\n\ngoods_titles_df = pd.DataFrame(goods_titles_list)\ngoods_images_df = pd.DataFrame(goods_images_list)\noffers_titles_df = pd.DataFrame(offers_titles_list)\noffers_images_df = pd.DataFrame(offers_images_list)","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:47:59.777356Z","iopub.execute_input":"2024-05-04T11:47:59.777868Z","iopub.status.idle":"2024-05-04T11:48:13.651263Z","shell.execute_reply.started":"2024-05-04T11:47:59.777835Z","shell.execute_reply":"2024-05-04T11:48:13.650078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_1 = train_data.merge(goods_titles_df, on='goods_depersanalised', how='left')\ntrain_data_1['goods_title_vector'].fillna(0, inplace=True)\n\n\ntest_data_1 = test_data.merge(goods_titles_df, on='goods_depersanalised', how='left')\ntest_data_1['goods_title_vector'].fillna(0, inplace=True)\n\ntest_data_1.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:48:13.654095Z","iopub.execute_input":"2024-05-04T11:48:13.654478Z","iopub.status.idle":"2024-05-04T11:48:15.195941Z","shell.execute_reply.started":"2024-05-04T11:48:13.654445Z","shell.execute_reply":"2024-05-04T11:48:15.194746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_2 = train_data_1.merge(goods_images_df, on='goods_depersanalised', how='left')\n\ntrain_data_2['goods_image_vector'].fillna(0, inplace=True)\n\ntest_data_2 = test_data_1.merge(goods_images_df, on='goods_depersanalised', how='left')\n\ntest_data_2['goods_image_vector'].fillna(0, inplace=True)\n\ntest_data_2.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:48:15.197784Z","iopub.execute_input":"2024-05-04T11:48:15.198321Z","iopub.status.idle":"2024-05-04T11:48:17.179017Z","shell.execute_reply.started":"2024-05-04T11:48:15.198287Z","shell.execute_reply":"2024-05-04T11:48:17.177192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_3 = train_data_2.merge(offers_titles_df, on='offer_depersanalised', how='left')\n\ntrain_data_3['offer_title_vector'].fillna(0, inplace=True)\n\ntest_data_3 = test_data_2.merge(offers_titles_df, on='offer_depersanalised', how='left')\n\ntest_data_3['offer_title_vector'].fillna(0, inplace=True)\n\ntest_data_3.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:48:17.180463Z","iopub.execute_input":"2024-05-04T11:48:17.180840Z","iopub.status.idle":"2024-05-04T11:48:19.671962Z","shell.execute_reply.started":"2024-05-04T11:48:17.180809Z","shell.execute_reply":"2024-05-04T11:48:19.670728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_4 = train_data_3.merge(offers_images_df, on='offer_depersanalised', how='left')\n\ntrain_data_4['offer_image_vector'].fillna(0, inplace=True)\n\ntest_data_4 = test_data_3.merge(offers_images_df, on='offer_depersanalised', how='left')\n\ntest_data_4['offer_image_vector'].fillna(0, inplace=True)\n\ntest_data_4.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:48:19.673354Z","iopub.execute_input":"2024-05-04T11:48:19.673785Z","iopub.status.idle":"2024-05-04T11:48:22.421979Z","shell.execute_reply.started":"2024-05-04T11:48:19.673752Z","shell.execute_reply":"2024-05-04T11:48:22.420638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calculate_dot_product(v1, v2):\n    if np.isscalar(v1) and v1 == 0 or np.isscalar(v2) and v2 == 0:\n        return 0\n    return np.dot(v1, v2)\n\ntrain_data_4['titles_dot'] = train_data_4.apply(lambda row: calculate_dot_product(row['goods_title_vector'], row['offer_title_vector']), axis=1)\ntrain_data_4['images_dot'] = train_data_4.apply(lambda row: calculate_dot_product(row['goods_image_vector'], row['offer_image_vector']), axis=1)\n\ntest_data_4['titles_dot'] = test_data_4.apply(lambda row: calculate_dot_product(row['goods_title_vector'], row['offer_title_vector']), axis=1)\ntest_data_4['images_dot'] = test_data_4.apply(lambda row: calculate_dot_product(row['goods_image_vector'], row['offer_image_vector']), axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:48:22.423037Z","iopub.execute_input":"2024-05-04T11:48:22.423354Z","iopub.status.idle":"2024-05-04T11:50:21.427147Z","shell.execute_reply.started":"2024-05-04T11:48:22.423327Z","shell.execute_reply":"2024-05-04T11:50:21.425899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_data_4.drop(columns=['target', 'id', 'goods_title_vector', 'offer_title_vector', 'goods_image_vector', 'offer_image_vector'])\ny = train_data_4['target']\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)\n\nlgbm_classifier = lgb.LGBMClassifier(n_estimators=500, random_state=42, reg_lambda=0.3, reg_alpha=0.4)","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:50:21.428858Z","iopub.execute_input":"2024-05-04T11:50:21.429315Z","iopub.status.idle":"2024-05-04T11:50:22.058657Z","shell.execute_reply.started":"2024-05-04T11:50:21.429273Z","shell.execute_reply":"2024-05-04T11:50:22.057465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:50:22.060139Z","iopub.execute_input":"2024-05-04T11:50:22.060617Z","iopub.status.idle":"2024-05-04T11:50:22.093074Z","shell.execute_reply.started":"2024-05-04T11:50:22.060550Z","shell.execute_reply":"2024-05-04T11:50:22.091863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_classifier.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:50:22.094440Z","iopub.execute_input":"2024-05-04T11:50:22.094843Z","iopub.status.idle":"2024-05-04T11:51:27.227987Z","shell.execute_reply.started":"2024-05-04T11:50:22.094811Z","shell.execute_reply":"2024-05-04T11:51:27.226770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_val = lgbm_classifier.predict(X_val)\nprint(f1_score(y_val, y_pred_val, average='macro'))","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:51:27.229537Z","iopub.execute_input":"2024-05-04T11:51:27.229907Z","iopub.status.idle":"2024-05-04T11:51:39.741085Z","shell.execute_reply.started":"2024-05-04T11:51:27.229876Z","shell.execute_reply":"2024-05-04T11:51:39.739880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"0.8875819847075844\n","metadata":{}},{"cell_type":"code","source":"test_data_new = pd.read_csv('/kaggle/input/binary-classification-offers-on-megamarket/test.csv')\n\ntest_data_new.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:51:39.746476Z","iopub.execute_input":"2024-05-04T11:51:39.746883Z","iopub.status.idle":"2024-05-04T11:51:40.308132Z","shell.execute_reply.started":"2024-05-04T11:51:39.746850Z","shell.execute_reply":"2024-05-04T11:51:40.306946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_4.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:51:40.309303Z","iopub.execute_input":"2024-05-04T11:51:40.310023Z","iopub.status.idle":"2024-05-04T11:51:40.334371Z","shell.execute_reply.started":"2024-05-04T11:51:40.309991Z","shell.execute_reply":"2024-05-04T11:51:40.333090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_4.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:51:40.335879Z","iopub.execute_input":"2024-05-04T11:51:40.336213Z","iopub.status.idle":"2024-05-04T11:51:40.343916Z","shell.execute_reply.started":"2024-05-04T11:51:40.336187Z","shell.execute_reply":"2024-05-04T11:51:40.342671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_4.drop_duplicates(subset=['id'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:51:40.345239Z","iopub.execute_input":"2024-05-04T11:51:40.345881Z","iopub.status.idle":"2024-05-04T11:51:40.495734Z","shell.execute_reply.started":"2024-05-04T11:51:40.345843Z","shell.execute_reply":"2024-05-04T11:51:40.494710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_4.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:51:40.496822Z","iopub.execute_input":"2024-05-04T11:51:40.497825Z","iopub.status.idle":"2024-05-04T11:51:40.504448Z","shell.execute_reply.started":"2024-05-04T11:51:40.497779Z","shell.execute_reply":"2024-05-04T11:51:40.503396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test_data_4.drop(columns=['id', 'goods_title_vector', 'offer_title_vector', 'goods_image_vector', 'offer_image_vector'])\nprint(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:51:40.505725Z","iopub.execute_input":"2024-05-04T11:51:40.506041Z","iopub.status.idle":"2024-05-04T11:51:40.534979Z","shell.execute_reply.started":"2024-05-04T11:51:40.506014Z","shell.execute_reply":"2024-05-04T11:51:40.533668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = lgbm_classifier.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:51:40.537119Z","iopub.execute_input":"2024-05-04T11:51:40.538050Z","iopub.status.idle":"2024-05-04T11:51:48.838733Z","shell.execute_reply.started":"2024-05-04T11:51:40.538006Z","shell.execute_reply":"2024-05-04T11:51:48.837718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:51:48.840266Z","iopub.execute_input":"2024-05-04T11:51:48.840929Z","iopub.status.idle":"2024-05-04T11:51:48.847836Z","shell.execute_reply.started":"2024-05-04T11:51:48.840896Z","shell.execute_reply":"2024-05-04T11:51:48.846624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"solution = pd.DataFrame({'id': test_data_4.id, 'target': y_pred})\nsolution.set_index('id', inplace=True)\n\n\nsolution.to_csv('/kaggle/working/submit.csv')","metadata":{"execution":{"iopub.status.busy":"2024-05-04T11:51:48.849768Z","iopub.execute_input":"2024-05-04T11:51:48.850211Z","iopub.status.idle":"2024-05-04T11:51:49.441537Z","shell.execute_reply.started":"2024-05-04T11:51:48.850172Z","shell.execute_reply":"2024-05-04T11:51:49.440272Z"},"trusted":true},"execution_count":null,"outputs":[]}]}