Kaggle competition: TMDB Box Office Prediction
params = {'metric' : 'auc',
'boosting_type' : 'gbdt',
'colsample_bytree' : 0.9234,
'num_leaves' : 13,
'max_depth' : -1,
'n_estimators' : 200,
'min_child_samples': 399,
'min_child_weight': 0.1,
'reg_alpha': 2,
'reg_lambda': 5,
'subsample': 0.855,
'verbose' : -1,
'num_threads' : 4
}<train_model>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 submission = pd.read_csv('.. /input/sample_submission.csv' )
lgbm = lgb.train(params,
train_data,
2500,
valid_sets=valid_data,
early_stopping_rounds= 30,
verbose_eval= 10
)<compute_test_metric>
train.loc[train['id'] == 16,'revenue'] = 192864
train.loc[train['id'] == 90,'budget'] = 30000000
train.loc[train['id'] == 118,'budget'] = 60000000
train.loc[train['id'] == 149,'budget'] = 18000000
train.loc[train['id'] == 313,'revenue'] = 12000000
train.loc[train['id'] == 451,'revenue'] = 12000000
train.loc[train['id'] == 464,'budget'] = 20000000
train.loc[train['id'] == 470,'budget'] = 13000000
train.loc[train['id'] == 513,'budget'] = 930000
train.loc[train['id'] == 797,'budget'] = 8000000
train.loc[train['id'] == 819,'budget'] = 90000000
train.loc[train['id'] == 850,'budget'] = 90000000
train.loc[train['id'] == 1007,'budget'] = 2
train.loc[train['id'] == 1112,'budget'] = 7500000
train.loc[train['id'] == 1131,'budget'] = 4300000
train.loc[train['id'] == 1359,'budget'] = 10000000
train.loc[train['id'] == 1542,'budget'] = 1
train.loc[train['id'] == 1570,'budget'] = 15800000
train.loc[train['id'] == 1571,'budget'] = 4000000
train.loc[train['id'] == 1714,'budget'] = 46000000
train.loc[train['id'] == 1721,'budget'] = 17500000
train.loc[train['id'] == 1865,'revenue'] = 25000000
train.loc[train['id'] == 1885,'budget'] = 12
train.loc[train['id'] == 2091,'budget'] = 10
train.loc[train['id'] == 2268,'budget'] = 17500000
train.loc[train['id'] == 2491,'budget'] = 6
train.loc[train['id'] == 2602,'budget'] = 31000000
train.loc[train['id'] == 2612,'budget'] = 15000000
train.loc[train['id'] == 2696,'budget'] = 10000000
train.loc[train['id'] == 2801,'budget'] = 10000000
train.loc[train['id'] == 335,'budget'] = 2
train.loc[train['id'] == 348,'budget'] = 12
train.loc[train['id'] == 470,'budget'] = 13000000
train.loc[train['id'] == 513,'budget'] = 1100000
train.loc[train['id'] == 640,'budget'] = 6
train.loc[train['id'] == 696,'budget'] = 1
train.loc[train['id'] == 797,'budget'] = 8000000
train.loc[train['id'] == 850,'budget'] = 1500000
train.loc[train['id'] == 1199,'budget'] = 5
train.loc[train['id'] == 1282,'budget'] = 9
train.loc[train['id'] == 1347,'budget'] = 1
train.loc[train['id'] == 1755,'budget'] = 2
train.loc[train['id'] == 1801,'budget'] = 5
train.loc[train['id'] == 1918,'budget'] = 592
train.loc[train['id'] == 2033,'budget'] = 4
train.loc[train['id'] == 2118,'budget'] = 344
train.loc[train['id'] == 2252,'budget'] = 130
train.loc[train['id'] == 2256,'budget'] = 1
train.loc[train['id'] == 2696,'budget'] = 10000000
y_hat = lgbm.predict(data)
 score = roc_auc_score(y, y_hat)
 print("Overall AUC: {:.3f}".format(score))<import_modules>
