Kaggle competition: Restaurant Revenue Prediction
<SOS> metric: RMSE Kaggle data source: restaurant-revenue-prediction<train_model>
!unzip /kaggle/input/restaurant-revenue-prediction/test.csv.zip
!unzip /kaggle/input/restaurant-revenue-prediction/train.csv.zip
print('Check Condition(1a):', w1 + w2 + w3)
 if w1 + w2 + w3 - 1 <= 1e-10:
print('Great! The sum of all weights equals to 1!')
 else:
print('Manual adjustion is needed to modify the weights.' )<load_from_csv>
import numpy as np
import pandas as pd
from sklearn.linear_model import BayesianRidge, ElasticNet, Lasso, Ridge, LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV, cross_val_score
from sklearn.metrics import make_scorer, mean_squared_log_error
from sklearn.preprocessing import StandardScaler
from bayes_opt import BayesianOptimization
import matplotlib.pyplot as plt
submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 public_ids = list(submission['sig_id'].values)
 submission.shape<load_from_csv>
filterwarnings('ignore' )
test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 test_ids = list(test['sig_id'].values)
 private_ids = list(set(test_ids)-set(public_ids))
len(private_ids )<prepare_output>
def preparing_data(df):
df.drop('Id', axis=1, inplace=True)
 df = df[~df['revenue'].ge(1.25 * 10**7)]
df['Open Date'] = pd.to_datetime(df['Open Date'], format='%m/%d/%Y')
 df['OpenDays'] =(df['Open Date'].max() - df['Open Date'] ).astype('timedelta64[D]' ).astype(int)+ 1
df['OpenDays'] = np.log(df['OpenDays'])
 df.drop('Open Date', axis=1, inplace=True)
 df.drop('City', axis=1, inplace=True)
 df = pd.get_dummies(df, columns=['City Group'], drop_first=True)
 df['Type'].replace('MB', 'DT', inplace=True)
 df = pd.get_dummies(df, columns=['Type'], drop_first=True)
 df['revenue'] = np.log(df['revenue'])
 return df
col = sub1.columns
tar_col = col[1:]
sub = sub1.copy()
sub.loc[:,train_targets.columns] = sub1.loc[:,train_targets.columns]*w1+ sub2.loc[:,train_targets.columns]*w2 + sub3.loc[:,train_targets.columns]*w3<drop_column>
train = pd.read_csv('./train.csv')
 test = pd.read_csv('./test.csv')
 df = pd.concat([train, test], axis=0 )
del oof1,oof2,oof3,sub1,sub2,sub3
gc.collect()<prepare_output>
df = preparing_data(df )
predictions = []
predictions.append(sub)
 predictions.append(sub5)
 predictions.append(sub9)
<drop_column>
df = df[['P2', 'P6', 'P23', 'P28', 'OpenDays', 'revenue']]
target_columns = list(sub.columns)
 target_columns.remove('sig_id' )<prepare_x_and_y>
train = df[~df['revenue'].isnull() ]
test = df[df['revenue'].isnull() ]
y_pred = pd.DataFrame()
y_pred['sig_id'] = predictions[0]['sig_id']
for column in target_columns:
column_data = []
for i in range(len(predictions)) :
column_data.append(predictions[i][column])
 y_pred[column] = np.mean(column_data, axis=0 )<prepare_output>
train_X, train_y = train.drop('revenue', axis=1), train['revenue']
test_X = test.drop('revenue', axis=1 )
score1 = 1.- 0.01840
score3 = 1.- 0.01846
score5 = 1.- 0.01833
scores = [score1,score3,score5]
sum_scores = sum(scores)
 weights = [x / sum_scores for x in scores]
weighted_y_pred = pd.DataFrame()
weighted_y_pred['sig_id'] = predictions[0]['sig_id']
for column in target_columns:
column_data = []
for i in range(len(predictions)) :
column_data.append(predictions[i][column] * weights[i])
 weighted_y_pred[column] = np.sum(column_data, axis=0)
 weighted_y_pred.shape<prepare_output>
