Kaggle competition: Tabular Playground Series - Jan 2021
<load_from_csv>
import lightgbm as lgb
import optuna.integration.lightgbm as oplgb
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
from tqdm.notebook import tqdm
import matplotlib.pyplot as plt
import seaborn as sns
!unzip.. /input/home-depot-product-search-relevance/attributes.csv.zip
!unzip.. /input/home-depot-product-search-relevance/product_descriptions.csv.zip
!unzip.. /input/home-depot-product-search-relevance/test.csv.zip
!unzip.. /input/home-depot-product-search-relevance/train.csv.zip<install_modules>
df_train = pd.read_csv("/kaggle/input/tabular-playground-series-jan-2021/train.csv")
 df_test = pd.read_csv("/kaggle/input/tabular-playground-series-jan-2021/test.csv")
 df_sample = pd.read_csv("/kaggle/input/tabular-playground-series-jan-2021/sample_submission.csv" )
!pip install bs4<import_modules>
train_id = df_train["id"]
test_id = df_test["id"]
df_train.drop("id", axis=1, inplace=True)
 df_test.drop("id", axis=1, inplace=True )
import pandas as pd
import re
import math
from nltk.stem.snowball import SnowballStemmer
from sklearn.feature_extraction.text import CountVectorizer
import numpy as np
from bs4 import BeautifulSoup
import lxml
import re
import nltk
from nltk.corpus import stopwords
from nltk.metrics import edit_distance
from string import punctuation
from collections import Counter
import numpy as np<load_from_csv>
feature_cols = [c for c in df_train.columns if c != "target"]
train_data = pd.read_csv("train.csv", encoding="ISO-8859-1")
 test_data = pd.read_csv("test.csv", encoding="ISO-8859-1")
 attribute_data = pd.read_csv('attributes.csv')
 descriptions = pd.read_csv('product_descriptions.csv' )<count_duplicates>
train_x = df_train[feature_cols]
train_y = df_train.target
test_x = df_test
train_data[train_data.duplicated(keep='first')].shape<count_duplicates>
folds = KFold(n_splits=10, shuffle=True, random_state=2021 )
test_data[test_data.duplicated(keep='first')].shape<count_duplicates>
class FoldsAverageLGBM:
def __init__(self, folds):
self.folds = folds
self.models = []
def fit(self, lgb_params, train_x, train_y):
oof_preds = np.zeros_like(train_y)
 self.train_x = train_x.values
self.train_y = train_y.values
for tr_idx, va_idx in tqdm(folds.split(train_x)) :
tr_x, va_x = self.train_x[tr_idx], self.train_x[va_idx]
tr_y, va_y = self.train_y[tr_idx], self.train_y[va_idx]
lgb_train_dataset = lgb.Dataset(tr_x, tr_y)
 lgb_valid_dataset = lgb.Dataset(va_x, va_y)
 model = lgb.train(lgb_params, lgb_train_dataset, valid_sets=[lgb_valid_dataset], verbose_eval=100)
 self.models.append(model)
 oof_pred = model.predict(va_x)
 oof_preds[va_idx] = oof_pred
self.oof_preds = oof_preds
def predict(self, test_x):
preds = []
for model in tqdm(self.models):
pred = model.predict(test_x)
 preds.append(pred)
 preds = np.mean(preds, axis=0)
 return preds
attribute_data[attribute_data.duplicated(keep='first')]
attribute_data = attribute_data.drop_duplicates()<count_duplicates>
best_lgb_params = {
'seed': 2021,
'objective': 'regression',
'metric': 'rmse',
'verbosity': -1,
'feature_pre_filter': False,
'lambda_l1': 6.540486456085813,
'lambda_l2': 0.01548480538099245,
'num_leaves': 256,
'feature_fraction': 0.52,
'bagging_fraction': 0.6161835249194311,
'bagging_freq': 7,
'min_child_samples': 20
}
best_lgb_params["learning_rate"] = 0.001
best_lgb_params["early_stopping_round"] = 1000
best_lgb_params["num_iterations"] = 20000
descriptions[descriptions.duplicated(keep='first')].shape<count_values>
folds_average_lgbm = FoldsAverageLGBM(folds )
print('total data has html tags in',descriptions.product_description.str.count('<br$' ).values.sum() )<rename_columns>
folds_average_lgbm.fit(best_lgb_params, train_x, train_y )
df_brand = attribute_data[attribute_data.name == "MFG Brand Name"][["product_uid", "value"]].rename(columns={"value": "brand"} )<feature_engineering>
np.sqrt(mean_squared_error(df_train.target, folds_average_lgbm.oof_preds))
df_attr_stripped = attribute_data
df_attr_stripped['name'] = df_attr_stripped['name'].astype(str)
 df_attr_stripped['name'] = df_attr_stripped['name'].apply(lambda s: re.sub(r"Bullet([0-9]+)", "", s))
df_attr_stripped['attribute'] = df_attr_stripped['name'] + " " + df_attr_stripped['value']
df_attr_test = df_attr_stripped.groupby('product_uid' ).agg({'attribute': lambda s : ' '.join(s.astype(str)) } ).reset_index() 
<concatenate>
y_pred = folds_average_lgbm.predict(test_x )
<merge><EOS> 
sub = df_sample.copy()
sub["target"] = y_pred
sub.to_csv("submission_lgbm_fold_10.csv", index=False)
 sub.head()