test.loc[test['id'] == 3033,'budget'] = 250
test.loc[test['id'] == 3051,'budget'] = 50
test.loc[test['id'] == 3084,'budget'] = 337
test.loc[test['id'] == 3224,'budget'] = 4
test.loc[test['id'] == 3594,'budget'] = 25
test.loc[test['id'] == 3619,'budget'] = 500
test.loc[test['id'] == 3831,'budget'] = 3
test.loc[test['id'] == 3935,'budget'] = 500
test.loc[test['id'] == 4049,'budget'] = 995946
test.loc[test['id'] == 4424,'budget'] = 3
test.loc[test['id'] == 4460,'budget'] = 8
test.loc[test['id'] == 4555,'budget'] = 1200000
test.loc[test['id'] == 4624,'budget'] = 30
test.loc[test['id'] == 4645,'budget'] = 500
test.loc[test['id'] == 4709,'budget'] = 450
test.loc[test['id'] == 4839,'budget'] = 7
test.loc[test['id'] == 3125,'budget'] = 25
test.loc[test['id'] == 3142,'budget'] = 1
test.loc[test['id'] == 3201,'budget'] = 450
test.loc[test['id'] == 3222,'budget'] = 6
test.loc[test['id'] == 3545,'budget'] = 38
test.loc[test['id'] == 3670,'budget'] = 18
test.loc[test['id'] == 3792,'budget'] = 19
test.loc[test['id'] == 3881,'budget'] = 7
test.loc[test['id'] == 3969,'budget'] = 400
test.loc[test['id'] == 4196,'budget'] = 6
test.loc[test['id'] == 4221,'budget'] = 11
test.loc[test['id'] == 4222,'budget'] = 500
test.loc[test['id'] == 4285,'budget'] = 11
test.loc[test['id'] == 4319,'budget'] = 1
test.loc[test['id'] == 4639,'budget'] = 10
test.loc[test['id'] == 4719,'budget'] = 45
test.loc[test['id'] == 4822,'budget'] = 22
test.loc[test['id'] == 4829,'budget'] = 20
test.loc[test['id'] == 4969,'budget'] = 20
test.loc[test['id'] == 5021,'budget'] = 40
test.loc[test['id'] == 5035,'budget'] = 1
test.loc[test['id'] == 5063,'budget'] = 14
test.loc[test['id'] == 5119,'budget'] = 2
test.loc[test['id'] == 5214,'budget'] = 30
test.loc[test['id'] == 5221,'budget'] = 50
test.loc[test['id'] == 4903,'budget'] = 15
test.loc[test['id'] == 4983,'budget'] = 3
test.loc[test['id'] == 5102,'budget'] = 28
test.loc[test['id'] == 5217,'budget'] = 75
test.loc[test['id'] == 5224,'budget'] = 3
test.loc[test['id'] == 5469,'budget'] = 20
test.loc[test['id'] == 5840,'budget'] = 1
test.loc[test['id'] == 5960,'budget'] = 30
test.loc[test['id'] == 6506,'budget'] = 11
test.loc[test['id'] == 6553,'budget'] = 280
test.loc[test['id'] == 6561,'budget'] = 7
test.loc[test['id'] == 6582,'budget'] = 218
test.loc[test['id'] == 6638,'budget'] = 5
test.loc[test['id'] == 6749,'budget'] = 8
test.loc[test['id'] == 6759,'budget'] = 50
test.loc[test['id'] == 6856,'budget'] = 10
test.loc[test['id'] == 6858,'budget'] = 100
test.loc[test['id'] == 6876,'budget'] = 250
test.loc[test['id'] == 6972,'budget'] = 1
test.loc[test['id'] == 7079,'budget'] = 8000000
test.loc[test['id'] == 7150,'budget'] = 118
test.loc[test['id'] == 6506,'budget'] = 118
test.loc[test['id'] == 7225,'budget'] = 6
test.loc[test['id'] == 7231,'budget'] = 85
test.loc[test['id'] == 5222,'budget'] = 5
test.loc[test['id'] == 5322,'budget'] = 90
test.loc[test['id'] == 5350,'budget'] = 70
test.loc[test['id'] == 5378,'budget'] = 10
test.loc[test['id'] == 5545,'budget'] = 80
test.loc[test['id'] == 5810,'budget'] = 8
test.loc[test['id'] == 5926,'budget'] = 300
test.loc[test['id'] == 5927,'budget'] = 4
test.loc[test['id'] == 5986,'budget'] = 1
test.loc[test['id'] == 6053,'budget'] = 20
test.loc[test['id'] == 6104,'budget'] = 1
test.loc[test['id'] == 6130,'budget'] = 30
test.loc[test['id'] == 6301,'budget'] = 150
test.loc[test['id'] == 6276,'budget'] = 100
test.loc[test['id'] == 6473,'budget'] = 100
test.loc[test['id'] == 6842,'budget'] = 30
import shap<normalization>
pd.DataFrame(train.budget.value_counts() ).T
%time shap_values = shap.TreeExplainer(lgbm ).shap_values(valid_x )<count_values>
train[train['revenue'] < 10][['imdb_id', 'title']].T
data['CODE_GENDER'].value_counts()<filter>
train['revenue'] = np.log1p(train['revenue'] )
data[data['CODE_GENDER']==2]<count_values>
x = train.budget.values.reshape(-1,1)
 y = train.revenue
