Kaggle competition: Tabular Playground Series - Jan 2021
data_all['cosineSim_search_title'] = arr1
data_all['cosineSim_search_description'] = arr2
data_all['cosineSim_search_attribute'] = arr3<feature_engineering>
from catboost import CatBoostRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import optuna
data_all['product_info'] = data_all['search_term']+'\t'+data_all['product_title']+'\t'+data_all['product_description']+'\t'+data_all['attribute']
data_all['word_in_title'] = data_all['product_info'].map(lambda x:str_common_word(str(x ).split('\t')[0],str(x ).split('\t')[1]))
data_all['word_in_description'] = data_all['product_info'].map(lambda x:str_common_word(str(x ).split('\t')[0],str(x ).split('\t')[2]))
data_all['word_in_attributes'] = data_all['product_info'].map(lambda x:str_common_word(str(x ).split('\t')[0],str(x ).split('\t')[3]))
data_all['ratio_title'] = data_all['word_in_title']/data_all['number_of_words_in_search']
data_all['ratio_description'] = data_all['word_in_description']/data_all['number_of_words_in_search']
data_all['ratio_attributes'] = data_all['word_in_attributes']/data_all['number_of_words_in_search']
data_all['title_length'] = data_all.product_title.str.len()<feature_engineering>
SAMPLE_RATE = 0.4
RANDOM_SEED = 1
EARLY_STOPPING_ROUND = 100
data_all['attr'] = str(data_all['search_term'])+"\t"+str(data_all['brand'])
 data_all['brand_in_search'] = data_all['attr'].map(lambda x:str_common_word(str(x ).split('\t')[0],str(x ).split('\t')[1]))
data_all['ratio_brand'] = data_all['brand_in_search']/data_all['len_of_brand']<feature_engineering>
train = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/train.csv')
 test = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/test.csv')
 sample_sub = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/sample_submission.csv' )
data_all['ratio_title'] = data_all['ratio_title'].fillna(data_all['ratio_title'].sum() /len(data_all))
data_all['ratio_description'] = data_all['ratio_description'].fillna(data_all['ratio_description'].sum() /len(data_all))
data_all['ratio_attributes'] = data_all['ratio_attributes'].fillna(data_all['ratio_attributes'].sum() /len(data_all))<normalization>
features = [c for c in train.columns if c not in('id', 'target')]
features
def fuzzy_partial_ratio(string_1 , string_2):
return fuzz.partial_ratio(string_1, string_2 )<compute_test_metric>
X = train_sample[features]
y = train_sample.target
X_test = test[features]
def fuzzy_token_sort_ratio(string_1,string_2):
return fuzz.token_sort_ratio(string_1,string_2 )<feature_engineering>
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, random_state=RANDOM_SEED )
data_all['fuzzy_ratio_in_title'] = data_all['product_info'].map(lambda x:fuzzy_partial_ratio(x.split('\t')[0],x.split('\t')[1]))
data_all['fuzzy_ratio_in_description'] = data_all['product_info'].map(lambda x:fuzzy_partial_ratio(x.split('\t')[0],x.split('\t')[2]))
data_all['fuzzy_ratio_in_attribute'] = data_all['product_info'].map(lambda x:fuzzy_partial_ratio(x.split('\t')[0],x.split('\t')[3]))<feature_engineering>
X_train, X_eval, y_train, y_eval = train_test_split(X_train, y_train, test_size=0.1, random_state=RANDOM_SEED )
data_all['fuzzy_token_sort_ratio_in_title'] = data_all['product_info'].map(lambda x:fuzzy_token_sort_ratio(x.split('\t')[0],x.split('\t')[1]))
data_all['fuzzy_token_sort_ratio_in_description'] = data_all['product_info'].map(lambda x:fuzzy_token_sort_ratio(x.split('\t')[0],x.split('\t')[2]))
data_all['fuzzy_token_sort_ratio_in_attribute'] = data_all['product_info'].map(lambda x:fuzzy_token_sort_ratio(x.split('\t')[0],x.split('\t')[3]))<feature_engineering>
def objective(trial):
param = {}
param['learning_rate'] = trial.suggest_discrete_uniform("learning_rate", 0.001, 0.02, 0.001)
 param['depth'] = trial.suggest_int('depth', 9, 15)
 param['l2_leaf_reg'] = trial.suggest_discrete_uniform('l2_leaf_reg', 1.0, 5.5, 0.5)
 param['min_child_samples'] = trial.suggest_categorical('min_child_samples', [1, 4, 8, 16, 32])
 param['grow_policy'] = 'Depthwise'
