Kaggle competition: Restaurant Revenue Prediction
print(test_X.shape, test_features.shape)
 print(train_X.shape)
 test_preds = final_preds(models, test_X)
 ensemble_preds = sum(test_preds)/ len(test_preds)
 total_test_preds = test_preds + [ensemble_preds] 
<predict_on_test>
pd.set_option('display.max_columns', None)
 warnings.filterwarnings(action="ignore" )
test_preds = create_final_prediction(total_test_preds, target_model )<create_dataframe>
train = pd.read_csv('.. /input/restaurant-revenue-prediction/train.csv.zip')
 test = pd.read_csv('.. /input/restaurant-revenue-prediction/test.csv.zip')
 train.shape, test.shape
print(test_preds.shape)
 predictions = pd.concat([test_features['sig_id'],
pd.DataFrame(test_preds, columns=train_targets_scored.columns[1:])],
axis=1)
 print(predictions.shape )<save_to_csv>
combine = [train, test]
for dataset in combine:
dataset["Open Date"] = pd.to_datetime(dataset["Open Date"])
 dataset["Standard_date"] = "2015-04-27"
dataset["Standard_date"] = pd.to_datetime(dataset["Standard_date"])
 dataset["Business_Period"] =(dataset["Standard_date"]-dataset["Open Date"] ).apply(lambda x: x.days )
predictions.to_csv("submission.csv", index=False )<import_modules>
train["Type"] = train["Type"].map({"IL":0,"FC":1,"DT":2} ).astype(int)
 test["Type"] = test["Type"].map({"IL":0,"FC":1,"DT":2,"MB":3} ).astype(int)
 train["City Group"] = train["City Group"].map({"Big Cities":0, "Other":1} ).astype(int)
 test["City Group"] = test["City Group"].map({"Big Cities":0, "Other":1} ).astype(int)
import xgboost as xgb
import lightgbm as lgb
from catboost import CatBoostRegressor
from hyperopt import Trials, STATUS_OK, tpe, hp, fmin<compute_train_metric>
train["City"] = train["City"].map({'İstanbul':0, 'Ankara':1, 'Diyarbakır':2, 'Tokat':3, 'Gaziantep':4,
'Afyonkarahisar':5, 'Edirne':6, 'Kocaeli':7, 'Bursa':8, 'İzmir':9, 'Sakarya':10,
'Elazığ':11, 'Kayseri':12, 'Eskişehir':13, 'Şanlıurfa':14, 'Samsun':15, 'Adana':16,
'Antalya':17, 'Kastamonu':18, 'Uşak':19, 'Muğla':20, 'Kırklareli':21, 'Konya':22,
'Karabük':23, 'Tekirdağ':24, 'Denizli':25, 'Balıkesir':26, 'Aydın':27, 'Amasya':28,
'Kütahya':29, 'Bolu':30, 'Trabzon':31, 'Isparta':32, 'Osmaniye':33, 'Niğde':34,
'Rize':35, 'Düzce':36, 'Hatay':37, 'Erzurum':38, 'Mersin':39, 'Zonguldak':40,
'Malatya':41, 'Çanakkale':42, 'Kars':43, 'Batman':44, 'Bilecik':45, 'Giresun':46,
'Sivas':47, 'Kırıkkale':48, 'Mardin':49, 'Erzincan':50, 'Manisa':51,
'Kahramanmaraş':52, 'Yalova':53, 'Tanımsız':54, 'Kırşehir':55, 'Aksaray':56,
'Nevşehir':57, 'Çorum':58, 'Ordu':59, 'Artvin':60, 'Siirt':61, 'Çankırı':62} ).astype(int)
 test["City"] = test["City"].map({'İstanbul':0, 'Ankara':1, 'Diyarbakır':2, 'Tokat':3, 'Gaziantep':4,
'Afyonkarahisar':5, 'Edirne':6, 'Kocaeli':7, 'Bursa':8, 'İzmir':9, 'Sakarya':10,
'Elazığ':11, 'Kayseri':12, 'Eskişehir':13, 'Şanlıurfa':14, 'Samsun':15, 'Adana':16,
'Antalya':17, 'Kastamonu':18, 'Uşak':19, 'Muğla':20, 'Kırklareli':21, 'Konya':22,
'Karabük':23, 'Tekirdağ':24, 'Denizli':25, 'Balıkesir':26, 'Aydın':27, 'Amasya':28,
'Kütahya':29, 'Bolu':30, 'Trabzon':31, 'Isparta':32, 'Osmaniye':33, 'Niğde':34,
'Rize':35, 'Düzce':36, 'Hatay':37, 'Erzurum':38, 'Mersin':39, 'Zonguldak':40,
'Malatya':41, 'Çanakkale':42, 'Kars':43, 'Batman':44, 'Bilecik':45, 'Giresun':46,
'Sivas':47, 'Kırıkkale':48, 'Mardin':49, 'Erzincan':50, 'Manisa':51,
'Kahramanmaraş':52, 'Yalova':53, 'Tanımsız':54, 'Kırşehir':55, 'Aksaray':56,
'Nevşehir':57, 'Çorum':58, 'Ordu':59, 'Artvin':60, 'Siirt':61, 'Çankırı':62} ).astype(int )
class ObjectiveFunction() :
def __init__(self, clf, X, y, cv=5, scoring=None):
self.X = X
self.y = y
self.cv = cv
self.scoring = scoring
self.clf = clf
def change_model(self, clf):
self.clf = clf
return self
def __call__(self, space):
clf = self.clf(**space)
 scores = cross_val_score(clf, self.X, self.y, cv=self.cv, scoring=self.scoring)
 return sum(scores)/ len(scores)
 def train_classifiers(clf, X_train, y_train, space, clf_params):
