Kaggle competition: Restaurant Revenue Prediction
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random
import time
import tensorflow as tf
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
import tensorflow.keras.backend as K
import tensorflow.keras.backend as B
import tensorflow_addons as tfa
from keras.models import Model
from sklearn.decomposition import PCA
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import log_loss
from sklearn.utils import shuffle
from sklearn.mixture import BayesianGaussianMixture
from sklearn.cluster import KMeans<load_from_csv>
df = pd.read_csv('/kaggle/input/restaurant-revenue-prediction/train.csv.zip')
 df.shape
data_folder = '.. /input/lish-moa/'
train=pd.read_csv(data_folder + '/train_features.csv')
 test=pd.read_csv(data_folder + '/test_features.csv')
 targets=pd.read_csv(data_folder + '/train_targets_scored.csv')
 targets_ns = pd.read_csv(data_folder + '/train_targets_nonscored.csv')
 drug_table = pd.read_csv(data_folder + '/train_drug.csv')
 train.head()<feature_engineering>
test_data = pd.read_csv('/kaggle/input/restaurant-revenue-prediction/test.csv.zip')
 test_data.shape
print('Train rows:', len(train.index))
print('Test rows:', len(test.index))
tag = 'tag'
train[tag] = 'train'
test[tag] = 'test'
total=pd.concat([train, test])
 print('Total rows:', len(total.index))<define_variables>
df.sum().isnull()
columns = train.columns.to_list()
c_columns = [col for col in columns if col.startswith('c-')]
print('Number of c_cols:', len(c_columns))
g_columns = [col for col in columns if col.startswith('g-')]
print('Number of g_cols:', len(g_columns))<filter>
test_data.loc[test_data['Type']=='MB', 'Type'] = 'DT'
vehicles=total.loc[total[type_col]=='ctl_vehicle']
print('Total vehicles:',len(vehicles.index))<sort_values>
df['Open Date'] = pd.to_datetime(df['Open Date'])
 test_data['Open Date'] = pd.to_datetime(test_data['Open Date'] )
targets_agg = targets.agg([sum])
 targets_agg.drop(['sig_id'], axis=1,inplace=True)
 target_col = targets_agg.columns
np.sort(targets_agg.values)
 zero_targets = targets_agg[targets_agg.columns[(targets_agg<10 ).any() ]]
zero_targets<groupby>
df.drop('Id',axis=1,inplace=True)
 df.drop('City', axis=1, inplace = True)
 test_data.drop('City', axis = 1, inplace = True)
 df.head()
drug_id = 'drug_id'
drug_targets = pd.concat([drug_table[drug_id], targets], axis = 1)
 grouped = drug_targets.groupby(drug_id)
 drug_examples = grouped[drug_id].count()
train_ids = train[id_col]
train_nums = grouped[drug_id].transform('count' )<categorify>
launch_date = datetime.datetime(2015, 3, 23)
 df['Days Open'] =(launch_date - df['Open Date'] ).dt.days / 1000
test_data['Days Open'] =(launch_date - test_data['Open Date'] ).dt.days / 1000
df.drop('Open Date', axis=1, inplace=True)
 test_data.drop('Open Date', axis=1, inplace=True )
total2 = total.copy()
targets2 = targets.copy()
scored_columns = targets2.columns
scored_columns = scored_columns.drop('sig_id')
 num_scored = len(scored_columns)
 targets2 = pd.concat([targets2, targets_ns], axis = 1)
 total2[dose_col]=total2[dose_col].map({'D1':0, 'D2':1})
 def onehot_time(x):
return [(x==24 ).astype(int),(x==48 ).astype(int),(x==72 ).astype(int)]
total2['t1'], total2['t2'], total2['t3'] = onehot_time(total2[time_col])
 total2.drop([time_col], axis=1, inplace=True)
 total2.drop([tag], axis=1, inplace=True)
 vehicles_mask = total2[type_col] == 'ctl_vehicle'
n_train = len(targets2.index)
 vehicles = total2[vehicles_mask]
total2 = total2[~vehicles_mask]
targets2 = targets2[~vehicles_mask[:n_train]]
train_nums2 = train_nums[~vehicles_mask[:n_train]]
train_nums2 = np.array(train_nums2.to_list())
 total2.drop([type_col], axis=1, inplace=True)
 vehicles.drop([type_col], axis=1, inplace=True)
 def rm_index(df):
df.reset_index(drop=True, inplace=True)
 df.drop([id_col], axis=1, inplace=True)
 rm_index(total2)
 rm_index(targets2)
 rm_index(vehicles)
 total2.head()
targets2.head()<normalization>
columnsToEncode = df.select_dtypes(include=[object] ).columns
df = pd.get_dummies(df, columns=columnsToEncode, drop_first=False)
 test_data = pd.get_dummies(test_data, columns=columnsToEncode, drop_first=False )
