Kaggle competition: Grupo Bimbo Inventory Demand
def add_trend_feature(arr, abs_values=False):

 idx = np.array(range(len(arr)))
 if abs_values:
arr = np.abs(arr)
 lr = LinearRegression()
lr.fit(idx.reshape(-1, 1), arr)
 return lr.coef_[0]
def extract_features_from_segment(x):

 features = {}
features['ave'] = x.values.mean()
features['std'] = x.values.std()
features['max'] = x.values.max()
features['min'] = x.values.min()
features['q90'] = np.quantile(x.values, 0.90)
 features['q95'] = np.quantile(x.values, 0.95)
 features['q99'] = np.quantile(x.values, 0.99)
 features['q05'] = np.quantile(x.values, 0.05)
 features['q10'] = np.quantile(x.values, 0.10)
 features['q01'] = np.quantile(x.values, 0.01)
 features['std_to_mean'] = features['std'] / features['ave']
features['abs_max'] = np.abs(x.values ).max()
features['abs_mean'] = np.abs(x.values ).mean()
features['abs_std'] = np.abs(x.values ).std()
features['trend'] = add_trend_feature(x.values)
 features['abs_trend'] = add_trend_feature(x.values, abs_values=True)
 for w in [10, 50, 100, 1000]:
x_roll_abs_mean = x.abs().rolling(w ).mean().dropna().values
x_roll_mean = x.rolling(w ).mean().dropna().values
x_roll_std = x.rolling(w ).std().dropna().values
x_roll_min = x.rolling(w ).min().dropna().values
x_roll_max = x.rolling(w ).max().dropna().values
features['ave_roll_std_' + str(w)] = x_roll_std.mean()
features['std_roll_std_' + str(w)] = x_roll_std.std()
features['max_roll_std_' + str(w)] = x_roll_std.max()
features['min_roll_std_' + str(w)] = x_roll_std.min()
features['q01_roll_std_' + str(w)] = np.quantile(x_roll_std, 0.01)
 features['q05_roll_std_' + str(w)] = np.quantile(x_roll_std, 0.05)
 features['q10_roll_std_' + str(w)] = np.quantile(x_roll_std, 0.10)
 features['q95_roll_std_' + str(w)] = np.quantile(x_roll_std, 0.95)
 features['q99_roll_std_' + str(w)] = np.quantile(x_roll_std, 0.99)
 features['ave_roll_mean_' + str(w)] = x_roll_mean.mean()
features['std_roll_mean_' + str(w)] = x_roll_mean.std()
features['max_roll_mean_' + str(w)] = x_roll_mean.max()
features['min_roll_mean_' + str(w)] = x_roll_mean.min()
features['q05_roll_mean_' + str(w)] = np.quantile(x_roll_mean, 0.05)
 features['q95_roll_mean_' + str(w)] = np.quantile(x_roll_mean, 0.95)
 features['ave_roll_abs_mean_' + str(w)] = x_roll_abs_mean.mean()
features['std_roll_abs_mean_' + str(w)] = x_roll_abs_mean.std()
features['q05_roll_abs_mean_' + str(w)] = np.quantile(x_roll_abs_mean, 0.05)
 features['q95_roll_abs_mean_' + str(w)] = np.quantile(x_roll_abs_mean, 0.95)
 features['std_roll_min_' + str(w)] = x_roll_min.std()
features['max_roll_min_' + str(w)] = x_roll_min.max()
features['q05_roll_min_' + str(w)] = np.quantile(x_roll_min, 0.05)
 features['q95_roll_min_' + str(w)] = np.quantile(x_roll_min, 0.95)
 features['std_roll_max_' + str(w)] = x_roll_max.std()
features['min_roll_max_' + str(w)] = x_roll_max.min()
features['q05_roll_max_' + str(w)] = np.quantile(x_roll_max, 0.05)
 features['q95_roll_max_' + str(w)] = np.quantile(x_roll_max, 0.95)
 return features<train_model>