reg = LinearRegression().fit(x, y )
test['CODE_GENDER'].value_counts()<predict_on_test>
print(f'Regression Score: {reg.score(x, y)}')
 print(f'Regression Coefficient: {reg.coef_[0]}')
 print(f'Regression Intercept: {reg.intercept_}' )
sub_pred = lgbm.predict(test )<prepare_output>
predictions = reg.predict(test['budget'].values.reshape(-1,1))
sub_pred = np.clip(sub_pred, 0, 1 )<save_to_csv>
submission['revenue'] = np.round(np.expm1(predictions))
lgbm_submission.TARGET = sub_pred
lgbm_submission.to_csv('subm_lgbm_auc{:.8f}.csv'.format(score), index=False, float_format='%.8f' )<load_from_csv>
submission.to_csv('submission_budget_linreg.csv', index = False )
app_train = pd.read_csv(path + "application_train.csv")
 app_train.head()<load_from_csv>
rf_cols = ['budget', 'original_language', 'popularity', 'release_date', 'runtime', 'status', 'homepage', 'overview', 'revenue']
rf_train = train[rf_cols].copy()
rf_cols.remove('revenue')
 rf_test = test[rf_cols].copy()
bureau = pd.read_csv(path + "bureau.csv")
 bureau.head()<load_from_csv>
def fill_budget(df, median_budget):
df['budget_is_median'] = 0
df.loc[df.budget == 0, 'budget_is_median'] = 1
df.loc[df.budget == 0, 'budget'] = median_budget
return df
bureau_balance = pd.read_csv(path + "bureau_balance.csv")
 bureau_balance.head()<load_from_csv>
rf_train = fill_budget(rf_train, median_budget)
 rf_test = fill_budget(rf_test, median_budget )
credit_card_balance = pd.read_csv(path + "credit_card_balance.csv")
 credit_card_balance.head()<load_from_csv>
rf_combined = pd.concat([rf_train, rf_test], sort=False )
pcb = pd.read_csv(path + "POS_CASH_balance.csv")
 pcb.head()<load_from_csv>
le = LabelEncoder()
le.fit(rf_combined['original_language'])
 rf_train['original_language'] = le.transform(rf_train['original_language'])
 rf_test['original_language'] = le.transform(rf_test['original_language'] )
previous_application = pd.read_csv(path + "previous_application.csv")
 previous_application.head()<load_from_csv>
rf_test.loc[rf_test['status'].isnull() == True, 'status'] = 'Released'
rf_combined.loc[rf_combined['status'].isnull() == True, 'status'] = 'Released'
installments_payments = pd.read_csv(path + "installments_payments.csv")
 installments_payments.head()<prepare_x_and_y>
le = LabelEncoder()
le.fit(rf_combined['status'])
 rf_train['status'] = le.transform(rf_train['status'])
 rf_test['status'] = le.transform(rf_test['status'] )
app_test = pd.read_csv('.. /input/application_test.csv')
 app_test['is_test'] = 1
app_test['is_train'] = 0
app_train['is_test'] = 0
app_train['is_train'] = 1
Y = app_train['TARGET']
train_X = app_train.drop(['TARGET'], axis = 1)
 test_id = app_test['SK_ID_CURR']
test_X = app_test
data = pd.concat([train_X, test_X], axis=0 )<categorify>
rf_train.loc[rf_train['homepage'].isnull() == True, 'homepage'] = 0
rf_train.loc[rf_train['homepage'].isnull() == False, 'homepage'] = 1
rf_test.loc[rf_test['homepage'].isnull() == True, 'homepage'] = 0
rf_test.loc[rf_test['homepage'].isnull() == False, 'homepage'] = 1
def _get_categorical_features(df):
feats = [col for col in list(df.columns)if df[col].dtype == 'object']
return feats
def _factorize_categoricals(df, cats):
for col in cats:
df[col], _ = pd.factorize(df[col])
 return df