def scale_total(total2, scaler = None):
if scaler is None:
scaler = MinMaxScaler()
scaler.fit(total2)
 total2 = pd.DataFrame(scaler.transform(total2))
return total2, scaler<normalization>
df['revenue'] = np.log1p(df['revenue'])
 X, y = df.drop('revenue', axis=1), df['revenue']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42 )
total_scaled, scaler = scale_total(total2)
 vehicles_scaled, _ = scale_total(vehicles, scaler)
 total_scaled.head()<define_variables>
clf = LinearRegression().fit(X_train, y_train)
 clf.score(X_train, y_train )
seed = None
def set_seed(s):
tf.random.set_seed(seed)
 return s
seed = set_seed(42 )<compute_train_metric>
params_grid = {
'max_depth': [10, 30, 35, 50, 65, 75, 100],
'max_features': [.3,.4,.5,.6],
'min_samples_leaf': [3, 4, 5],
'min_samples_split': [8, 10, 12],
'n_estimators': [30, 50, 100, 200]
}
rf_regressor = RandomForestRegressor()
rf_cv_regressor = GridSearchCV(rf_regressor, params_grid, scoring='neg_root_mean_squared_error', cv = 10, n_jobs = -1)
 rf_cv_regressor.fit(X_train, y_train)
 print('Best params {}'.format(rf_cv_regressor.best_params_))
print('Best score {}'.format(rf_cv_regressor.best_score_))
class CorrLoss(tf.keras.losses.Loss):
def __init__(self, mu, sigma, moving_mean, moving_variance, *args, **kwargs):
super(CorrLoss, self ).__init__(*args, **kwargs)
 self.mu = mu
self.b = sigma * moving_variance
self.moving_mean = moving_mean
def __call__(self, y_true, y_pred, *args, **kwargs):
a = tf.reduce_mean(( y_true - self.mu)*(y_pred - self.moving_mean), axis = 0)
 result = tf.math.divide(a, self.b)
 result = tf.math.abs(1 - result)
 return tf.reduce_mean(result)
 def zero_loss(y_true, y_pred):
return 0
def create_corr_model(num_inputs, num_outputs, mu, sigma):
in_layer = L.Input(num_inputs)
 base_layer = in_layer
hidden = L.Dense(1024, use_bias = False )(base_layer)
 hidden = L.BatchNormalization()(hidden)
 hidden = L.Activation('tanh' )(hidden)
 out_layer = L.Dense(num_outputs, name='out', use_bias = False )(hidden)
 hidden = L.Dense(1024, use_bias = False )(base_layer)
 hidden = L.BatchNormalization()(hidden)
 hidden = L.Activation('tanh' )(hidden)
 out_layer += L.Dense(num_outputs, name='out1', use_bias = False )(hidden)
 out_layer = L.Activation('sigmoid', name='final' )(out_layer)
 bn = L.BatchNormalization(name = 'bn')
 out2 = bn(out_layer)
 moving_mean = bn.variables[2]
moving_variance = bn.variables[3]
model = Model(in_layer, [out_layer, out2])
 model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
loss = [CorrLoss(mu, sigma, moving_mean, moving_variance), zero_loss])
 return model<train_model>
rf_regressor = RandomForestRegressor(max_depth = 10,
max_features = 0.6,
min_samples_leaf = 3,
min_samples_split = 8,
n_estimators = 50)
 rf_regressor.fit(X_train, y_train )
def do_train(X, y):
num_inputs = X.shape[1]
num_outputs = y.shape[1]
mu = np.mean(y, axis = 0)
 sigma = np.std(y, axis = 0)
 id_range = np.arange(X.shape[0])
 train_ids, val_ids= train_test_split(id_range, test_size=0.4, random_state=seed)
 checkpoint_filepath = 'corr_checkpoint.h5'
corr_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
 filepath=checkpoint_filepath,
metric = 'val_final_loss',
save_weights_only=True,
save_best_only=True)
 model = create_corr_model(num_inputs, num_outputs, mu, sigma)
 return model,(train_ids, val_ids )<data_type_conversions>
y_train_pred = rf_regressor.predict(X_train)
 y_pred = rf_regressor.predict(X_test)
 train_rmse = np.sqrt(mean_squared_error(y_train_pred, y_train))
test_rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print(f'Train RMSE: {train_rmse:.4f}')
 print(f'Test RMSE: {test_rmse:.4f}' )
<normalization><EOS> 
submission = pd.DataFrame(columns=['Id','Prediction'])
 submission['Id'] = test_data['Id']
rf_pred = rf_regressor.predict(test_data.drop('Id', axis=1))
submission['Prediction'] = np.expm1(rf_pred)
 submission.to_csv('submission_rf.csv',index=False )