Kaggle competition: Restaurant Revenue Prediction
<SOS> metric: RMSE Kaggle data source: restaurant-revenue-prediction<categorify>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
for el in predictors:
el_type = df[el].dtype
if el_type == 'object':
labelencoder = LabelEncoder()
df.loc[:, el] = labelencoder.fit_transform(df.loc[:, el])
 df[el] = df[el]**0.5 
<data_type_conversions>
train = pd.read_csv('/kaggle/input/restaurant-revenue-prediction/train.csv.zip')
 test = pd.read_csv('/kaggle/input/restaurant-revenue-prediction/test.csv.zip')
 train_Id = train.Id
test_Id = test.Id
train.drop('Id', axis=1, inplace=True)
 test.drop('Id', axis=1, inplace=True )
df['Survived'].replace(to_replace='no', value=0, inplace=True)
 df['Survived'].replace(to_replace='yes', value=1, inplace=True)
 df['Survived'] = df['Survived'].astype('int64')
 df['Survived'].head(3 )<predict_on_test>
pd.options.display.max_columns = None
pd.options.display.max_rows = 80
pd.options.display.float_format = '{:.2f}'.format
%matplotlib inline
warnings.filterwarnings('ignore')
 %matplotlib inline
NaN_predict(df )<define_variables>
train['pd_date'] = pd.to_datetime(train['Open Date'], format='%m/%d/%Y')
 train['Open_Year'] = train['pd_date'].dt.strftime('%Y')
 train['Open_Month'] = train['pd_date'].dt.strftime('%m')
 train["Open Date"] = pd.to_datetime(train["Open Date"])
 train["Day"] = train["Open Date"].apply(lambda x:x.day)
 train["kijun"] = "2015-04-27"
train["kijun"] = pd.to_datetime(train["kijun"])
 train["BusinessPeriod"] =(train["kijun"] - train["Open Date"] ).apply(lambda x: x.days)
 train = train.drop('kijun', axis=1)
 train = train.drop('pd_date',axis=1)
 train = train.drop('Open Date',axis=1 )
target_column = ['Survived']
predictors = list(set(list(df.columns)) -set(target_column))
predictors<prepare_x_and_y>
test['pd_date'] = pd.to_datetime(test['Open Date'], format='%m/%d/%Y')
 test['Open_Year'] = test['pd_date'].dt.strftime('%Y')
 test['Open_Month'] = test['pd_date'].dt.strftime('%m')
 test["Open Date"] = pd.to_datetime(test["Open Date"])
 test["Day"] = test["Open Date"].apply(lambda x:x.day)
 test["kijun"] = "2015-04-27"
test["kijun"] = pd.to_datetime(test["kijun"])
 test["BusinessPeriod"] =(test["kijun"] - test["Open Date"] ).apply(lambda x: x.days)
 test = test.drop('kijun', axis=1)
 test = test.drop('pd_date',axis=1)
 test = test.drop('Open Date',axis=1 )
X = df.loc[:, predictors]
X<prepare_x_and_y>
train.dtypes.value_counts()
y = df.loc[:,target_column]
y<set_options>
train.nunique(axis=0 )
def features_selection(X, y):

 if not sys.warnoptions:
warnings.simplefilter("ignore")
 global droped_features
droped_features = []
def cross_val(X, y, features):
CV = ShuffleSplit(n_splits=5, test_size=0.25, random_state=0)
 test_df = X[features]
model = lgb.LGBMClassifier(random_state=0)
 model.fit(test_df, y)
 cv_score = cross_val_score(model,
test_df,
y,
cv=CV,
scoring='f1')
 cv_score = cv_score.mean()
return cv_score
features = list(X.columns)
 SCORES = pd.DataFrame(index = features, columns=['cv_score'])
 score_with_all_features = cross_val(X, y, features)
 print(f'{score_with_all_features} General cv_score with all features')
 cv_score_of_monstrous_feature = 1