def _get_dummies(df, cats):
for col in cats:
df = pd.concat([df, pd.get_dummies(df[col], prefix=col)], axis=1)
 return df
data_cats = _get_categorical_features(data)
 prev_app_cats = _get_categorical_features(previous_application)
 bureau_cats = _get_categorical_features(bureau)
 pcb_cats = _get_categorical_features(pcb)
 ccbal_cats = _get_categorical_features(credit_card_balance)
 previous_application = _get_dummies(previous_application, prev_app_cats)
 bureau = _get_dummies(bureau, bureau_cats)
 pcb = _get_dummies(pcb, pcb_cats)
 credit_card_balance = _get_dummies(credit_card_balance, ccbal_cats)
 data = _factorize_categoricals(data, data_cats )<merge>
def fill_runtime(df, median_runtime):
df['runtime_is_median'] = 0
df.loc[df.runtime == 0, 'runtime_is_median'] = 1
df.loc[df.runtime.isnull() == True, 'runtime_is_median'] = 1
df.loc[df.runtime == 0, 'runtime'] = median_runtime
df.loc[df.runtime.isnull() == True, 'runtime'] = median_runtime
return df
prev_apps_count = previous_application[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
previous_application['SK_ID_PREV'] = previous_application['SK_ID_CURR'].map(prev_apps_count['SK_ID_PREV'])
 prev_apps_avg = previous_application.groupby('SK_ID_CURR' ).mean()
prev_apps_avg.columns = ['p_' + col for col in prev_apps_avg.columns]
data = data.merge(right=prev_apps_avg.reset_index() , how='left', on='SK_ID_CURR' )<merge>
rf_train = fill_runtime(rf_train, median_runtime)
 rf_test = fill_runtime(rf_test, median_runtime )
bureau_avg = bureau.groupby('SK_ID_CURR' ).mean()
bureau_avg['buro_count'] = bureau[['SK_ID_BUREAU','SK_ID_CURR']].groupby('SK_ID_CURR' ).count() ['SK_ID_BUREAU']
bureau_avg.columns = ['b_' + f_ for f_ in bureau_avg.columns]
data = data.merge(right=bureau_avg.reset_index() , how='left', on='SK_ID_CURR')
<merge>
from datetime import timedelta, date
cnt_inst = installments_payments[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
installments_payments['SK_ID_PREV'] = installments_payments['SK_ID_CURR'].map(cnt_inst['SK_ID_PREV'])
 avg_inst = installments_payments.groupby('SK_ID_CURR' ).mean()
avg_inst.columns = ['i_' + f_ for f_ in avg_inst.columns]
data = data.merge(right=avg_inst.reset_index() , how='left', on='SK_ID_CURR' )<merge>
rf_test.loc[rf_test['release_date'].isnull() == True, 'release_date'] = '10/19/2001'
test.loc[test['release_date'].isnull() == True, 'release_date'] = '10/19/2001'
pcb_count = pcb[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
pcb['SK_ID_PREV'] = pcb['SK_ID_CURR'].map(pcb_count['SK_ID_PREV'])
 pcb_avg = pcb.groupby('SK_ID_CURR' ).mean()
data = data.merge(right=pcb_avg.reset_index() , how='left', on='SK_ID_CURR' )<merge>
def add_date_features(df, col, prefix):
df[col] = pd.to_datetime(df[col])
 future = df[col] > pd.Timestamp(year=2017,month=12,day=31)
 df.loc[future, col] -= timedelta(days=365.25*100)
 df[prefix+'_day_of_week'] = df[col].dt.dayofweek
df[prefix+'_day_of_year'] = df[col].dt.dayofyear
df[prefix+'_month'] = df[col].dt.month
df[prefix+'_year'] = df[col].dt.year
df[prefix+'_day'] = df[col].dt.day
df[prefix+'_is_year_end'] = df[col].dt.is_year_end
df[prefix+'_is_year_start'] = df[col].dt.is_year_start
df[prefix+'_week'] = df[col].dt.week
df[prefix+'_quarter'] = df[col].dt.quarter
df.drop(col, axis = 1, inplace = True)
 return df
nb_prevs = credit_card_balance[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
credit_card_balance['SK_ID_PREV'] = credit_card_balance['SK_ID_CURR'].map(nb_prevs['SK_ID_PREV'])
 avg_cc_bal = credit_card_balance.groupby('SK_ID_CURR' ).mean()
avg_cc_bal.columns = ['cc_bal_' + f_ for f_ in avg_cc_bal.columns]
data = data.merge(right=avg_cc_bal.reset_index() , how='left', on='SK_ID_CURR' )<prepare_x_and_y>
rf_train = add_date_features(rf_train, 'release_date', 'release')
 rf_test = add_date_features(rf_test, 'release_date', 'release' )
ignore_features = ['SK_ID_CURR', 'is_train', 'is_test']
relevant_features = [col for col in data.columns if col not in ignore_features]
trainX = data[data['is_train'] == 1][relevant_features]
testX = data[data['is_test'] == 1][relevant_features]<split>
def get_dictionary(s):
try:
d = eval(s)
 except:
d = {}
return d
x_train, x_val, y_train, y_val = train_test_split(trainX, Y, test_size=0.2, random_state=18)
 lgb_train = lgb.Dataset(data=x_train, label=y_train)
 lgb_eval = lgb.Dataset(data=x_val, label=y_val )<train_model>
json_cols = ['production_companies', 'production_countries', 'cast', 'crew', 'Keywords', 'belongs_to_collection']
for col in json_cols:
rf_train[col] = train[col]
rf_train[col] = rf_train[col].apply(lambda x: get_dictionary(x))
rf_test[col] = test[col]
rf_test[col] = rf_test[col].apply(lambda x: get_dictionary(x))
params = {'task': 'train', 'boosting_type': 'gbdt', 'objective': 'binary', 'metric': 'auc',
'learning_rate': 0.01, 'num_leaves': 48, 'num_iteration': 5000, 'verbose': 0 ,
'colsample_bytree':.8, 'subsample':.9, 'max_depth':7, 'reg_alpha':.1, 'reg_lambda':.1,
'min_split_gain':.01, 'min_child_weight':1}
model = lgb.train(params, lgb_train, valid_sets=lgb_eval, early_stopping_rounds=150, verbose_eval=200 )<save_to_csv>
for col in json_cols:
if col == 'belongs_to_collection':
rf_train['collection_name'] = rf_train[col].apply(lambda row: row[0]['name'] if row != {} else '0')
 rf_test['collection_name'] = rf_test[col].apply(lambda row: row[0]['name'] if row != {} else '0')
 rf_combined = pd.concat([rf_train, rf_test], sort=False)
 le = LabelEncoder()
le.fit(rf_combined['collection_name'])
 rf_train['collection_name'] = le.transform(rf_train['collection_name'])
 rf_test['collection_name'] = le.transform(rf_test['collection_name'])
 rf_train[col] = rf_train[col].apply(lambda row: 0 if row is None else len(row))
rf_test[col] = rf_test[col].apply(lambda row: 0 if row is None else len(row))
rf_train['overview_wordcount'] = rf_train['overview'].str.split().str.len()
rf_train.drop('overview', axis = 1, inplace = True)
 rf_train.loc[rf_train['overview_wordcount'].isnull() == True, 'overview_wordcount'] = 0
rf_test['overview_wordcount'] = rf_test['overview'].str.split().str.len()
rf_test.drop('overview', axis = 1, inplace = True)
 rf_test.loc[rf_test['overview_wordcount'].isnull() == True, 'overview_wordcount'] = 0
rf_train['_budget_runtime_ratio'] = np.round(rf_train['budget']/rf_train['runtime'], 2)
 rf_train['_budget_year_ratio'] = np.round(rf_train['budget']/(rf_train['release_year']*rf_train['release_year']), 2)
 rf_train['_releaseYear_popularity_ratio'] = np.round(rf_train['release_year']/rf_train['popularity'], 2)
 rf_test['_budget_runtime_ratio'] = np.round(rf_test['budget']/rf_test['runtime'], 2)
 rf_test['_budget_year_ratio'] = np.round(rf_test['budget']/(rf_test['release_year']*rf_test['release_year']), 2)
 rf_test['_releaseYear_popularity_ratio'] = np.round(rf_test['release_year']/rf_test['popularity'], 2)
preds = model.predict(testX)
 sub_lgb = pd.DataFrame()
sub_lgb['SK_ID_CURR'] = test_id
sub_lgb['TARGET'] = preds
sub_lgb.to_csv("lgb_baseline.csv", index=False)
 sub_lgb.head()<load_from_csv>
X_train, X_valid, y_train, y_valid = train_test_split(
 rf_train.drop('revenue', axis = 1), rf_train['revenue'],
test_size=0.1,
random_state=42
)
train_data = pd.read_csv('.. /input/ghouls-goblins-and-ghosts-boo/train.csv.zip', compression='zip')
 test_data = pd.read_csv('.. /input/ghouls-goblins-and-ghosts-boo/test.csv.zip', compression='zip' )<split>
def rmse(y_pred, y_true):
return np.sqrt(mean_squared_error(y_pred, y_true))
def print_rf_score(model):
print(f'Train R2: {model.score(X_train, y_train)}')
 print(f'Valid R2: {model.score(X_valid, y_valid)}')
 print(f'Train RMSE: {rmse(model.predict(X_train), y_train)}')
 print(f'Valid RMSE: {rmse(model.predict(X_valid), y_valid)}' )
X = pd.get_dummies(train_data.drop(['color','type','id'], axis = 1))
Y= train_data['type']
trainid=train_data.values[:,0]
testid=test_data.values[:,0]
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.25, random_state = 0)
 X=np.array(X_train)
 Y=np.array(y_train )<train_model>
rf = RandomForestRegressor(n_jobs = -1, random_state = 42)
 rf.fit(X_train, y_train)
 print_rf_score(rf )
model=MLPClassifier(solver='lbfgs', alpha=0.00001, hidden_layer_sizes=(5,), random_state=0)
 model.fit(X,Y)
 print(model)
 y_pred=model.predict(X_test )<predict_on_test>
rf = RandomForestRegressor(
 n_estimators = 1,
max_depth = 3,
bootstrap = False,
n_jobs = -1,
random_state = 42 
)
 rf.fit(X_train, y_train)
 print_rf_score(rf )
y_pred=model.predict(X_test)
 model.score(X_test,y_test )<compute_train_metric>
export_graphviz(
 rf.estimators_[0],
out_file='tree.dot',
feature_names = X_train.columns,
rounded = True,
proportion = False,
precision = 2,
filled = True,
rotate = True 
)
 call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])
 Image(filename = 'tree.png', height = 600, width = 800 )
scores= cross_val_score(model, X, Y,cv=10)
 scores
scores.mean()<save_to_csv>
rf = RandomForestRegressor(
 n_estimators = 1,
bootstrap = False,
n_jobs = -1,
random_state = 42 
)
 rf.fit(X_train, y_train)
 print_rf_score(rf )
test=test_data.drop(['color','id'],axis=1)
 prediction=model.predict(test)
 submission=pd.DataFrame({'id':testid, 'type': prediction})
 submission.to_csv("submission.csv",index=False )<set_options>
rf = RandomForestRegressor(n_estimators = 10, n_jobs = -1, random_state = 42)
 rf.fit(X_train, y_train)
 print_rf_score(rf )
init_notebook_mode(connected=True)
 color = sns.color_palette()
sns.set_style('darkgrid')
 def ignore_warn(*args, **kwargs):
pass
warnings.warn = ignore_warn
pd.set_option('display.float_format', lambda x: '{:.3f}'.format(x))
print(os.listdir(".. /input")) 
<load_from_csv>
tree_preds = np.stack([tree.predict(X_valid)for tree in rf.estimators_])
 print(f' Individual Tree Predictions: {[np.around(tree_preds[:,0], 1)]}')
 print(f' Mean of Tree Predictions: {np.mean(tree_preds[:,0])}')
 print(f' Ground truth for sample: {y_valid[0]}' )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
%%time
train_scores_r2 = []
valid_scores_r2 = []
train_scores_rmse = []
valid_scores_rmse = []
for trees in tqdm_notebook(range(1, 100)) :
rf = RandomForestRegressor(n_estimators = trees, n_jobs = -1, random_state = 42)
 rf.fit(X_train, y_train)
 train_scores_r2.append(rf.score(X_train, y_train))
valid_scores_r2.append(rf.score(X_valid, y_valid))
train_scores_rmse.append(rmse(rf.predict(X_train), y_train))
valid_scores_rmse.append(rmse(rf.predict(X_valid), y_valid))
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<count_missing_values>
rf = RandomForestRegressor(
 n_estimators = 20,
n_jobs = -1,
oob_score = True,
random_state = 42 
)
 rf.fit(X_train, y_train)
 print_rf_score(rf)
 print(f'OOB Score: {rf.oob_score_}' )
missing = train.isnull().sum()<load_from_csv>
rf = RandomForestRegressor(
 n_estimators = 20,
min_samples_leaf = 4,
n_jobs = -1,
oob_score = True,
random_state = 42 
)
 rf.fit(X_train, y_train)
 print_rf_score(rf)
 print(f'OOB Score: {rf.oob_score_}' )
train_dummy = pd.get_dummies(pd.read_csv('.. /input/train.csv'))
train_dummy.head()<define_variables>
rf = RandomForestRegressor(
 n_estimators = 20,
min_samples_leaf = 4,
max_features = 0.3,
n_jobs = -1,
oob_score = True,
random_state = 42 
)
 rf.fit(X_train, y_train)
 print_rf_score(rf)
 print(f'OOB Score: {rf.oob_score_}' )
ghost_num = {"type": {"Ghoul": 1, "Goblin": 2, "Ghost": 3} } 
<prepare_x_and_y>
rf = RandomForestRegressor(
 n_estimators = 20,
min_samples_leaf = 4,
max_features = 0.3,
n_jobs = -1,
oob_score = True,
random_state = 42, 
)
 rf.fit(X_train, y_train)
 print_rf_score(rf)
 print(f'OOB Score: {rf.oob_score_}' )
ntrain = train.shape[0]
ntest = test.shape[0]
y = train.type.values
all_data = pd.concat(( train, test)).reset_index(drop=True)
 all_data.drop(['type'], axis=1, inplace=True)
 print("all_data size is : {}".format(all_data.shape))<drop_column>
predictions = np.expm1(rf.predict(rf_test))
submission['revenue'] = np.round(predictions)
 submission.to_csv('submission_simple_rf.csv', index = False )
all_data.drop(['color'], axis=1, inplace=True)
<feature_engineering>
def xgtrain(X_train, X_valid, y_train, y_valid):
regressor = XGBRegressor(
 n_estimators = 50000,
learning_rate = 0.001,
max_depth = 6,
subsample = 0.3,
colsample_bytree = 0.2 
)
 regressor_ = regressor.fit(
 X_train.values, y_train.values,
eval_metric = 'rmse',
eval_set = [ 
(X_train.values, y_train.values), 
(X_valid.values, y_valid.values)
 ],
verbose = 1000,
early_stopping_rounds = 500, 
)
 return regressor_
all_data["bone_soul"] = all_data["bone_length"]*all_data["has_soul"]
all_data["hair_soul"] = all_data["hair_length"]*all_data["has_soul"]
all_data["flesh_soul"] = all_data["rotting_flesh"]*all_data["has_soul"] 
<feature_engineering>
%%time
regressor_ = xgtrain(X_train, X_valid, y_train, y_valid )
all_data["bone_hair"] = all_data["bone_length"]*all_data["hair_length"]
all_data["flesh_hair"] = all_data["rotting_flesh"]*all_data["hair_length"] 
<create_dataframe>
def lgbtrain(X_train, y_train, X_valid, y_valid):
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_valid, y_valid, reference=lgb_train)
 params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': {'l2_root'},
'max_depth': 4,
'learning_rate': 0.001,
'feature_fraction': 0.3,
'bagging_fraction': 1,
'bagging_freq': 1,
}
gbm = lgb.train(
 params,
lgb_train,
valid_sets = lgb_eval,
num_boost_round=50000,
early_stopping_rounds=500,
verbose_eval = 1000 
)
 return gbm
all_data_simple = pd.DataFrame()
all_data_simple["bone_hair"] = all_data["bone_hair"]
all_data_simple["rotting_flesh"] = all_data["rotting_flesh"]
all_data_simple["bone_soul"] = all_data["bone_soul"]
all_data_simple["hair_soul"] = all_data["hair_soul"] 
<filter>
%%time
gbm = lgbtrain(X_train, y_train, X_valid, y_valid )
train[train['bone_length']>0.8]<prepare_x_and_y>
predictions = np.expm1(rf.predict(rf_test)) + np.expm1(regressor_.predict(rf_test.values)) + np.expm1(gbm.predict(rf_test.values))
predictions /= 3
X = all_data_simple[:ntrain]<split>
submission['revenue'] = np.round(predictions )
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
<choose_model_class>
submission.to_csv('submission_ensemble.csv', index = False )
<train_on_grid><EOS> 
FileLinks('.' )