Kaggle competition: Rossmann Store Sales
params = model.get_params()<init_hyperparams>
store = pd.read_csv(".. /input/store.csv")
 train = pd.read_csv(".. /input/train.csv",parse_dates=[2])
 test = pd.read_csv(".. /input/test.csv",parse_dates=[3] )
params['objective'] = 'binary'
params['metric'] = 'auc'<train_model>
store.isnull().sum()
skf = StratifiedKFold(n_splits=3, random_state=42, shuffle=True)
 final_importance = np.zeros(len(train_data.columns))
for n_fold,(train_index, valid_index)in tqdm(enumerate(skf.split(train_data, train_target.TARGET))):
X_train = train_data.iloc[train_index]
y_train = train_target.iloc[train_index].TARGET
X_valid = train_data.iloc[valid_index]
y_valid = train_target.iloc[valid_index].TARGET
lgb_train = lgb.Dataset(data=X_train, label=y_train)
 lgb_eval = lgb.Dataset(data=X_valid, label=y_valid)
 model = lgb.train(params, lgb_train, valid_sets=lgb_eval, early_stopping_rounds=150, verbose_eval=100)
 final_importance += model.feature_importance()<create_dataframe>
store.PromoInterval.value_counts()
fi = pd.DataFrame()
fi['FEAT'] = train_data.columns<feature_engineering>
store.fillna(0, inplace=True )
fi['importance'] = final_importance<sort_values>
train.head().append(train.tail() )
fi = fi.sort_values(by='importance', ascending=False )<filter>
train.isnull().sum()
fi = fi[fi.importance != 0]<merge>
train.isnull().sum()
cols = list(set(fi.FEAT.values ).union(set(['SK_ID_CURR'])) )<create_dataframe>
train.Open.value_counts()
train_data = train_data[cols]<create_dataframe>
np.sum([train['Sales'] == 0] )
test_data = test_data[cols]<define_search_space>
test.isnull().sum()
def get_random_params() :
params = {
'boosting_type': 'gbdt',
'metric': 'auc',
'num_leaves': random.randint(10, 60),
'max_depth': random.randint(10, 30),
'learning_rate': random.choice([0.0001, 0.0005, 0.001, 0.005, 0.01]),
'n_estimators': random.randint(1000, 20000),
'objective': 'binary',
'reg_alpha': random.choice([0.001, 0.005, 0.01, 0.05, 0.1]),
'reg_lambda': random.choice([0.001, 0.005, 0.01, 0.05, 0.1]),
'colsample_bytree': random.choice([0.4, 0.5, 0.6, 0.7, 0.8, 0.9]),
'min_child_samples': random.randint(10, 100),
'subsample_for_bin': random.randint(50000, 300000)
 }
return params<init_hyperparams>
test.isnull().sum()
best_params = {'boosting_type': 'gbdt',
'metric': 'auc',
'num_leaves': 46,
'max_depth': 18,
'learning_rate': 0.01,
'n_estimators': 6289,
'objective': 'binary',
'reg_alpha': 0.05,
'reg_lambda': 0.05,
'colsample_bytree': 0.4,
'min_child_samples': 79,
'subsample_for_bin': 113092}
best_auc = 0.787228<train_on_grid>
test[test.Open.isnull() ]
def get_best_params(hyper_rounds, n_folds, best_params=None, best_auc=0):
best_params = best_params
best_auc = best_auc
lgb_train = lgb.Dataset(data=train_data, label=train_target.TARGET)
 for i in tqdm(range(hyper_rounds)) :
curr_params = get_random_params()
start = time.time()
print(curr_params)
 eval_hist = lgb.cv(curr_params, lgb_train, early_stopping_rounds = 200, nfold = n_folds, seed = 42, verbose_eval = 100)
 end = time.time()
print('TIME:', end-start)
 curr_auc = eval_hist['auc-mean'][-1]
if curr_auc > best_auc:
best_params = curr_params
best_auc = curr_auc
return best_params, best_auc<define_variables>
test.fillna(value = 1, inplace = True )
N_FOLDS = 10<split>
train = pd.merge(train, store, on='Store')
 test = pd.merge(test, store, on='Store')
 train = train.sort_values(['Date'],ascending = False)
 train_total = train.copy()
split_index = 6*7*1115
valid = train[:split_index]
train = train[split_index:]
valid = valid[(valid.Open != 0)&(valid.Sales >0)]
train = train[(train.Open != 0)&(train.Sales >0)]
train_total = train_total[(train_total.Open != 0)&(train_total.Sales >0)]
skf = StratifiedKFold(n_splits=N_FOLDS, random_state=42, shuffle=True)
 sub_preds = np.zeros(len(test_data))
avg_valid_auc = 0
for n_fold,(train_index, valid_index)in tqdm(enumerate(skf.split(train_data, train_target.TARGET))):
print("FOLD N:", n_fold)
 X_train = train_data.iloc[train_index]