param['iterations'] = 10000
param['use_best_model'] = True
param['eval_metric'] = 'RMSE'
param['od_type'] = 'iter'
param['od_wait'] = 20
param['random_state'] = RANDOM_SEED
param['logging_level'] = 'Silent'
regressor = CatBoostRegressor(**param)
 regressor.fit(X_train.copy() , y_train.copy() ,
eval_set=[(X_eval.copy() , y_eval.copy())],
early_stopping_rounds=EARLY_STOPPING_ROUND)
 loss = mean_squared_error(y_valid, regressor.predict(X_valid.copy()))
return loss
tf = TfidfVectorizer()
arr1 =[]
arr2 = []
arr3 = []
arr4 = []
for i in range(len(data_all)) :
arr1.append(tf.fit_transform([data_all['product_description'][i]] ).mean())
 arr3.append(tf.fit_transform([data_all['product_title'][i]] ).mean())
 arr4.append(tf.fit_transform([data_all['attribute'][i]] ).mean())
 data_all['mean_product_des'] = arr1
data_all['mean_product_title'] = arr3
data_all['mean_attribute'] = arr4<feature_engineering>
%%time
study = optuna.create_study(study_name=f'catboost-seed{RANDOM_SEED}')
 study.optimize(objective, n_trials=10000, n_jobs=-1, timeout=24000 )
data_all['last_word_title_match'] = data_all['product_info'].map(lambda x:str_common_word(str(x ).split('\t')[0].split(" ")[-1],str(x ).split('\t')[1]))<feature_engineering>
study.best_value
data_all['last_word_description_match'] = data_all['product_info'].map(lambda x:str_common_word(str(x ).split('\t')[0].split(" ")[-1],str(x ).split('\t')[2]))<feature_engineering>
study.best_params
data_all['first_word_title_match'] = data_all['product_info'].map(lambda x:str_common_word(str(x ).split('\t')[0].split(" ")[0],str(x ).split('\t')[1]))<feature_engineering>
%%time
optimized_regressor = CatBoostRegressor(learning_rate=study.best_params['learning_rate'],
depth=study.best_params['depth'],
l2_leaf_reg=study.best_params['l2_leaf_reg'],
min_child_samples=study.best_params['min_child_samples'],
grow_policy='Depthwise',
iterations=10000,
use_best_model=True,
eval_metric='RMSE',
od_type='iter',
od_wait=20,
random_state=RANDOM_SEED,
logging_level='Silent')
 optimized_regressor.fit(X_train.copy() , y_train.copy() ,
eval_set=[(X_eval.copy() , y_eval.copy())],
early_stopping_rounds=EARLY_STOPPING_ROUND)
 pred_train = optimized_regressor.predict(X_train.copy())
 pred_valid = optimized_regressor.predict(X_valid.copy() )
data_all['first_word_description_match'] = data_all['product_info'].map(lambda x:str_common_word(str(x ).split('\t')[0].split(" ")[0],x.split('\t')[2]))<define_variables>
mean_squared_error(y_train, pred_train )
data_all_test = data_all<drop_column>
mean_squared_error(y_valid, pred_valid )
data_all = data_all.drop(['product_title', 'search_term', 'product_description', 'search_term_tokens', 'product_title_tokens', 'product_description_tokens', 'product_info', 'attr', 'attribute', 'brand'], axis = 1 )<prepare_x_and_y>
%%time
sample_sub['target'] = optimized_regressor.predict(X_test )
train = data_all[:74067]
test = data_all[74067:]<prepare_x_and_y>
sample_sub.to_csv('submission.csv', index=False )