Kaggle competition: Restaurant Revenue Prediction
oof1 = valid_results.values[:,1:]
sub1 = sub.copy()<drop_column>
import pandas as pd
import numpy as np
del valid_results,sub,test,y_true,y_pred,train
gc.collect() 
<set_options>
data = pd.read_csv('/kaggle/input/restaurant-revenue-prediction/train.csv.zip')
 test_data = pd.read_csv('/kaggle/input/restaurant-revenue-prediction/test.csv.zip')
 data.head()
warnings.filterwarnings('ignore' )<import_modules>
data['Type_IL'] = np.where(data['Type'] == 'IL', 1, 0)
 data['Type_FC'] = np.where(data['Type'] == 'FC', 1, 0)
 data['Type_DT'] = np.where(data['Type'] == 'DT', 1, 0)
 data['Big_Cities'] = np.where(data['City Group'] == 'Big Cities', 1, 0)
 data['Days_Open'] =(pd.to_datetime('2015-03-23')- pd.to_datetime(data['Open Date'])).dt.days
data = data.drop('Type', axis=1)
 data = data.drop('City Group', axis=1)
 data = data.drop('City', axis=1)
 data = data.drop('Open Date', axis=1)
 test_data['Type_IL'] = np.where(test_data['Type'] == 'IL', 1, 0)
 test_data['Type_FC'] = np.where(test_data['Type'] == 'FC', 1, 0)
 test_data['Type_DT'] = np.where(test_data['Type'] == 'DT', 1, 0)
 test_data['Big_Cities'] = np.where(test_data['City Group'] == 'Big Cities', 1, 0)
 test_data['Days_Open'] =(pd.to_datetime('2015-03-23')- pd.to_datetime(test_data['Open Date'])).dt.days
test_data = test_data.drop('Type', axis=1)
 test_data = test_data.drop('City Group', axis=1)
 test_data = test_data.drop('City', axis=1)
 test_data = test_data.drop('Open Date', axis=1 )
from sklearn.preprocessing import QuantileTransformer 
<import_modules>
X = data.drop(['Id', 'revenue'], axis=1)
 Y = data.revenue
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
import tensorflow_addons as tfa
from sklearn.model_selection import KFold
from tensorflow.keras import layers,regularizers,Sequential,backend,callbacks,optimizers,metrics,losses<load_from_csv>
def check_rmse(X, Y, alpha):
RMSE_lasso = []
RMSE_ridge = []
for i in alpha:
lasso = Lasso(alpha=i)
 lasso.fit(X, Y)
 ridge = Ridge(alpha=i)
 ridge.fit(X, Y)
 RMSE_lasso.append(metrics.mean_squared_error(Y, lasso.predict(X)))
 RMSE_ridge.append(metrics.mean_squared_error(Y, ridge.predict(X)))
 return(RMSE_lasso, RMSE_ridge)
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')
 del train_targets_nonscored['sig_id']
test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )<define_variables>
lasso = Lasso(alpha=5.5)
 lasso.fit(X, Y)
 metrics.mean_squared_error(Y, lasso.predict(X))
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]<normalization>
model = Lasso(alpha=5.5)
 model.fit(X, Y)
 test_predicted = pd.DataFrame()
test_predicted['Id'] = test_data.Id
test_predicted['Prediction'] = model.predict(test_data.drop('Id', axis=1))
test_predicted.to_csv('submission-lasso-5.5.csv', index=False)
 test_predicted.describe()
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>
model = RandomForestRegressor(n_estimators=150)
 model.fit(X, Y)
 test_predicted = pd.DataFrame()
test_predicted['Id'] = test_data.Id
test_predicted['Prediction'] = model.predict(test_data.drop('Id', axis=1))
test_predicted.to_csv('submission-random-forest.csv', index=False)
 test_predicted.describe()
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 )<create_dataframe>
model = Ridge(alpha=330)
 model.fit(X, Y)
 test_predicted = pd.DataFrame()
test_predicted['Id'] = test_data.Id
test_predicted['Prediction'] = model.predict(test_data.drop('Id', axis=1))
test_predicted.to_csv('submission-ridge-330.csv', index=False)
 test_predicted.describe()
n_comp = 600
data = pd.concat([pd.DataFrame(train_features[GENES]), pd.DataFrame(test_features[GENES])])
 data_PCA = PCA(n_components=n_comp, random_state=42 ).fit(data[GENES][:train_features.shape[0]])
 data2 =(data_PCA.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 )<create_dataframe>
model = Lasso(alpha=200000)
 model.fit(X, Y)
 test_predicted = pd.DataFrame()
test_predicted['Id'] = test_data.Id
test_predicted['Prediction'] = model.predict(test_data.drop('Id', axis=1))
test_predicted.to_csv('submission-lasso-high-alpha.csv', index=False)
 test_predicted.describe()
n_comp = 50
data = pd.concat([pd.DataFrame(train_features[CELLS]), pd.DataFrame(test_features[CELLS])])
 data_PCA =(PCA(n_components=n_comp, random_state=42 ).fit(data[CELLS][:train_features.shape[0]]))
data2 =(data_PCA.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 )<prepare_x_and_y>
data['Time_Open'] = round(data['Days_Open'] / 700, 0)
 data = data.drop('Days_Open', axis=1)
 test_data['Time_Open'] = round(test_data['Days_Open'] / 700, 0)
 test_data = test_data.drop('Days_Open', axis=1)
var_thresh = VarianceThreshold(0.8)
 data = train_features.append(test_features)
 data_transformed_var = var_thresh.fit(data.iloc[:, 4:][:train_features.shape[0]])
 data_transformed = data_transformed_var.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<feature_engineering>
X = data.drop(['Id', 'revenue'], axis=1)
 Y = data.revenue
def fe_stats(train, test):
features_g = list(train.columns[4:776])
 features_c = list(train.columns[776:876])
 for df in train, test:
df['g_sum'] = df[features_g].sum(axis = 1)
 df['g_mean'] = df[features_g].mean(axis = 1)
 df['g_std'] = df[features_g].std(axis = 1)
 df['g_kurt'] = df[features_g].kurtosis(axis = 1)
 df['g_skew'] = df[features_g].skew(axis = 1)
 df['c_sum'] = df[features_c].sum(axis = 1)
 df['c_mean'] = df[features_c].mean(axis = 1)
 df['c_std'] = df[features_c].std(axis = 1)
 df['c_kurt'] = df[features_c].kurtosis(axis = 1)
 df['c_skew'] = df[features_c].skew(axis = 1)
 df['gc_sum'] = df[features_g + features_c].sum(axis = 1)
 df['gc_mean'] = df[features_g + features_c].mean(axis = 1)
 df['gc_std'] = df[features_g + features_c].std(axis = 1)
 df['gc_kurt'] = df[features_g + features_c].kurtosis(axis = 1)
 df['gc_skew'] = df[features_g + features_c].skew(axis = 1)
 return train, test
train_features,test_features=fe_stats(train_features,test_features )<merge>
model = Ridge(alpha=330)
 model.fit(X, Y)
 test_predicted = pd.DataFrame()
test_predicted['Id'] = test_data.Id
test_predicted['Prediction'] = model.predict(test_data.drop('Id', axis=1))
test_predicted.to_csv('submission.csv', index=False)
 test_predicted
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>
model = Lasso(alpha=200000)
 model.fit(X, Y)
 test_predicted = pd.DataFrame()
test_predicted['Id'] = test_data.Id
test_predicted['Prediction'] = model.predict(test_data.drop('Id', axis=1))
test_predicted.to_csv('submission-lasso-.csv', index=False)
 test_predicted.describe()