Kaggle competition: Tabular Playground Series - Jan 2021
data_all['attribute'] = data_all['attribute'].map(lambda x:string_edit(str(x)))
 data_all['brand'] = data_all['brand'].map(lambda x:string_edit(str(x)) )<string_transform>
train = pd.read_csv(input_path / 'train.csv', index_col='id' )
def remove_html_tag(text):
soup = BeautifulSoup(text, 'lxml')
 text = soup.get_text().replace('Click here to review our return policy for additional information regarding returns', '')
 return text
def str_stemmer(doc):
tokens = doc.split()
table = str.maketrans('', '', punctuation)
 tokens = [w.translate(table)for w in tokens]
tokens = [word for word in tokens if word.isalpha() ]
stop_words = set(stopwords.words('english'))
tokens = [w for w in tokens if not w in stop_words]
tokens = [word for word in tokens if len(word)> 1]
return ' '.join(tokens)
 def str_stemmer_title(s):
return " ".join(map(stemmer.stem, s.lower().split()))
def str_common_word(str1, str2):
whole_set = set(str1.split())
 return sum(int(str2.find(word)>=0)for word in whole_set)
 def get_shared_words(row_data):
return np.sum([str_common_word(*row_data[:-1]), str_common_word(*row_data[1:])] )<feature_engineering>
test = pd.read_csv(input_path / 'test.csv', index_col='id' )
data_all['search_term'] = pd.Series(data_all['search_term'].map(lambda x:str_stemmer(str(x))))
data_all['product_title'] = pd.Series(data_all['product_title'].map(lambda x:str_stemmer(str(x))))
data_all['product_description'] = pd.Series(data_all['product_description'].map(lambda x:str_stemmer(str(x))))
data_all['attribute'] = pd.Series(data_all['attribute'].map(lambda x:str_stemmer(str(x))))<feature_engineering>
submission = pd.read_csv(input_path / 'sample_submission.csv', index_col='id' )
data_all['search_term'] = pd.Series(data_all['search_term'].map(lambda x:remove_html_tag(str(x))))
data_all['product_title'] = pd.Series(data_all['product_title'].map(lambda x:remove_html_tag(str(x))))
data_all['product_description'] = pd.Series(data_all['product_description'].map(lambda x:remove_html_tag(str(x))))<feature_engineering>
target = train.pop('target')
 X_train, X_test, y_train, y_test = train_test_split(train, target, train_size=0.60 )
data_all['search_term_tokens'] = data_all.search_term.str.lower().str.split()
data_all['product_title_tokens'] = data_all.product_title.str.lower().str.split()
data_all['product_description_tokens'] = data_all.product_description.str.lower().str.split()<feature_engineering>
from xgboost import XGBRegressor
import xgboost as xgb
data_all["edistance_sprot"] = [edit_distance(word1, word2)for word1, word2 in
data_all[["search_term","product_title"]].values.tolist() ]
data_all["edistance_sd"] = [edit_distance(word1, word2)for word1, word2 in
data_all[["search_term","product_description"]].values.tolist() ]<concatenate>
regressor = xgb.XGBRegressor(colsample_bytree=0.5,
alpha=0.01,
reg_lambda=0.003,
learning_rate=0.01,
max_depth=15,
min_child_weight=257,
n_estimators=1000,
subsample=0.7,
random_state=2020,
metric_period=100,
silent=1)
 regressor.fit(X_train, y_train, early_stopping_rounds=6, eval_set=[(X_test, y_test)], verbose=1)
 y_pred = regressor.predict(X_test)
 plot_results("XGBRegressor", y_test, y_pred)
def get_jaccard_sim(columns):
str1, str2 = columns[0], columns[1]
a = set(str1)
 b = set(str2)
 c = a.intersection(b)
 return float(len(c)) /(len(a)+ len(b)- len(c))<feature_engineering>
import lightgbm as lgb
data_all['j_dis_sqt'] = [get_jaccard_sim(rows)for rows in data_all[["search_term_tokens","product_title_tokens"]].values]
data_all['j_dis_sqd'] = [get_jaccard_sim(rows)for rows in data_all[["search_term_tokens","product_description_tokens"]].values]<feature_engineering>
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_valid = lgb.Dataset(X_test, y_test)
 param = {
'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,
'learning_rate' : 0.001,
'early_stopping_round' : 1000,
'num_iterations' : 20000
}
lgb_model = lgb.train(param,
lgb_train,
valid_sets=lgb_valid,
num_boost_round=5000,
early_stopping_rounds=100)
 plot_results('LightGBM', y_test, y_pred )