y_train = train_target.iloc[train_index].TARGET
X_valid = train_data.iloc[valid_index]
y_valid = train_target.iloc[valid_index].TARGET
lgb_train = lgb.Dataset(data=X_train, label=y_train)
 lgb_eval = lgb.Dataset(data=X_valid, label=y_valid)
 model = lgb.train(best_params, lgb_train, valid_sets=lgb_eval, early_stopping_rounds=150, verbose_eval=100)
 y_pred = model.predict(X_valid)
 sub_preds += model.predict(test_data)/ skf.n_splits
avg_valid_auc += roc_auc_score(y_valid, y_pred)/ N_FOLDS<prepare_output>
def process(data, isTest = False):
mappings = {'0':0, 'a':1, 'b':2, 'c':3, 'd':4}
data.StoreType.replace(mappings, inplace=True)
 data.Assortment.replace(mappings, inplace=True)
 data.StateHoliday.replace(mappings, inplace=True)
 data['Month'] = data.Date.dt.month
data['Year'] = data.Date.dt.year
data['Day'] = data.Date.dt.day
data['WeekOfYear'] = data.Date.dt.weekofyear
data['CompetitionOpen'] = 12 *(data.Year - data.CompetitionOpenSinceYear)+ \ 
(data.Month - data.CompetitionOpenSinceMonth)
 data['CompetitionOpen'] = data['CompetitionOpen'].apply(lambda x: x if x > 0 else 0)
 data['PromoOpen'] = 12 *(data.Year - data.Promo2SinceYear)+ \ 
(data.WeekOfYear - data.Promo2SinceWeek)/ 4.0
data['PromoOpen'] = data['PromoOpen'].apply(lambda x: x if x > 0 else 0)
 month2str = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', \
7:'Jul', 8:'Aug', 9:'Sept', 10:'Oct', 11:'Nov', 12:'Dec'}
data['month_str'] = data.Month.map(month2str)
 def check(row):
if isinstance(row['PromoInterval'],str)and row['month_str'] in row['PromoInterval']:
return 1
else:
return 0
data['IsPromoMonth'] = data.apply(lambda row: check(row),axis=1)
 features = ['Store', 'DayOfWeek', 'Promo', 'StateHoliday', 'SchoolHoliday',
'StoreType', 'Assortment', 'CompetitionDistance',
'CompetitionOpenSinceMonth', 'CompetitionOpenSinceYear', 'Promo2',
'Promo2SinceWeek', 'Promo2SinceYear', 'Year', 'Month', 'Day',
'WeekOfYear', 'CompetitionOpen', 'PromoOpen', 'IsPromoMonth']
if not isTest:
features.append('Sales')
 data = data[features]
return data
train = process(train)
 valid = process(valid)
 train_total = process(train_total)
 x_test = process(test,isTest = True )
submit['TARGET'] = sub_preds<save_to_csv>
valid.sort_index(inplace = True)
 train.sort_index(inplace = True)
 train_total.sort_index(inplace = True)
 x_train, y_train = train.drop(columns = ['Sales']), np.log1p(train['Sales'])
 x_valid, y_valid = valid.drop(columns = ['Sales']), np.log1p(valid['Sales'])
 x_train_total, y_train_total = train_total.drop(columns = ['Sales']), np.log1p(train_total['Sales'] )
submit.to_csv('submission.csv', index = False )<set_options>
def rmspe(y, yhat):
return np.sqrt(np.mean(( yhat/y-1)** 2))
def rmspe_xg(yhat, y):
y = np.expm1(y.get_label())
 yhat = np.expm1(yhat)
 return "rmspe", rmspe(y,yhat )
np.random.seed(98643)
 tf.set_random_seed(683)
 denoise_wavelet, estimate_sigma, denoise_tv_bregman, denoise_nl_means)
 %matplotlib inline 
<data_type_conversions>
clf = RandomForestRegressor(n_estimators = 15)
 clf.fit(x_train, y_train)
 y_pred = clf.predict(x_valid)
 error = rmspe(np.expm1(y_valid), np.expm1(y_pred))
print('RMSPE: {:.4f}'.format(error))
train = pd.read_json(".. /input/train.json")
 train.inc_angle = train.inc_angle.replace('na', 0)
 train.inc_angle = train.inc_angle.astype(float ).fillna(0.0)
 train_all = True
train_b = True or train_all
train_img = True or train_all
train_total = True or train_all
predict_submission = True and train_all
clean_all = True
clean_b = True or clean_all
clean_img = True or clean_all
load_all = False
load_b = False or load_all
load_img = False or load_all<create_dataframe>
params = {"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.03,
"max_depth": 10,
"subsample": 0.9,
"colsample_bytree": 0.7,
"silent": 1,
"seed": 10
}
num_boost_round = 4000
dtrain = xgb.DMatrix(x_train, y_train)
 dvalid = xgb.DMatrix(x_valid, y_valid)
 watchlist = [(dtrain, 'train'),(dvalid, 'eval')]