predictions = []
predictors = []
X_train,X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.3, shuffle=True)
 num_targets = y_train.shape[-1]
for target in range(num_targets):
print("target :{}".format(target))
fit_labels = y_train[:, target]
predictor = clf(**clf_params)
 predictor.fit(X_train, y=fit_labels, eval_set =(X_val, y_val[:, target]))
predictors.append(predictor)
 preds = predictor.predict(X_val)
 loss = LoglossMetric().evaluate([preds], y_val[:, target])[0] / len(preds)
 print("loss:", loss)
 predictions.append(loss)
 return predictors, predictions 
<define_search_space>
train["revenue"] = train.revenue.apply(lambda x:np.log1p(x))
space_gbm = {
'max_depth' : hp.choice('max_depth', range(5, 30, 1)) ,
'learning_rate' : hp.uniform('learning_rate', 0.01, 1),
'n_estimators' : hp.choice('n_estimators', range(20, 200, 1)) ,
'gamma' : hp.uniform('gamma', 0, 1),
'min_child_weight' : hp.uniform('min_child_weight', 1, 10),
'subsample' : hp.uniform('subsample', 0.1, 1),
'colsample_bytree' : hp.quniform('colsample_bytree', 0.1, 1.0, 0.01),
'reg_alpha' : hp.uniform('aplha', 0, 1),
'reg_lambda' : hp.uniform('lambda', 0, 1)}
space_xgb = {
'max_depth' : hp.choice('max_depth', range(2, 30, 1)) ,
'learning_rate' : hp.uniform('learning_rate', 0.01, 1),
'n_estimators' : hp.choice('n_estimators', range(20, 200, 1)) ,
'gamma' : hp.uniform('gamma', 0, 1),
'min_child_weight' : hp.uniform('min_child_weight', 0, 10),
'subsample' : hp.uniform('subsample', 0.01, 1),
'lambda': hp.uniform('lambda', 0.00, 10)} 
<compute_test_metric>
train = train.drop(["Id"], axis=1)
 train = train.drop(["Open Date"], axis=1)
 train = train.drop(["Standard_date"], axis=1)
 test = test.drop(["Id"], axis=1)
 test = test.drop(["Open Date"], axis=1)
 test = test.drop(["Standard_date"], axis=1 )
class LoglossObjective(object):
def calc_ders_range(self, approxes, targets, weights=None):
assert len(approxes)== len(targets)
 if weights is not None:
assert len(weights)== len(approxes)
 result = []
for index in range(len(targets)) :
e = np.exp(approxes[index])
 p = e /(1 + e)
 der1 = targets[index] - p
der2 = -p *(1 - p)
 if weights is not None:
der1 *= weights[index]
der2 *= weights[index]
result.append(( der1, der2))
return result
class LoglossMetric(object):
def get_final_error(self, error, weight):
return error /(weight + 1e-38)
 def is_max_optimal(self):
return False
def evaluate(self, approxes, target, weight=None):
assert len(approxes)== 1
assert len(target)== len(approxes[0])
 approx = approxes[0]
error_sum = 0.0
weight_sum = 0.0
for i in range(len(approx)) :
e = np.exp(approx[i])
 p = e /(1 + e)
 w = 1.0 if weight is None else weight[i]
weight_sum += w
error_sum += -w *(target[i] * np.log(p)+(1 - target[i])* np.log(1 - p))
return error_sum, weight_sum<predict_on_test>
y = train["revenue"]
train = train.drop(["revenue"], axis=1 )
def predict_predictors(predictors, test_X):
for target_predictor in predictor:
preds = target_predictor.predict(test_X)
 predictions.append(preds)
 return np.asarray(predictions)
<compute_test_metric>
X_train, X_test, y_train, y_test = train_test_split(
 train, y, random_state=0, train_size=0.7,shuffle=False )
def compute_score(y_pred, y_true):
assert y_pred.shape == y_true.shape
score = 0
scores = []
for i in range(PARAMS["TARGETS_OUTPUT"]):
score_ = log_loss(y_true[:, i], y_pred[:, i])
 print("target {}:{}".format(i, score_))
score += score_
scores.append(score_)
 return score / PARAMS["TARGETS_OUTPUT"], scores<choose_model_class>
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
 params = {
'task' : 'train',
'boosting_type' : 'gbdt',
'objective' : 'regression',
'metric' : {'l2'},
'num_leaves' : 31,
'learning_rate' : 0.1,
'feature_fraction' : 0.9,
'bagging_fraction' : 0.8,
'bagging_freq': 5,
'verbose' : 0,
'n_jobs': 2
}
gbm = lgb.train(params,
lgb_train,
num_boost_round=100,
valid_sets=lgb_eval,
early_stopping_rounds=10)
 prediction_lgb = np.exp(gbm.predict(test))
def choose_model(neural_nets_scores, catboost_predictors_scores):
target_model = {}
for target in range(206):
neural_scores = [neural_nets_scores[i][target] for i in range(len(neural_nets_scores)) ]
neural_min = min(neural_scores)
 argmin_neural = neural_scors.index(neural_min)
 if neural_min < cat_boost_predictors[target]:
target_model[target] = argmin_neural
else:
target_model[target] = -1
return target_model 
<set_options>
sample =pd.read_csv('/kaggle/input/restaurant-revenue-prediction/sampleSubmission.csv' )
warnings.filterwarnings('ignore' )<define_variables>
sample["Prediction"] = prediction_lgb
TRAINING = False<install_modules>
sample.to_csv('.. /working/submission.csv', index = False )