parameters = [{'alpha': [0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30, 100, 300, 1000, 3000, 10000],
'normalize': [True, False]}]
y_pred = weighted_y_pred 
<save_to_csv>
lr = LinearRegression(normalize=True ).fit(train_X, train_y)
 lr_res = np.e ** lr.predict(test_X)
 pd.DataFrame(lr_res, columns=['Prediction'] ).to_csv('linear_regression.csv', index=True, index_label='Id' )
submission = pd.DataFrame(index = public_ids + private_ids, columns=target_columns)
 submission.index.name = 'sig_id'
submission[:] = 0
submission.loc[y_pred.sig_id,:] = y_pred[target_columns].values
submission.loc[test[test.cp_type == 'ctl_vehicle'].sig_id] = 0
submission.to_csv('submission.csv', index=True)
 submission.head().T<set_options>
grid1 = GridSearchCV(Ridge() , parameters)
 grid1.fit(train_X, train_y)
 grid1.best_estimator_
warnings.filterwarnings('ignore')
<load_from_csv>
ridge_grid = grid1.predict(test_X)
 ridge_grid = np.e ** ridge_grid
pd.DataFrame(ridge_grid, columns=['Prediction'] ).to_csv('ridge_grid.csv', index=True, index_label='Id' )
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )<define_variables>
grid2 = GridSearchCV(Lasso() , parameters)
 grid2.fit(train_X, train_y)
 grid2.best_estimator_
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]<categorify>
lasso_grid = grid2.predict(test_X)
 lasso_grid = np.e ** lasso_grid
pd.DataFrame(lasso_grid, columns=['Prediction'] ).to_csv('lasso_grid.csv', index=True, index_label='Id' )
for col in(GENES + CELLS):
transformer = QuantileTransformer(n_quantiles=100,random_state=0, output_distribution="normal")
 vec_len = len(train_features[col].values)
 vec_len_test = len(test_features[col].values)
 raw_vec = train_features[col].values.reshape(vec_len, 1)
 transformer.fit(raw_vec)
 train_features[col] = transformer.transform(raw_vec ).reshape(1, vec_len)[0]
test_features[col] = transformer.transform(test_features[col].values.reshape(vec_len_test, 1)).reshape(1, vec_len_test)[0]<set_options>
params = [{'alpha': [0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30, 100, 300, 1000, 3000, 10000],
'normalize': [True, False]}]
grid3 = GridSearchCV(ElasticNet(max_iter=10**4), parameters)
 grid3.fit(train_X, train_y)
 grid3.best_estimator_
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(seed=42 )<concatenate>
elastic_grid = grid3.predict(test_X)
 elastic_grid = np.e ** elastic_grid
pd.DataFrame(elastic_grid, columns=['Prediction'] ).to_csv('elastic_grid.csv', index=True, index_label='Id' )
n_comp = 600
data = pd.concat([pd.DataFrame(train_features[GENES]), pd.DataFrame(test_features[GENES])])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[GENES]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )<concatenate>
ridge_bayesian = BayesianRidge(normalize=True)
 ridge_bayesian.fit(train_X, train_y)
 ridge_bayesian.alpha_
n_comp = 50
data = pd.concat([pd.DataFrame(train_features[CELLS]), pd.DataFrame(test_features[CELLS])])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[CELLS]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )<create_dataframe>
ridge_bayesian_res = ridge_bayesian.predict(test_X)
 ridge_bayesian_res = np.e ** ridge_bayesian_res
pd.DataFrame(ridge_bayesian_res, columns=['Prediction'] ).to_csv('ridge_bayesian.csv', index=True, index_label='Id' )
var_thresh = VarianceThreshold(0.8)
 data = train_features.append(test_features)
 data_transformed = var_thresh.fit_transform(data.iloc[:, 4:])
 train_features_transformed = data_transformed[ : train_features.shape[0]]
