Kaggle competition: Data Science - Master
<SOS> metric: rmse Kaggle data source: data-science-master<train_model>
import datetime
import numpy as np
import os
import pandas as pd
from lightgbm import LGBMRegressor
from sklearn.model_selection import cross_val_score, KFold, GridSearchCV
def find_top_seeds(X_train,y_train,X_valid,y_valid,params, range_min = 0, range_max = 10):
metric_dict = pd.DataFrame(columns=['state','rmse'])
 for i in range(range_min,range_max):
print('--- random state : {} ---
'.format(i))
params['random_seed'] = i
catmodel = CatBoostRegressor(**params)
 catmodel.fit(X_train,y_train,
eval_set=(X_valid,y_valid),
plot=False 
)
 best_rmse_valid = catmodel.best_score_['validation']['RMSE']
metric_dict.loc[i-range_min] = [int(i)] + [best_rmse_valid]
metric_dict['state'] = metric_dict['state'].astype(np.int32)
 return metric_dict.sort_values(by='rmse', ascending = 'False' )<compute_test_metric>
def ImportData() :
train = pd.read_csv(os.path.join('.. /input/data-science-master/train_booking.csv', 'train_booking.csv'), sep=",", encoding="utf-8")
 val = train = pd.read_csv(os.path.join('.. /input/data-science-master/test_booking.csv', 'test_booking.csv'), sep=",", encoding="utf-8")
 return train, val
metric = find_top_seeds(X_train_pred,y_train,X_valid_pred, y_valid,
params=params_catboost, range_min=0, range_max=10 )<train_on_grid>
def Cible(train, val, cible):
train = train.drop(columns = ['id'])
 id_val = val['id']
val = val.drop(columns = ['id'])
 y_train = train[cible]
train = train.drop(columns = [cible])
 train = train[~(y_train.isnull())].reset_index(drop = True)
 y_train = y_train[~(y_train.isnull())]
return train, val, y_train, id_val
def TextToDate(train, val, list_col):
for col in list_col:
train[col] = pd.to_datetime(train[col].str[:10])
 val[col] = pd.to_datetime(val[col].str[:10])
 return(train, val )
predictions_test = []
for i in [4,8,6]:
print('random state {}'.format(i))
params_catboost['random_seed'] = i
catmodel_bagging_pred = CatBoostRegressor(**params_catboost)
 catmodel_bagging_pred.fit(X_train_pred,y_train,
eval_set=(X_valid_pred,y_valid),
plot=False 
)
 predict = catmodel_bagging_pred.predict(X_valid_pred ).clip(0,20)
 r2 = r2_score(y_valid,predict)
 print('r2_score est {}'.format(r2))
predictions_test.append(catmodel_bagging_pred.predict(X_test_pred ).clip(0,20))<prepare_output>
def TreatVariables(train, val, dates, split):
train = train.drop(columns = ['host_id'])
 val = val.drop(columns = ['host_id'])
 for col in dates:
train[col + '_an'] = train[col].dt.year
val[col + '_an'] = val[col].dt.year
train[col + '_mois'] = train[col].dt.month
val[col + '_mois'] = val[col].dt.month
train[col + '_jour'] = train[col].dt.day
val[col + '_jour'] = val[col].dt.day
train[col + '_heure'] = train[col].dt.hour
val[col + '_heure'] = val[col].dt.hour
train[col + '_ecart'] =(datetime.datetime.now() - train[col] ).dt.days
val[col + '_ecart'] =(datetime.datetime.now() - val[col] ).dt.days
train = train.drop(columns = [col])
 val = val.drop(columns = [col])
 for var in split:
train[var + '_nb'] = train[var].map(lambda row: len(str(row ).split(',')))
 val[var + '_nb'] = val[var].map(lambda row: len(str(row ).split(',')))
 modalites = list(train[var].astype(str ).to_numpy().flat)
 modalites = list(set(','.join(modalites ).split(',')))
 for modalite in modalites:
col = var + '_' + ''.join(e for e in modalite if e.isalnum())
 train[col] = train[var].str.contains(modalite ).astype(float)
 val[col] = val[var].str.contains(modalite ).astype(float)
 train = train.drop(columns = [var])
 val = val.drop(columns = [var])
 return train, val
predictions = np.mean(np.array(predictions_test), axis = 0 )<save_to_csv>
def Remove(train, val):
texte = ['name','description','neighborhood_overview','notes','transit']
texte += ['access','interaction','house_rules']
train = train.drop(columns = texte)
 val = val.drop(columns = texte)
 geo = ['latitude_booking', 'longitude_booking', 'geolocation', 'geopoint_announce']
train = train.drop(columns = geo)
 val = val.drop(columns = geo)
 return train, val
dt_test_opt = pd.DataFrame()
dt_test_opt['item_cnt_month'] = predictions
dt_test_opt.index.name='ID'
dt_test_opt.to_csv('submission_featurev11_bagging_[4_6_8].csv' )<define_variables>
def OneHot(train, val):
categorielles = train.dtypes[train.dtypes == 'object'].index.tolist()
for var in categorielles:
for modalite in train[var].unique() :
if modalite in val[var].unique() :
if modalite == modalite:
col = var + '_' + ''.join(e for e in modalite if e.isalnum() and e not in ['É', 'é', 'ô'])
 train[col] =(train[var] == modalite ).astype(float)
 val[col] =(val[var].astype(str)== modalite ).astype(float)
 else:
col = var + '_' + str(mod)
 train[col] = train[var].isnull().astype(float)
 val[col] = val[var].isnull().astype(float)
 train = train.drop(columns = categorielles)
 val = val.drop(columns = categorielles)
 return train, val
train_path = ".. /input/competitive-data-science-predict-future-sales/sales_train.csv"
test_path = ".. /input/competitive-data-science-predict-future-sales/test.csv"
items_path = ".. /input/competitive-data-science-predict-future-sales/items.csv"
shops_path = ".. /input/competitive-data-science-predict-future-sales/shops.csv"
item_cat_path = ".. /input/competitive-data-science-predict-future-sales/item_categories.csv"<load_from_csv>
dir_path = os.path.join('.. /input', os.listdir('.. /input')[0])
 print("Files are :", os.listdir(dir_path))
def load_data(dir_path, filename):
df = pd.read_csv(os.path.join(dir_path, filename), sep=",", encoding="utf-8")
 return df
train_data = load_data(dir_path, os.path.join('train_booking.csv', 'train_booking.csv'))
test_data = load_data(dir_path, os.path.join('test_booking.csv', 'test_booking.csv'))
items = pd.read_csv(items_path)
 cats = pd.read_csv(item_cat_path)
 shops = pd.read_csv(shops_path)
 train = pd.read_csv(train_path)
 test = pd.read_csv(test_path)
<feature_engineering>
%%time
print('--- 1.Import Data ---
')
 train_raw, val_raw = ImportData()
train_raw = train_data
val_raw = test_data
print('--- 2.Pretraitement ---')
 print('--- Cible & id ---')
 train, val, y_train, id_val = Cible(train_raw, val_raw, 'price')
 print('--- Texte vers Date ---')
 train, val = TextToDate(train, val, ['host_since', 'first_review', 'last_review'])
 print('--- Feat engineering ---')
 train, val = TreatVariables(train, val, ['host_since', 'first_review', 'last_review'],['host_verifications', 'amenities'])
 print('--- Col inutiles ---')
 train, val = Remove(train, val)
 print('--- One Hot Encoding ---')
 train, val = OneHot(train, val)
 print('
---------------------------')
 print('Done.' )
train = train[train.item_price > 0].reset_index(drop = True)
 train.loc[train.item_cnt_day < 1, "item_cnt_day"] = 0 
<feature_engineering>
xx, xxx, yy, yyy = train_test_split(train, y_train ,test_size=0.3, random_state=42 )
train.loc[train.shop_id == 0, "shop_id"] = 57
test.loc[test.shop_id == 0 , "shop_id"] = 57
train.loc[train.shop_id == 1, "shop_id"] = 58
test.loc[test.shop_id == 1 , "shop_id"] = 58
train.loc[train.shop_id == 11, "shop_id"] = 10
test.loc[test.shop_id == 11, "shop_id"] = 10
train.loc[train.shop_id == 40, "shop_id"] = 39
test.loc[test.shop_id == 40, "shop_id"] = 39<feature_engineering>
model = LGBMRegressor(num_leaves = 35, learning_rate = 0.1, n_estimators = 1000,
colsample_bytree = 0.4, subsample = 0.2)
 model.fit(train, y_train )
shops.loc[ shops.shop_name == 'Сергиев Посад ТЦ "7Я"',"shop_name" ] = 'СергиевПосад ТЦ "7Я"'
shops["city"] = shops.shop_name.str.split(" " ).map(lambda x: x[0])
 shops["category"] = shops.shop_name.str.split(" " ).map(lambda x: x[1])
 shops.loc[shops.city == "!Якутск", "city"] = "Якутск" 
<feature_engineering>
rfr_pred= model.predict(xxx)
 rfr_pred = rfr_pred.reshape(-1,1)
 R2_val = model.score(xxx,yyy)
 print("R² validation: {}".format(round(R2_val,4)))
 print('MAE validation:', metrics.mean_absolute_error(yyy, rfr_pred))
print('MSE validation:', metrics.mean_squared_error(yyy, rfr_pred))
print('RMSE validation:', np.sqrt(metrics.mean_squared_error(yyy, rfr_pred)))
 rfr_pred= model.predict(xx)
 rfr_pred = rfr_pred.reshape(-1,1)
 R2_train = model.score(xx,yy)
 print("R² train: {}".format(round(R2_train,4)))
 print('MAE train:', metrics.mean_absolute_error(yy, rfr_pred))
print('MSE train:', metrics.mean_squared_error(yy, rfr_pred))
print('RMSE train:', np.sqrt(metrics.mean_squared_error(yy, rfr_pred)))
 rfr_pred= model.predict(train)
 rfr_pred = rfr_pred.reshape(-1,1)
 R2_train = model.score(train, y_train)
 print("R² train: {}".format(round(R2_train,4)))
 print('MAE train:', metrics.mean_absolute_error(y_train, rfr_pred))
print('MSE train:', metrics.mean_squared_error(y_train, rfr_pred))
print('RMSE train:', np.sqrt(metrics.mean_squared_error(y_train, rfr_pred)) )
shops.category = shops.category.apply(lambda x: x if(x in category)else "etc" )<count_unique_values>
predict_v1 = model.predict(val)
 sub_v1 = pd.DataFrame()
sub_v1['Id'] = id_val
sub_v1['Predicted'] = predict_v1.tolist()
sub_v1.to_csv('soumission_1.csv', index = False, sep = ',', decimal = '.' )