Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<feature_engineering>
%matplotlib inline
sns.set_style('whitegrid')
 plt.style.use('seaborn-deep')
 plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.serif'] = 'Ubuntu'
plt.rcParams['font.monospace'] = 'Ubuntu Mono'
plt.rcParams['font.size'] = 10
plt.rcParams['axes.labelsize'] = 12
plt.rcParams['axes.titlesize'] = 12
plt.rcParams['xtick.labelsize'] = 8
plt.rcParams['ytick.labelsize'] = 8
plt.rcParams['legend.fontsize'] = 12
plt.rcParams['figure.titlesize'] = 14
plt.rcParams['figure.figsize'] =(12, 8)
 pd.options.mode.chained_assignment = None
pd.options.display.float_format = '{:.2f}'.format
pd.set_option('display.max_columns', 200)
 pd.set_option('display.width', 400)
 warnings.filterwarnings('ignore')
 seed = 12
np.random.seed(seed)
data_all['ratio_title'] = data_all['word_in_title']/data_all['len_of_query']<feature_engineering>
base = '/kaggle/input/tabular-playground-series-jan-2021/'
data_file = base + "train.csv"
df = pd.read_csv(data_file)
 df.head()
data_all['ratio_description'] = data_all['word_in_description']/data_all['len_of_query']<feature_engineering>
data_file = base + "test.csv"
df_test = pd.read_csv(data_file)
 df_test.head()
data_all['ratio_attributes'] = data_all['word_in_attributes']/data_all['len_of_query']<feature_engineering>
df.drop('id', inplace=True, axis=1 )
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>
print(df.shape)
 df.drop_duplicates(inplace=True)
 print(df.shape )
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>
df.isna().any().sum()
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>
skewed_features = df.apply(lambda x: x.skew() ).sort_values(ascending=False)
 skewed_features
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]))<create_dataframe>
data_all_copy1 = data_all.copy()<drop_column>
df_shuffle = df.sample(frac=1, random_state=seed ).reset_index(drop=True)
 df_y = df_shuffle.pop('target')
 df_X = df_shuffle
X_train, X_test, y_train, y_test = skms.train_test_split(df_X, df_y, train_size=0.9, random_state=seed)
 print(f"Train set has {X_train.shape[0]} records out of {len(df_shuffle)} which is {round(X_train.shape[0]/len(df_shuffle)*100)}%")
 print(f"Test set has {X_test.shape[0]} records out of {len(df_shuffle)} which is {round(X_test.shape[0]/len(df_shuffle)*100)}%" )
data_all = data_all.drop(['attr','product_info'],axis=1 )<feature_engineering>
import sklearn.linear_model as sklm
data_all["prod_desc_merge"] = data_all["product_description"].map(str)+' '+data_all["brand"].fillna('')+ ' ' + data_all["attribute"].fillna('' ).map(str )<choose_model_class>
scaler = skp.MinMaxScaler()
X_train = pd.DataFrame(scaler.fit_transform(X_train), columns=X_train.columns)
 X_test = pd.DataFrame(scaler.transform(X_test), columns=X_train.columns)
 X_train.describe()
tfidf = TfidfVectorizer(ngram_range=(1, 2), stop_words='english')
 svd = TruncatedSVD(n_components=100, random_state = 2019 )<choose_model_class>
def expm1(x):
return np.expm1(x)
 def getRmse(y_train, y_train_pred):
print(skm.mean_squared_error(y_train, y_train_pred))
pipe = Pipeline(steps=[('tfidf', tfidf),('svd', svd)] )<categorify>
lmr = sklm.Ridge(alpha=0.001)
 lmr.fit(X_train, y_train)
 y_train_pred = lmr.predict(X_train)
 y_test_pred = lmr.predict(X_test)
 getRmse(y_train, y_train_pred)
 getRmse(y_test, y_test_pred )
start_time = time.time()
data_all["product_title"]=pipe.fit_transform(data_all["product_title"])
 data_all["search_term"]=pipe.fit_transform(data_all["search_term"])
 data_all["prod_desc_merge"]=pipe.fit_transform(data_all["prod_desc_merge"])
 print("--- %s seconds ---" %(time.time() - start_time))<drop_column>
params = {'alpha': [0.0001, 0.001, 0.005, 0.01, 0.03, 0.05, 0.1, 0.5, 1.0, 5.0, 10]}
ridge = sklm.Ridge()
model_cv_ridge = skms.GridSearchCV(estimator = ridge, n_jobs=-1, param_grid = params,
scoring= 'neg_mean_squared_error', cv = 5,
return_train_score=True, verbose = 3)
 model_cv_ridge.fit(X_train, y_train)
 print(model_cv_ridge.best_estimator_)
 y_train_pred = model_cv_ridge.predict(X_train)
 y_test_pred = model_cv_ridge.predict(X_test)
 getRmse(y_train, y_train_pred)
 getRmse(y_test, y_test_pred )
