Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<merge>
!pip3 install -qq optuna
sns.set()
data_all = pd.merge(data_all, df_brand, how = 'left', on = 'product_uid' )<merge>
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")
 print(train.shape)
 print(test.shape)
 X, y = train.iloc[:,1:-1].values, train.iloc[:,-1].values
feat_names = list(train.columns[1:-1])
 kfold = KFold(n_splits=5,random_state=2021, shuffle=True)
 X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.15, random_state=2021 )
data_all = pd.merge(data_all, df_attr_test, how = 'left', on = 'product_uid' )<categorify>
baseline_score = mean_squared_error(y,np.mean(y)* np.ones(y.shape[0]),squared=False)
 lr = LinearRegression()
lr_scores = cross_val_score(lr,X, y, cv=kfold,scoring='neg_root_mean_squared_error')
 lasso = Lasso()
lasso_scores = cross_val_score(lasso,X, y, cv=kfold,scoring='neg_root_mean_squared_error')
 ridge = Ridge()
ridge_scores = cross_val_score(ridge,X, y, cv=kfold,scoring='neg_root_mean_squared_error')
 print(-lr_scores, np.mean(-lr_scores))
print(-lasso_scores, np.mean(-lasso_scores))
print(-ridge_scores, np.mean(-ridge_scores))
fig, ax = plt.subplots(ncols=4,nrows=2, figsize=(20, 10))
ridge.fit(X, y)X
lr.fit(X,y)
 feat_names2, coef_order = zip(*sorted(zip(feat_names,ridge.coef_), key=lambda k : abs(k[1]), reverse=True))
sns.barplot(list(coef_order),list(feat_names2), ax=ax[0,0])
 sns.scatterplot(y - ridge.predict(X), y,ax=ax[0,1])
 sns.distplot(y - ridge.predict(X),ax=ax[0,2])
 sns.distplot(ridge.predict(X),ax=ax[0,3])
 feat_names2, coef_order2 = zip(*sorted(zip(feat_names,lr.coef_), key=lambda k : abs(k[1]), reverse=True))
sns.barplot(list(coef_order2),list(feat_names2), ax=ax[1,0])
 sns.scatterplot(y - lr.predict(X), y,ax=ax[1,1])
 sns.distplot(y - lr.predict(X),ax=ax[1,2])
 sns.distplot(lr.predict(X),ax=ax[1,3])
 print("Ridge on full dataset",mean_squared_error(y, ridge.predict(X), squared=False))
def string_edit(s):
if isinstance(s, str):
s = re.sub(r"(\w)\.( [A-Z])", r"\1 \2", s)
 s = s.lower()
s = s.replace(" "," ")
 s = s.replace(",","")
 s = s.replace("$"," ")
 s = s.replace("?"," ")
 s = s.replace("-"," ")
 s = s.replace("//","/")
 s = s.replace(".. ",".")
 s = s.replace(" / "," ")
 s = s.replace(" \\ "," ")
 s = s.replace(".",".")
 s = re.sub(r"(^\.|/)", r"", s)
 s = re.sub(r"(\.|/)$", r"", s)
 s = re.sub(r"([0-9] )([a-z])", r"\1 \2", s)
 s = re.sub(r"([a-z] )([0-9])", r"\1 \2", s)
 s = s.replace(" x "," xbi ")
 s = re.sub(r"([a-z] )(*)\.( * )([a-z])", r"\1 \4", s)
 s = re.sub(r"([a-z] )(*)/(* )([a-z])", r"\1 \4", s)
 s = s.replace("*"," xbi ")
 s = s.replace(" by "," xbi ")
 s = re.sub(r"([0-9] )(*)\.( * )([0-9])", r"\1.\4", s)
 s = re.sub(r"([0-9]+ )(* )(inches|inch|in|')\.?", r"\1in.", s)
 s = re.sub(r"([0-9]+ )(* )(foot|feet|ft|'')\.?", r"\1ft.", s)
 s = re.sub(r"([0-9]+ )(* )(pounds|pound|lbs|lb)\.?", r"\1lb.", s)
 s = re.sub(r"([0-9]+ )(* )(square|sq)?\.?(feet|foot|ft)\.?", r"\1sq.ft.", s)
 s = re.sub(r"([0-9]+ )(* )(cubic|cu)?\.?(feet|foot|ft)\.?", r"\1cu.ft.", s)
 s = re.sub(r"([0-9]+ )(* )(gallons|gallon|gal)\.?", r"\1gal.", s)
 s = re.sub(r"([0-9]+ )(* )(ounces|ounce|oz)\.?", r"\1oz.", s)
 s = re.sub(r"([0-9]+ )(* )(centimeters|cm)\.?", r"\1cm.", s)
 s = re.sub(r"([0-9]+ )(* )(milimeters|mm)\.?", r"\1mm.", s)
 s = s.replace("°"," degrees ")
 s = re.sub(r"([0-9]+ )(* )(degrees|degree)\.?", r"\1deg.", s)
 s = s.replace(" v "," volts ")
 s = re.sub(r"([0-9]+ )(* )(volts|volt)\.?", r"\1volt.", s)
 s = re.sub(r"([0-9]+ )(* )(watts|watt)\.?", r"\1watt.", s)
 s = re.sub(r"([0-9]+ )(* )(amperes|ampere|amps|amp)\.?", r"\1amp.", s)
 s = s.replace(" "," ")
 s = s.replace("."," ")
 s = re.sub(r"zero\.?", r"0 ", s)
 s = re.sub(r"one\.?", r"1 ", s)
 s = re.sub(r"two\.?", r"2 ", s)
 s = re.sub(r"three\.?", r"3 ", s)
 s = re.sub(r"four\.?", r"4 ", s)
 s = re.sub(r"five\.?", r"5 ", s)
 s = re.sub(r"six\.?", r"6 ", s)
 s = re.sub(r"seven\.?", r"7 ", s)
 s = re.sub(r"eight\.?", r"8 ", s)
 s = re.sub(r"nine\.?", r"9 ", s)
 return s
else:
return "null"<feature_engineering>
%%time
i = 0
for train_index, test_index in tqdm(kfold.split(X)) :
cr = CatBoostRegressor(verbose=False,iterations=2000, learning_rate=0.01,
random_strength=52, max_depth=12,random_seed=2021)
 cr.fit(X[train_index,:], y[train_index])
 train[f'target{i}'] = cr.predict(X)
 test[f'target{i}'] = cr.predict(test.iloc[:,1:].values)
 i += 1
train['target_final'] = train.loc[:,["target"+str(i)for i in range(5)]].mean(axis=1)
 print("In Sample: ", mean_squared_error(y, train['target_final'], squared=False))
test['target'] = test[["target"+str(i)for i in range(5)]].mean(axis=1)
 test[['id','target']].to_csv("submission.csv",index=False )
data_all['search_term'] = data_all['search_term'].map(lambda x:string_edit(str(x)))
 data_all['product_title'] = data_all['product_title'].map(lambda x:string_edit(str(x)))
 data_all['product_description'] = data_all['product_description'].map(lambda x:string_edit(str(x)) )<feature_engineering>