data_all['search_query_length'] = data_all.search_term.str.len()
data_all['number_of_words_in_descr'] = data_all.product_description.str.count("\\w+" )<feature_engineering>
import torch
from torch import nn
data_all['number_of_words_in_search'] = data_all['search_term_tokens'].map(lambda x: len(x))
data_all['number_of_words_in_title'] = data_all['product_title_tokens'].map(lambda x: len(x))
data_all['length_product_description'] = data_all.product_description.str.len()
data_all['length_product_title'] = data_all.product_title.str.len()
data_all['length_attribute'] = data_all.attribute.str.len()<data_type_conversions>
class linearRegression(torch.nn.Module):
def __init__(self, inputSize, outputSize):
super(linearRegression, self ).__init__()
self.head = torch.nn.Linear(inputSize, inputSize//2)
 self.out = torch.nn.Linear(inputSize//2, outputSize)
 def forward(self, x):
y = self.out(self.head(x))
return y
data_all['len_of_attribute'] = data_all['attribute'].map(lambda x:len(str(x ).split())).astype(np.int64)
 data_all['len_of_brand'] = data_all['brand'].map(lambda x:len(str(x ).split())).astype(np.int64 )<compute_test_metric>
model = linearRegression(X_train.shape[-1], 1 )
def cosineSim(v1, v2):
sumxx, sumxy, sumyy = 0, 0, 0
for i in range(len(v1)) :
x = v1[i]
y = v2[i]
sumxx += x*x
sumyy += y*y
sumxy += x*y
return sumxy/math.sqrt(sumxx*sumyy )<choose_model_class>
learningRate = 3e-4
criterion = torch.nn.MSELoss()
optimizer = torch.optim.AdamW(model.parameters() , lr=learningRate )
arr1 = []
arr2 = []
arr3 = []
for i in range(len(data_all)) :
product_title_i = data_all['product_title'][i]
description_i = data_all['product_description'][i]
attribute_i = data_all['attribute'][i]
search_term_i = data_all['search_term'][i]
TfidfVectorizer1 = TfidfVectorizer(ngram_range=(1, 2), stop_words='english')
 TfidfVectorizer2 = TfidfVectorizer(ngram_range=(1, 2), stop_words='english')
 TfidfVectorizer3 = TfidfVectorizer(ngram_range=(1, 2), stop_words='english')
 tf_idf1 = TfidfVectorizer1.fit_transform([product_title_i, search_term_i])
 tf_idf2 = TfidfVectorizer2.fit_transform([description_i, search_term_i])
 tf_idf3 = TfidfVectorizer3.fit_transform([attribute_i, search_term_i])
 cosineSim_title_search = cosineSim(tf_idf1.toarray() [0], tf_idf1.toarray() [1])
 arr1.append(cosineSim_title_search)
 cosineSim_description = cosineSim(tf_idf2.toarray() [0], tf_idf2.toarray() [1])
 arr2.append(cosineSim_description)
 cosineSim_attribute = cosineSim(tf_idf3.toarray() [0], tf_idf3.toarray() [1])
 arr3.append(cosineSim_attribute )<feature_engineering>
if torch.cuda.is_available() :
inputs = torch.Tensor(np.array(X_train)).cuda()
labels = torch.Tensor(np.array(y_train)).cuda()
else:
inputs = torch.Tensor(np.array(X_train))
labels = torch.Tensor(np.array(y_train))
data_all['tfidf_cosineSim_search_title'] = arr1
data_all['tfidf_cosineSim_search_description'] = arr2
data_all['tfidf_cosineSim_search_attribute'] = arr3<feature_engineering>
for epoch in range(10):
for i in range(0, len(labels), 64):
X = inputs[i:i+64]
target = labels[i:i+64]
optimizer.zero_grad()
outputs = model(X ).view(len(target))
loss = criterion(outputs, target)
 loss.backward()
optimizer.step()
print('epoch {}, loss {}'.format(epoch, loss.item()))
data_all['tfidf_cosineSim_search_title'] = data_all['tfidf_cosineSim_search_title'].fillna(data_all['tfidf_cosineSim_search_title'].sum() /len(data_all))
data_all['tfidf_cosineSim_search_description'] = data_all['tfidf_cosineSim_search_description'].fillna(data_all['tfidf_cosineSim_search_description'].sum() /len(data_all))<feature_engineering>
with torch.no_grad() :
if torch.cuda.is_available() :
predicted = model(torch.Tensor(np.array(X_test)).cuda() ).view(len(y_test)).cpu()
else:
predicted = model(torch.Tensor(np.array(X_test)) ).view(len(y_test))
print(criterion(predicted, torch.Tensor(np.array(y_test))))
arr1 = []
arr2 = []
arr3 = []
for i in range(len(data_all)) :
product_title_i = data_all['product_title'][i]
description_i = data_all['product_description'][i]
attribute_i = data_all['attribute'][i]
search_term_i = data_all['search_term'][i]
vectorizer1 = CountVectorizer()
vectorizer2 = CountVectorizer()
vectorizer3 = CountVectorizer()
tf1 = vectorizer1.fit_transform([product_title_i, search_term_i])
 tf2 = vectorizer2.fit_transform([description_i, search_term_i])
 tf3 = vectorizer3.fit_transform([attribute_i, search_term_i])
 cosineSim_title_search = cosineSim(tf1.toarray() [0], tf1.toarray() [1])
 arr1.append(cosineSim_title_search)
 cosineSim_description = cosineSim(tf2.toarray() [0], tf2.toarray() [1])
 arr2.append(cosineSim_description)
 cosineSim_attribute = cosineSim(tf3.toarray() [0], tf3.toarray() [1])
 arr3.append(cosineSim_attribute )<feature_engineering>
submission['target'] = model(torch.Tensor(np.array(test)) ).view(-1 ).detach()
submission.to_csv('res.csv' )