def random_sampler(filename, k):
sample = []
with open(filename, 'rb')as f:
f.seek(0, 2)
 filesize = f.tell()
random_set = sorted(random.sample(range(filesize), k))
for i in range(k):
f.seek(random_set[i])
 f.readline()
sample.append(f.readline().rstrip())
 return sample
X_train, target, quake = make_train(train, skip=150000)
 print("Train shape:", X_train.shape)
 X_train.head(3 )<split>
TRAIN_SAMPLES = 5*10**6
train_sample = random_sampler('.. /input/train.csv', TRAIN_SAMPLES )
def fold_generator(x, y, groups=None, num_folds=10, shuffle=True, seed=2019):
folds = KFold(num_folds, shuffle=shuffle, random_state=seed)
 for train_index, test_index in folds.split(x, y, groups):
yield train_index, test_index<train_model>
train_sample[0].decode().split(',' )
rf = RandomForestRegressor(n_jobs=-1, max_depth=5)
 selector = BorutaPy(rf, n_estimators='auto', verbose=0,
random_state=1, max_iter=500)
 selector.fit(X_train.values, target.values)
 X_filtered = selector.transform(X_train.values)
 X_train = pd.DataFrame(X_filtered, columns=X_train.columns[selector.support_])
 X_train.head()<choose_model_class>
train_sample_ = [row.decode().split(",")for row in train_sample]
def make_pipeline(estimator):
pipeline = Pipeline([ 
('scaler', StandardScaler()), 
('model', estimator)
 ])
 return pipeline<train_on_grid>
train = pd.DataFrame(train_sample_ )
def search_cv(x, y, pipeline, grid, max_iter=None, num_folds=10, shuffle=True):

 t0 = time.time()
cv = fold_generator(x, y, num_folds=num_folds)
 if max_iter is None:
search = GridSearchCV(pipeline, grid, cv=cv,
scoring='neg_mean_absolute_error')
 else:
search = RandomizedSearchCV(pipeline, grid, n_iter=max_iter, cv=cv,
scoring='neg_mean_absolute_error')
 search.fit(x, y)
 t0 = time.time() - t0
print("Best CV score: {:.4f}, time: {:.1f}s".format(-search.best_score_, t0))
print(search.best_params_)
 return search.best_estimator_
def make_predictions(x, y, pipeline, num_folds=10, shuffle=True, test=None, plot=True):

 if test is not None:
sub_prediction = np.zeros(test.shape[0])
 oof_prediction = np.zeros(x.shape[0])
 for tr_idx, val_idx in fold_generator(x, y, num_folds=num_folds):
pipeline.fit(x.iloc[tr_idx], y.iloc[tr_idx])
 oof_prediction[val_idx] = pipeline.predict(x.iloc[val_idx])
 if test is not None:
sub_prediction += pipeline.predict(test)/ num_folds
if plot:
plot_predictions(y, oof_prediction)
 if test is None:
return oof_prediction
else:
return oof_prediction, sub_prediction<find_best_params>
del train_sample
del train_sample_
grid = {'model__alpha': np.concatenate([np.linspace(0.001, 1, 200),
np.linspace(1, 100, 500)])}
ridge_pipe = make_pipeline(Ridge(random_state=2019))
ridge_pipe = search_cv(X_train, target, ridge_pipe, grid)
 ridge_oof = make_predictions(X_train, target, ridge_pipe )<compute_test_metric>
train_df = pd.read_csv('.. /input/train.csv', nrows=1)
 train.columns = train_df.columns
ridge_oof[ridge_oof < 0] = 0
print("Mean error: {:.4f}".format(mean_absolute_error(target, ridge_oof)) )<find_best_params>
train = train.apply(pd.to_numeric )
grid = {'model__gamma': np.linspace(1e-8, 1, 100),
'model__alpha': np.linspace(1e-6, 2, 200)}
kr_pipe = make_pipeline(KernelRidge(kernel='rbf'))
kr_pipe = search_cv(X_train, target, kr_pipe, grid, max_iter=60)
 kr_oof = make_predictions(X_train, target, kr_pipe )<find_best_params>