while score_with_all_features <= cv_score_of_monstrous_feature:
print('
')
 features = list(X.columns)
 SCORES = pd.DataFrame(index = features, columns=['cv_score'])
 score_with_all_features = cross_val(X, y, features)
 print(f'{len(features)} number of features')
 print(f'{score_with_all_features} score with all features')
 ')
 for without_feature in features:
without_feature = [without_feature]
fit_faetures = features[:]
fit_faetures = list(set(fit_faetures)- set(without_feature))
score = cross_val(X, y, fit_faetures)
 SCORES.loc[without_feature] = score
SCORES = SCORES.sort_values(by=['cv_score'], ascending=False)
 print('
__SORTED SCORES__')
 print(SCORES)
 monstrous_feature = SCORES.index[0]
cv_score_of_monstrous_feature = SCORES.iloc[0][0]
if score_with_all_features <= cv_score_of_monstrous_feature:
X.drop([monstrous_feature], axis=1, inplace=True)
 droped_features.append(monstrous_feature)
 print('--------------------------------------------')
 print(f' DROP ==== {monstrous_feature}')
 print('--------------------------------------------')
 else:
print('
')
 print(f'These features have been droped:
{droped_features}')
 print('
')
 return X, droped_features 
<compute_test_metric>
print(f'categorical variables: {cats}')
 print(f'numerical variables: {nums}' )
features_selection(X, y )<drop_column>
nominal_list =cats
num_list = nums
df.drop(droped_features, axis=1, inplace=True )<import_modules>
train = train.drop('Open_Month',axis=1)
 test= test.drop('Open_Month',axis=1)
 nominal_list.remove('Open_Month' )
from lightgbm import LGBMClassifier
from xgboost import XGBClassifier
from catboost import CatBoostClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier<normalization>
mean_revenue_per_city = train[['City', 'revenue']].groupby('City', as_index=False ).mean()
mean_revenue_per_city.head()
mean_revenue_per_city['revenue'] = mean_revenue_per_city['revenue'].apply(lambda x: int(x/1e6))
mean_revenue_per_city
mean_dict = dict(zip(mean_revenue_per_city.City, mean_revenue_per_city.revenue))
mean_dict
def constriction_hyperparameters_space(X,
y,
algorithm,
params,
n_iter_RandomizedSearchCV=100,
times_for_estimate=10):
global best_parameters
print('scores:
')
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
 CV = ShuffleSplit(n_splits=4, test_size=0.25, random_state=0)
 estimator = algorithm()
RESULTS_DF = None
PARAMS_DICT = None
PARAMS_DICT = {}
for i in range(0, times_for_estimate):
RANDOMIZED_MODEL = RandomizedSearchCV(estimator=estimator,
param_distributions=params,
n_iter=n_iter_RandomizedSearchCV,
scoring='f1',
cv=CV,
verbose=0,
n_jobs = -1)
 RANDOMIZED_FITED_BEST_MODEL = RANDOMIZED_MODEL.fit(X_train, y_train)
 best_parameters = RANDOMIZED_FITED_BEST_MODEL.best_params_
pred_test = RANDOMIZED_FITED_BEST_MODEL.predict(X_test)
 f1 = f1_score(y_test,pred_test)
 print(f' f1: {f1}')
 model = algorithm(**best_parameters)
 cv_score = cross_val_score(model, X, y, cv=CV)
 std_per =(cv_score.std())* 2
cv_score = cv_score.mean()
print(f'cv_score: {cv_score}')
 print(f' params: {best_parameters}')
 PARAMS_DICT[cv_score] = best_parameters
columns = ['cv_score', 'std_per', 'f1',]
values = []
values.append(cv_score)
 values.append(std_per)
 values.append(f1)
 for k,v in best_parameters.items() :
columns.append(k)
 values.append(v)
 if RESULTS_DF is None:
RESULTS_DF = pd.DataFrame(columns=columns)
 else:
pass
RESULTS_DF.loc[i] = values
print('
')
 RESULTS_DF = RESULTS_DF.sort_values(by='cv_score', ascending = False)
 RESULTS_DF.reset_index(drop=True, inplace=True)
 return RESULTS_DF, best_parameters<init_hyperparams>
l1_l2_and = set(city_train_list)& set(city_test_list)
 print(l1_l2_and)
 print(len(l1_l2_and))
<define_search_space>
len(set(city_test_list ).difference(city_train_list))
params = {'num_leaves': np.arange(600, 750, step=10),
'max_depth': [4, 5, 6, 7, 8],
'learning_rate': [0.01],
'n_estimators': np.arange(2000, 2300, step=20),
'subsample': [0.9, 1.0],
'feature_fraction': [0.3, 0.4],
'bagging_fraction': [0.7, 0.8],
'bagging_seed': np.arange(16, 19, step=1),
'lambda_l1': [0.1],
'lambda_l2': [0.1],
'min_child_samples': [2, 5],
'min_split_gain': [0.01],
'n_jobs':[-1],
}
constriction_hyperparameters_space(X,
y,
LGBMClassifier,
params,
n_iter_RandomizedSearchCV=100,
times_for_estimate=1)
<define_search_space>
len(set(city_train_list ).difference(city_test_list))
<init_hyperparams>
def adjust_cities(full_full_data, train, k):
relevant_pvars = ["P1", "P2", "P11", "P19", "P20", "P23","P30"]
train = train.loc[:, relevant_pvars]
kmeans = cluster.KMeans(n_clusters=k)
 kmeans.fit(train)
 full_data['City_Cluster'] = kmeans.predict(full_data.loc[:, relevant_pvars])
 return full_data
<define_variables>
num_train = train.shape[0]
num_test = test.shape[0]
print(num_train, num_test)
 full_data = pd.concat([train, test], ignore_index=True )
target_column = ['Survived']
predictors = list(set(list(df.columns)) -set(target_column))
predictors<prepare_x_and_y>
full_data = adjust_cities(full_data, train, 20)
 full_data
full_data = full_data.drop(['City'], axis=1 )
X = df.loc[:890, predictors]
X<prepare_x_and_y>
train = full_data[:num_train]
test = full_data[num_train:]
print("Train :",train.shape)
 print("Test:",test.shape)
 test
y = df.loc[:890,target_column]
y<prepare_x_and_y>
mean_revenue_per_city = train[['City_Cluster', 'revenue']].groupby('City_Cluster', as_index=False ).mean()
mean_revenue_per_city.head()
mean_revenue_per_city['revenue'] = mean_revenue_per_city['revenue'].apply(lambda x: int(x/1e6))
mean_revenue_per_city
mean_dict = dict(zip(mean_revenue_per_city.City_Cluster, mean_revenue_per_city.revenue))
mean_dict
test_X = df.loc[891:, predictors]
test_X<import_modules>
city_rev = []
for i in full_data['City_Cluster']:
for key, value in mean_dict.items() :
if i == key:
city_rev.append(value)
 df_city_rev = pd.DataFrame({'city_rev':city_rev})
 full_data = pd.concat([full_data,df_city_rev],axis=1)
 full_data.head
nominal_list.extend(['City_Cluster'])
 nominal_list.remove('City' )
from sklearn.metrics import f1_score<create_dataframe>
le = LabelEncoder()
le_count = 0
for i in range(len(nominal_list)) :
if len(list(full_data[nominal_list[i]].unique())) <= 2:
le.fit(full_data[nominal_list[i]])
 full_data[nominal_list[i]] = le.transform(full_data[nominal_list[i]])
 le_count += 1
print('%d columns were label encoded.' % le_count )
BAGGING_DF = pd.DataFrame()<set_options>
full_data = pd.get_dummies(full_data)
 print('full_dataing Features shape: ', full_data.shape )
result = BAGGING_DF.mode(axis='columns')
 result.tail(10 )<prepare_output>
def tukey_outliers(x):
q1 = np.percentile(x,25)
 q3 = np.percentile(x,75)
 iqr = q3-q1
min_range = q1 - iqr*1.5
max_range = q3 + iqr*1.5
outliers = x[(x<min_range)|(x>max_range)]
return outliers
predictions = np.array(result[0])
 predictions<save_to_csv>
skewed_data = train[num_list].apply(lambda x: skew(x)).sort_values(ascending=False)
 skewed_data[:10]
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Submission was successfully saved!" )<load_from_csv>
train = full_data[:num_train]
test = full_data[num_train:]
print("Train :",train.shape)
 print("Test:",test.shape )
read_train_data = pd.read_csv('.. /input/titanic/train.csv')
 print(read_train_data )<prepare_x_and_y>
( X_train, X_test, y_train, y_test)= train_test_split(train_X, train_y , test_size = 0.3 , random_state = 0)
 print("X_train: "+str(X_train.shape))
print("X_test: "+str(X_test.shape))
print("y_train: "+str(y_train.shape))
print("y_test: "+str(y_test.shape))
numeric_train_data = read_train_data
print(numeric_train_data)
 print(type(numeric_train_data.columns))
feature_names = ['Pclass', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
print("-------------------feature names-------------------")
 print(feature_names)
 y = numeric_train_data.Survived
X = pd.DataFrame(numeric_train_data[feature_names])
 print(X)
 print(y )<feature_engineering>
from sklearn.model_selection import GridSearchCV, cross_val_score, learning_curve
from sklearn.metrics import mean_absolute_error
from sklearn.linear_model import Lasso
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import ElasticNet
from sklearn.neighbors import KNeighborsRegressor
from sklearn.svm import SVR
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.tree import DecisionTreeRegressor
from xgboost import XGBRegressor
col_with_missing_values_categorical_val = [col for col in X.columns
if(X[col].isnull().any() &(X[col].dtype == 'object')) ]
print(col_with_missing_values_categorical_val)
 X[col_with_missing_values_categorical_val] = X[col_with_missing_values_categorical_val].fillna("Unknown")
 print(X )<categorify>
random_state = 2
classifiers = []
classifiers.append(Lasso(random_state=random_state))
classifiers.append(LinearRegression())
 classifiers.append(Ridge(random_state=random_state))
classifiers.append(ElasticNet(random_state=random_state))
classifiers.append(KNeighborsRegressor())
 classifiers.append(SVR())
 classifiers.append(RandomForestRegressor(random_state=random_state))
classifiers.append(GradientBoostingRegressor())
 classifiers.append(AdaBoostRegressor(random_state = random_state))
classifiers.append(DecisionTreeRegressor())
 classifiers.append(XGBRegressor() )
col_with_categorical_data = [col for col in X.columns
if(X[col].dtype=='object')]
dummies = pd.get_dummies(X[col_with_categorical_data])
 merged = pd.concat([X,dummies], axis=1)
 X = merged.drop(col_with_categorical_data, axis=1)
 print(X.head(62))<split>
print(cv_res.sort_values(ascending=False, by='CrossValMeans'))
imputed_X_train, imputed_X_test, y_train, y_test = train_test_split(imputed_X, y, test_size=0.2, random_state=0)
 print(imputed_X_train)
 print(imputed_X_test)
 print(y_train)
 print(y_test )<normalization>
def objective(trial):
params = {
'alpha': trial.suggest_loguniform("alpha", 0.1, 5),
'fit_intercept': trial.suggest_categorical('fit_intercept', [True, False]),
'normalize': trial.suggest_categorical('normalize', [True, False]),
}
reg = Ridge(**params)
 reg.fit(X_train, y_train)
 y_pred = reg.predict(X_test)
 mae = mean_absolute_error(y_test, y_pred)
 return mae
sc_age = StandardScaler()
sc_fare = StandardScaler()
imputed_X_train['Age'] = sc_age.fit_transform(imputed_X_train['Age'].values.reshape(-1,1))
imputed_X_train['Fare'] = sc_fare.fit_transform(imputed_X_train['Fare'].values.reshape(-1,1))
imputed_X_test['Age'] = sc_age.transform(imputed_X_test['Age'].values.reshape(-1,1))
imputed_X_test['Fare'] = sc_fare.transform(imputed_X_test['Fare'].values.reshape(-1,1))
print(imputed_X_train)
 print(imputed_X_test )<compute_train_metric>
study = optuna.create_study()
study.optimize(objective, n_trials=100)
 print(f'best score: {study.best_value:.4f}, best params: {study.best_params}' )
model = RandomForestClassifier(bootstrap=True, max_depth=80, max_features=3, min_samples_leaf=3, min_samples_split=10, n_estimators=1000, random_state=0)
 model.fit(imputed_X_train, y_train)
 predicted_y = model.predict(imputed_X_test)
 print(predicted_y)
 calculate_mae = mean_absolute_error(y_test, predicted_y)
 print(calculate_mae )<load_from_csv>
params = {'alpha': 1.7927096237808111, 'fit_intercept': True, 'normalize': True}
reg = Ridge(**params)
 reg.fit(X_train, y_train)
 prediction_log = reg.predict(test_X)
 prediction =np.exp(prediction_log)
 print(prediction )
test_data = pd.read_csv('.. /input/titanic/test.csv')
 print(feature_names)
 X_test = pd.DataFrame(test_data[feature_names])
 col_with_missing_values_categorical_val_test = [col for col in X_test.columns
if(X_test[col].isnull().any() &(X_test[col].dtype == 'object')) ]
print(col_with_missing_values_categorical_val_test)
 col_with_categorical_data_test = [col for col in X_test.columns
if(X_test[col].dtype=='object')]
dummies_test = pd.get_dummies(X_test[col_with_categorical_data_test])
 merged_test = pd.concat([X_test,dummies_test], axis=1)
 X_test = merged_test.drop(col_with_categorical_data_test, axis=1)
 X_test['Embarked_Unknown'] = 0
print(X_test)
 col_with_missing_values_test = [col for col in X_test.columns
if(X_test[col].isnull().any())]
print(col_with_missing_values_test)
 imputed_X_test_data = pd.DataFrame(my_imputer.fit_transform(X_test))
imputed_X_test_data.columns = X_test.columns
print(imputed_X_test)
 imputed_X_test_data['Age'] = sc_age.transform(imputed_X_test_data['Age'].values.reshape(-1,1))
imputed_X_test_data['Fare'] = sc_fare.transform(imputed_X_test_data['Fare'].values.reshape(-1,1))
print(imputed_X_test_data)
 test_preds = model.predict(imputed_X_test_data)
 print(type(test_preds))
print(test_preds)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId,
'Survived': test_preds})
 output.to_csv('./submission.csv', index=False)
 print(pd.read_csv('./submission.csv'))<import_modules>
submission = pd.DataFrame({"Id":test_Id, "Prediction":prediction})
 submission.to_csv("submission.csv", index=False )