model = xgb.train(params, dtrain, num_boost_round, evals=watchlist, \
early_stopping_rounds= 100, feval=rmspe_xg, verbose_eval=True )
def create_dataset(frame, labeled, smooth_rgb=0.2, smooth_gray=0.5,
weight_rgb=0.05, weight_gray=0.05):
band_1, band_2, images = frame['band_1'].values, frame['band_2'].values, color_composite(frame)
 to_arr = lambda x: np.asarray([np.asarray(item)for item in x])
 band_1 = to_arr(band_1)
 band_2 = to_arr(band_2)
 band_3 =(band_1 + band_2)/ 2
gray_reshape = lambda x: np.asarray([item.reshape(75, 75)for item in x])
 band_1 = gray_reshape(band_1)
 band_2 = gray_reshape(band_2)
 band_3 = gray_reshape(band_3)
 print('Denoising and reshaping')
 if train_b and clean_b:
band_1 = smooth(denoise(band_1, weight_gray, False), smooth_gray)
 print('Gray 1 done')
 band_2 = smooth(denoise(band_2, weight_gray, False), smooth_gray)
 print('Gray 2 done')
 band_3 = smooth(denoise(band_3, weight_gray, False), smooth_gray)
 print('Gray 3 done')
 if train_img and clean_img:
images = smooth(denoise(images, weight_rgb, True), smooth_rgb)
 print('RGB done')
 tf_reshape = lambda x: np.asarray([item.reshape(75, 75, 1)for item in x])
 band_1 = tf_reshape(band_1)
 band_2 = tf_reshape(band_2)
 band_3 = tf_reshape(band_3)
 band = np.concatenate([band_1, band_2, band_3], axis=3)
 X_angle = np.array(frame.inc_angle)
 if labeled:
y = np.array(frame["is_iceberg"])
 else:
y = None
return y, X_angle, band, images<prepare_x_and_y>
y_pred = model.predict(xgb.DMatrix(x_valid))
error = rmspe(np.expm1(y_valid), np.expm1(y_pred))
print('RMSPE: {:.4f}'.format(error))
y_train, X_angles, X_b, X_images = create_dataset(train, True )<choose_model_class>
x_train_total.head().append(x_train_total.tail() )
def get_model_notebook(angle, lr, decay, channels, relu_type='relu'):
input_1 = Input(shape=(75, 75, channels))
input_2 = Input(shape=[1])
 fcnn = Conv2D(32, kernel_size=(3, 3), activation=relu_type )(input_1)
 fcnn = MaxPooling2D(( 3, 3))(fcnn)
 fcnn = Dropout(0.2 )(fcnn)
 fcnn = Conv2D(64, kernel_size=(3, 3), activation=relu_type )(fcnn)
 fcnn = MaxPooling2D(( 2, 2), strides=(2, 2))(fcnn)
 fcnn = Dropout(0.2 )(fcnn)
 fcnn = Conv2D(128, kernel_size=(3, 3), activation=relu_type )(fcnn)
 fcnn = MaxPooling2D(( 2, 2), strides=(2, 2))(fcnn)
 fcnn = Dropout(0.2 )(fcnn)
 fcnn = Conv2D(128, kernel_size=(3, 3), activation=relu_type )(fcnn)
 fcnn = MaxPooling2D(( 2, 2), strides=(2, 2))(fcnn)
 fcnn = Dropout(0.2 )(fcnn)
 fcnn = Flatten()(fcnn)
 if angle:
local_input = [input_1, input_2]
else:
local_input = input_1
dense = Dropout(0.2 )(fcnn)
 dense = Dense(256, activation=relu_type )(dense)
 partial_model = Model(input_1, fcnn)
 dense = Dropout(0.2 )(dense)
 dense = Dense(128, activation=relu_type )(dense)
 dense = Dropout(0.2 )(dense)
 dense = Dense(64, activation=relu_type )(dense)
 dense = Dropout(0.2 )(dense)
 if angle:
dense = Concatenate()([dense, input_2])
 else:
dense = dense
output = Dense(1, activation="sigmoid" )(dense)
 model = Model(local_input, output)
 optimizer = Adam(lr=lr, decay=decay)
 model.compile(loss="binary_crossentropy", optimizer=optimizer, metrics=["accuracy"])
 return model, partial_model 
<choose_model_class>
dtrain = xgb.DMatrix(x_train_total, y_train_total)
 dtest = xgb.DMatrix(x_test)
 params = {"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.03,
"max_depth": 10,
"subsample": 0.9,
"colsample_bytree": 0.7,
"silent": 1,
"seed": 10
}
num_round = 3000
model = xgb.train(params, dtrain, num_round)
 preds = model.predict(dtest )
<train_model><EOS> 
result = pd.DataFrame({"Id": test["Id"],'Sales': np.expm1(preds*0.995)})
 result.to_csv("submission_xgb.csv", index=False)
 fig, ax = plt.subplots(figsize=(8,8))
xgb.plot_importance(model, max_num_features= 10, height=0.5, ax=ax)
 plt.show()