data_all.drop(["product_description","brand","attribute"],axis=1,inplace=True )<filter>
cbr = cb.CatBoostRegressor(loss_function='RMSE', verbose=0)
 cbr.fit(X_train, y_train, eval_set=(X_test, y_test))
print(cbr.best_score_)
 y_train_pred = cbr.predict(X_train)
 y_test_pred = cbr.predict(X_test)
 getRmse(y_train, y_train_pred)
 getRmse(y_test, y_test_pred )
missing = pd.DataFrame(data_all.isnull().sum())
 missing[missing[0]>0]<define_variables>
xgb = ske.GradientBoostingRegressor(criterion='mse', random_state=1)
 xgb.fit(X_train, y_train)
 y_train_pred = xgb.predict(X_train)
 y_test_pred = xgb.predict(X_test)
 getRmse(y_train, y_train_pred)
 getRmse(y_test, y_test_pred )
num_train=train.shape[0]<split>
xgb = ske.ExtraTreesRegressor(criterion='mse', random_state=1)
 xgb.fit(X_train, y_train)
 y_train_pred = xgb.predict(X_train)
 y_test_pred = xgb.predict(X_test)
 getRmse(y_train, y_train_pred)
 getRmse(y_test, y_test_pred )
df_train = data_all.iloc[:num_train]
df_test = data_all.iloc[num_train:]<compute_test_metric>
xgb = ske.RandomForestRegressor(criterion='mse', random_state=1)
 xgb.fit(X_train, y_train)
 y_train_pred = xgb.predict(X_train)
 y_test_pred = xgb.predict(X_test)
 getRmse(y_train, y_train_pred)
 getRmse(y_test, y_test_pred )
def fmean_squared_error(ground_truth, predictions):
fmean_squared_error_ = mean_squared_error(ground_truth, predictions)**0.1
return fmean_squared_error_<compute_test_metric>
xgb = xg.XGBRegressor(objective ='reg:squarederror', random_state=1)
 xgb.fit(X_train, y_train)
 y_train_pred = xgb.predict(X_train)
 y_test_pred = xgb.predict(X_test)
 getRmse(y_train, y_train_pred)
 getRmse(y_test, y_test_pred )
RMSE = make_scorer(fmean_squared_error, greater_is_better=False )<prepare_x_and_y>
print("TF version:-", tf.__version__)
 tf.random.set_seed(seed )
df_test_1 = df_test.copy()
df_train_1 = df_train.copy()
id_test = df_test_1['id']
y_train = df_train_1['relevance'].values
X_train = df_train_1.drop(['id','relevance'],axis=1 ).values
X_test = df_test_1.drop(['id','relevance'],axis=1 ).values<choose_model_class>
epochs = 40
model_1 = k.models.Sequential([
k.layers.Dense(512, activation='relu', input_shape=(X_train.shape[1],)) ,
k.layers.Dropout(0.2),
k.layers.Dense(256, activation='relu'),
k.layers.Dropout(0.2),
k.layers.Dense(1, activation='linear'),
])
 print(model_1.summary())
 model_1.compile(optimizer='adam',
loss='mse',
metrics='mse' 
)
 history = model_1.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=epochs,
callbacks=[callbacks_list] )
gbr = GradientBoostingRegressor()<define_search_space>
def getTestResults(m=None):
df_final = df.sample(frac=1, random_state=1 ).reset_index(drop=True)
 test_cols = [x for x in df.columns if 'target' not in x]
df_final_test = df_test[test_cols]
df_y = df_final.pop('target')
 df_X = df_final
scaler = skp.MinMaxScaler()
df_X = pd.DataFrame(scaler.fit_transform(df_X), columns=df_X.columns)
 X_test = pd.DataFrame(scaler.transform(df_final_test), columns=df_X.columns)
 if m is None:
lmr = cb.CatBoostRegressor(loss_function='RMSE', verbose=0)
 lmr.fit(df_X, df_y)
 else:
lmr = m
y_train_pred = lmr.predict(df_X)
 y_test_pred = lmr.predict(X_test)
 if m is not None:
y_test_pred = [y[0] for y in y_test_pred]
getRmse(df_y, y_train_pred)
 return y_test_pred
results = getTestResults()
param_grid = {
'n_estimators' : np.array([int(e)for e in np.linspace(90, 100, 11)]),
'max_depth': np.array([int(e)for e in np.linspace(3, 6, 4)])
 }<define_search_space>
submission = pd.DataFrame({
'id': df_test['id'],
'target': results,
})
 submission.head()
<choose_model_class><EOS> 
submission.to_csv('./submission_Catboost.csv', index=False )