test_features_transformed = data_transformed[-test_features.shape[0] : ]
train_features = pd.DataFrame(train_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 train_features = pd.concat([train_features, pd.DataFrame(train_features_transformed)], axis=1)
 test_features = pd.DataFrame(test_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 test_features = pd.concat([test_features, pd.DataFrame(test_features_transformed)], axis=1)
 train_features.shape<merge>
def rmsle(y_true, y_pred):
return np.sqrt(mean_squared_log_error(np.expm1(y_true), np.expm1(y_pred)))
 neg_rmsle = make_scorer(rmsle, greater_is_better=False)
 params = { 'alpha':(0.001, 1), 'fit_intercept':(0,1.99)}
train = train_features.merge(train_targets_scored, on='sig_id')
 train = train[train['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 target = train[train_targets_scored.columns]<drop_column>
def target(**params):
fit_intercept = int(params['fit_intercept'])
 fit_intercept_dict = { 0: False, 1: True }
model = Lasso(alpha = params['alpha'],
fit_intercept = fit_intercept_dict[fit_intercept],
copy_X = True)
 scores = cross_val_score(model, train_X, train_y, scoring=neg_rmsle, cv=3)
 return scores.mean()
lasso_alpha = BayesianOptimization(target, params, random_state=101)
 lasso_alpha.maximize(init_points=5, n_iter=15, acq='ucb', kappa=2 )
train = train.drop('cp_type', axis=1)
 test = test.drop('cp_type', axis=1 )<drop_column>
lasso = Lasso(alpha=0.3077 ).fit(train_X, train_y)
 pd.DataFrame(np.e ** lasso.predict(test_X), columns=['Prediction'] ).to_csv('lasso_bayesian.csv', index=True, index_label='Id' )
target_cols = target.drop('sig_id', axis=1 ).columns.values.tolist()<split>
def target(**params):
fit_intercept = int(params['fit_intercept'])
 fit_intercept_dict = { 0: False, 1: True }
model = ElasticNet(alpha = params['alpha'],
fit_intercept = fit_intercept_dict[fit_intercept],
copy_X = True)
 scores = cross_val_score(model, train_X, train_y, scoring=neg_rmsle, cv=3)
 return scores.mean()
elastic_alpha = BayesianOptimization(target, params, random_state=101)
 elastic_alpha.maximize(init_points=5, n_iter=15, acq='ucb', kappa=2 )
folds = train.copy()
mskf = MultilabelStratifiedKFold(n_splits=7)
 for f,(t_idx, v_idx)in enumerate(mskf.split(X=train, y=target)) :
folds.loc[v_idx, 'kfold'] = int(f)
 folds['kfold'] = folds['kfold'].astype(int)
 folds<categorify>
el1 = ElasticNet(alpha=0.3077 ).fit(train_X, train_y)
 pd.DataFrame(np.e ** el1.predict(test_X), columns=['Prediction'] ).to_csv('el1.csv', index=True, index_label='Id')
 el2 = ElasticNet(alpha=0.2426 ).fit(train_X, train_y)
 pd.DataFrame(np.e ** el2.predict(test_X), columns=['Prediction'] ).to_csv('el2.csv', index=True, index_label='Id')
 el3 = ElasticNet(alpha=0.04738 ).fit(train_X, train_y)
 pd.DataFrame(np.e ** el3.predict(test_X), columns=['Prediction'] ).to_csv('el3.csv', index=True, index_label='Id' )
class MoADataset:
def __init__(self, features, targets):
self.features = features
self.targets = targets
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float),
'y' : torch.tensor(self.targets[idx, :], dtype=torch.float)
 }
return dct
class TestDataset:
def __init__(self, features):
self.features = features
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float)
 }
return dct<train_model>
forest = RandomForestRegressor().fit(train_X, train_y)
 forest_res = np.e ** forest.predict(test_X)
 pd.DataFrame(forest_res, columns=['Prediction'] ).to_csv('random_forest.csv', index=True, index_label='Id' )