train['Demanda_uni_equil'] = np.log1p(train['Demanda_uni_equil'] )
grid = {'model__epsilon': np.linspace(0.001, 1, 100),
'model__C': np.linspace(0.01, 10, 100)}
svm_pipe = make_pipeline(SVR(kernel='rbf', gamma='scale'))
svm_pipe = search_cv(X_train, target, svm_pipe, grid, max_iter=60)
 svm_oof = make_predictions(X_train, target, svm_pipe )<compute_train_metric>
x_cols = train.columns
x_cols = x_cols.drop(['Venta_uni_hoy', 'Venta_hoy', 'Dev_uni_proxima', 'Dev_proxima', 'Demanda_uni_equil'])
 print(x_cols )
grid = {
'model__max_depth': [4, 6, 8, 10, 12],
'model__max_features': ['auto', 'sqrt', 'log2'],
'model__min_samples_leaf': [2, 4, 8, 12, 14, 16, 20],
'model__min_samples_split': [2, 4, 6, 8, 12, 16, 20],
}
rf_pipe = make_pipeline(RandomForestRegressor(criterion='mae', n_estimators=40))
rf_pipe = search_cv(X_train, target, rf_pipe, grid, max_iter=10)
 rf_oof = make_predictions(X_train, target, rf_pipe )<find_best_params>
model = RandomForestRegressor()
grid = {'model__learning_rate': np.linspace(1e-5, 0.9, 100)}
base = Ridge(alpha=2)
 ada_pipe = make_pipeline(AdaBoostRegressor(base_estimator=base, n_estimators=200))
ada_pipe = search_cv(X_train, target, ada_pipe, grid)
 ada_oof = make_predictions(X_train, target, ada_pipe )<define_search_space>
model.fit(train[x_cols], train['Demanda_uni_equil'] )
fixed_params = {
'objective': 'regression_l1',
'boosting': 'gbdt',
'verbosity': -1,
'random_seed': 19,
'n_estimators': 80000,
'max_depth': -1
}
param_grid = {
'learning_rate': [0.1, 0.05, 0.01, 0.005],
'num_leaves': list(range(2, 60, 2)) ,
'feature_fraction': [0.8, 0.85, 0.9, 0.95, 1],
'subsample': [0.8, 0.9, 0.95, 1],
'lambda_l1': [0, 0.2, 0.4, 0.6, 0.8, 2, 5],
'lambda_l2': [0, 0.2, 0.4, 0.6, 0.8, 2, 5],
'min_data_in_leaf': [10, 20, 40, 60, 100],
'min_gain_to_split': [0, 0.001, 0.01, 0.1],
}
best_score = 999
dataset = lgb.Dataset(X_train, label=target)
 for i in range(600):
params = {k: random.choice(v)for k, v in param_grid.items() }
params.update(fixed_params)
 result = lgb.cv(params, dataset, nfold=10, early_stopping_rounds=100,
stratified=False)
 if result['l1-mean'][-1] < best_score:
best_score = result['l1-mean'][-1]
best_params = params
best_nrounds = len(result['l1-mean'] )<train_model>
test = pd.read_csv('.. /input/test.csv')
 test.info()
print("Best mean score: {:.4f}, num rounds: {}".format(best_score, best_nrounds))
print(best_params)
 gb_pipe = make_pipeline(lgb.LGBMRegressor(**best_params))
gb_oof = make_predictions(X_train, target, gb_pipe )<save_to_csv>
test['Demanda_uni_equil'] = np.expm1(model.predict(test[x_cols]))
<set_options><EOS> 
test[['id', 'Demanda_uni_equil']].to_csv('predictions_rf_random_sampling.csv', index=False )