Kaggle competition: TMDB Box Office Prediction
<SOS> metric: RMSLE Kaggle data source: tmdb-box-office-prediction<sort_values>
%matplotlib inline
%precision 3
na = train.isnull().sum() /len(train)
 na.sort_values(ascending=False ).head(5 )<sort_values>
train = pd.read_csv('.. /input/tmdb-box-office-prediction/train.csv')
 test = pd.read_csv('.. /input/tmdb-box-office-prediction/test.csv' )
train.loc[:,['SK_ID_CURR','TARGET','AMT_INCOME_TOTAL']].sort_values('AMT_INCOME_TOTAL', ascending=False ).head(10 )<count_values>
train.loc[train['id'] == 1336,'runtime'] = 130
train.loc[train['id'] == 2303,'runtime'] = 80
train.loc[train['id'] == 391,'runtime'] = 96
train.loc[train['id'] == 592,'runtime'] = 90
train.loc[train['id'] == 925,'runtime'] = 86
train.loc[train['id'] == 978,'runtime'] = 93
train.loc[train['id'] == 1256,'runtime'] = 92
train.loc[train['id'] == 1542,'runtime'] = 93
train.loc[train['id'] == 1875,'runtime'] = 93
train.loc[train['id'] == 2151,'runtime'] = 108
train.loc[train['id'] == 2499,'runtime'] = 86
train.loc[train['id'] == 2646,'runtime'] = 98
train.loc[train['id'] == 2786,'runtime'] = 111
train.loc[train['id'] == 2866,'runtime'] = 96
test.loc[test['id'] == 3244,'runtime'] = 93
test.loc[test['id'] == 4490,'runtime'] = 90
test.loc[test['id'] == 4633,'runtime'] = 108
test.loc[test['id'] == 6818,'runtime'] = 90
test.loc[test['id'] == 4074,'runtime'] = 103
test.loc[test['id'] == 4222,'runtime'] = 91
test.loc[test['id'] == 4431,'runtime'] = 96
test.loc[test['id'] == 5520,'runtime'] = 86
test.loc[test['id'] == 5845,'runtime'] = 83
test.loc[test['id'] == 5849,'runtime'] = 140
test.loc[test['id'] == 6210,'runtime'] = 104
test.loc[test['id'] == 6804,'runtime'] = 140
test.loc[test['id'] == 7321,'runtime'] = 87
train['DAYS_EMPLOYED'].value_counts().head()<count_values>
train_add = pd.read_csv('.. /input/tmdb-competition-additional-features/TrainAdditionalFeatures.csv')
 test_add = pd.read_csv('.. /input/tmdb-competition-additional-features/TestAdditionalFeatures.csv')
 train = pd.merge(train, train_add, how='left', on=['imdb_id'])
 test = pd.merge(test, test_add, how='left', on=['imdb_id'] )
bureau_bal.STATUS.value_counts()<categorify>
df = pd.concat([train, test] ).set_index("id" )
house_train = train.loc[:,'APARTMENTS_AVG':'EMERGENCYSTATE_MODE']
house_test = test.loc[:,'APARTMENTS_AVG':'EMERGENCYSTATE_MODE']
house_var = house_train.columns.tolist()
house_train = pd.get_dummies(house_train)
 house_test = pd.get_dummies(house_test)
 house_train.fillna(0, inplace=True)
 house_test.fillna(0, inplace=True)
 house_train.head()<categorify>
low_budget = train[train["budget"] <= 100]
pca = PCA(n_components=3)
 house_train2 = pca.fit_transform(house_train)
 house_test2 = pca.transform(house_test)
 house_train2.shape, house_test2.shape<create_dataframe>
low_budget[low_budget["revenue"] >= 100000000]
house_train2 = pd.DataFrame(house_train2, columns=['house_pc1','house_pc2','house_pc3'])
 house_test2 = pd.DataFrame(house_test, columns=['house_pc1','house_pc2','house_pc3'] )<concatenate>
df.loc[df.index == 90,'budget'] = 30000000
df.loc[df.index == 118,'budget'] = 60000000
df.loc[df.index == 149,'budget'] = 18000000
df.loc[df.index == 464,'budget'] = 20000000
df.loc[df.index == 819,'budget'] = 90000000
df.loc[df.index == 1112,'budget'] = 6000000
df.loc[df.index == 1131,'budget'] = 4300000
df.loc[df.index == 1359,'budget'] = 10000000
df.loc[df.index == 1570,'budget'] = 15800000
df.loc[df.index == 1714,'budget'] = 46000000
df.loc[df.index == 1865,'budget'] = 80000000
df.loc[df.index == 2602,'budget'] = 31000000
train = pd.concat([train.drop(columns=house_var), house_train2], axis=1)
 test = pd.concat([test.drop(columns=house_var), house_test2], axis=1)
 train.shape, test.shape<feature_engineering>
df = df.drop(["poster_path", "original_title"], axis=1 )
train['CREDIT_INCOME_PERCENT'] = train['AMT_CREDIT']/train['AMT_INCOME_TOTAL']
train['ANNUITY_INCOME_PERCENT'] = train['AMT_ANNUITY']/train['AMT_INCOME_TOTAL']
train['DAYS_EMPLOYED_PERCENT'] = train['DAYS_EMPLOYED']/train['DAYS_BIRTH']
train['INCOME_PER_PERSON'] = train['AMT_INCOME_TOTAL'] / train['CNT_FAM_MEMBERS']
train['PAYMENT_RATE'] = train['AMT_ANNUITY'] / train['AMT_CREDIT']
test['CREDIT_INCOME_PERCENT'] = test['AMT_CREDIT']/test['AMT_INCOME_TOTAL']
test['ANNUITY_INCOME_PERCENT'] = test['AMT_ANNUITY']/test['AMT_INCOME_TOTAL']
test['DAYS_EMPLOYED_PERCENT'] = test['DAYS_EMPLOYED']/test['DAYS_BIRTH']
test['INCOME_PER_PERSON'] = test['AMT_INCOME_TOTAL'] / test['CNT_FAM_MEMBERS']
test['PAYMENT_RATE'] = test['AMT_ANNUITY'] / test['AMT_CREDIT']<feature_engineering>
df["log_revenue"] = np.log1p(df["revenue"])
 df["log_budget"] = np.log1p(df["budget"] )
log_features = ['AMT_INCOME_TOTAL','AMT_CREDIT','AMT_ANNUITY','AMT_GOODS_PRICE', 'DAYS_EMPLOYED', 'DAYS_REGISTRATION']
days_features = ['DAYS_BIRTH', 'DAYS_EMPLOYED','DAYS_REGISTRATION', 'DAYS_ID_PUBLISH']
train[days_features]=-train[days_features]
test[days_features]=-test[days_features]
train[log_features]=np.log1p(train[log_features])
 test[log_features]=np.log1p(test[log_features] )<feature_engineering>
df['isbelongs_to_collectionNA'] = 1
df.loc[pd.isnull(df['belongs_to_collection']),"isbelongs_to_collectionNA"] = 0
train['na_col'] = train.isnull().sum(axis=1)
 test['na_col'] = test.isnull().sum(axis=1 )<categorify>
dict_columns = ['belongs_to_collection', 'genres', 'production_companies',
'production_countries', 'spoken_languages', 'Keywords', 'cast', 'crew']
for col in dict_columns:
df[col]=df[col].apply(lambda x: [] if pd.isna(x)else ast.literal_eval(x))
train = pd.get_dummies(train)
 test = pd.get_dummies(test)
 train_labels = train['TARGET']
train, test = train.align(test, join = 'inner', axis = 1)
 train['TARGET'] = train_labels<categorify>
def count_word_list(series):
len_max = series.apply(len ).max()
tmp = series.map(lambda x: x+["nashi"]*(len_max-len(x)))
 word_set = set(sum(list(series.values), []))
for n in range(len_max):
word_dfn = pd.get_dummies(tmp.apply(lambda x: x[n]))
word_dfn = word_dfn.reindex(word_set, axis=1 ).fillna(0 ).astype(int)
 if n==0:
word_df = word_dfn
else:
word_df = word_df + word_dfn
return word_df
safe = bureau.loc[(bureau.CREDIT_DAY_OVERDUE==0)&(bureau.CNT_CREDIT_PROLONG==0),['SK_ID_CURR','SK_ID_BUREAU']]
safe = safe.groupby('SK_ID_CURR' ).count().reset_index()
safe.columns = ['SK_ID_CURR','past_loan_np']
prob = bureau[['SK_ID_CURR','CREDIT_DAY_OVERDUE','CNT_CREDIT_PROLONG']].groupby('SK_ID_CURR' ).max()
prob.columns=['overdue_max','prolong_max']
prob=prob.reset_index()
loan_type=pd.get_dummies(bureau.CREDIT_TYPE)
 loan_type['SK_ID_CURR'] = bureau['SK_ID_CURR']
loan_type = loan_type.groupby('SK_ID_CURR' ).mean().reset_index()<categorify>
dfdic_feature = {}
_ = bureau_bal.drop(columns=['STATUS'] ).groupby('SK_ID_BUREAU' ).max().reset_index()
final_status = _.merge(bureau_bal, on=['SK_ID_BUREAU','MONTHS_BALANCE'], how='left')
 final_status = pd.get_dummies(final_status ).drop(columns='MONTHS_BALANCE')
 final_status2 = bureau[['SK_ID_CURR','SK_ID_BUREAU']].merge(final_status, on='SK_ID_BUREAU', how='left')
 final_status2.STATUS_X.fillna(1, inplace=True)
 final_status2.fillna(0, inplace=True)
 final_status2 = final_status2.drop(columns='SK_ID_BUREAU' ).groupby('SK_ID_CURR' ).mean().reset_index()
final_status2 = final_status2.drop(columns=['STATUS_0','STATUS_C'] )<drop_column>
df["genre_names"] = df["genres"].apply(lambda x : [ i["name"] for i in x] )
del bureau, bureau_bal<groupby>
df['num_genres'] = df['genres'].apply(lambda x: len(x)if x != {} else 0 )
cash_dpd = cash.loc[:,['SK_ID_CURR','SK_DPD_DEF']].groupby('SK_ID_CURR' ).agg(['max','median'] ).reset_index()
cash_dpd.columns = ['SK_ID_CURR','Cash_SK_DPD_DEF_max', 'Cash_SK_DPD_DEF_median']
card_dpd = card.loc[:,['SK_ID_CURR','SK_DPD_DEF']].groupby('SK_ID_CURR' ).agg(['max','median'] ).reset_index()
card_dpd.columns = ['SK_ID_CURR','Card_SK_DPD_DEF_max', 'Card_SK_DPD_DEF_median']<drop_column>
dfdic_feature["genre"] = count_word_list(df["genre_names"])
 dfdic_feature["genre"] = dfdic_feature["genre"].drop("TV Movie", axis=1)
 dfdic_feature["genre"].head()
del cash, card<drop_column>
n_language = df.loc[:train.index[-1], "original_language"].value_counts()
large_language = n_language[n_language>=20].index
df.loc[~df["original_language"].isin(large_language), "original_language"] = "small"
p_good = previous.loc[(previous.NAME_CONTRACT_STATUS=='Approved')|(previous.NAME_CONTRACT_STATUS=='Unused offer'), ['SK_ID_CURR','AMT_CREDIT']]
p_bad = previous.loc[(previous.NAME_CONTRACT_STATUS=='Canceled')|(previous.NAME_CONTRACT_STATUS=='Refused'), ['SK_ID_CURR','AMT_CREDIT']]
p_good = p_good.dropna()
p_good2 = p_good.groupby('SK_ID_CURR' ).sum().reset_index()
p_good2.columns = ['SK_ID_CURR','good_credit']
p_bad2 = p_bad.groupby('SK_ID_CURR' ).sum().reset_index()
p_bad2.columns = ['SK_ID_CURR','bad_credit']<feature_engineering>
df["original_language"] = df["original_language"].astype("category" )
installment['diff'] = installment.AMT_PAYMENT - installment.AMT_INSTALMENT
ins_comp = installment.dropna(subset=['AMT_PAYMENT'] ).loc[:,['SK_ID_CURR','AMT_INSTALMENT','AMT_PAYMENT','diff']].groupby('SK_ID_CURR' ).sum().reset_index()
ins_comp['repay']=ins_comp.AMT_PAYMENT / ins_comp.AMT_INSTALMENT
ins_comp = ins_comp.loc[ins_comp.AMT_INSTALMENT!=0,:]<drop_column>
df['isOriginalLanguageEng'] = 0
df.loc[ df['original_language'] == "en" ,"isOriginalLanguageEng"] = 1
del previous, installment<merge>
dfdic_feature["original_language"] = pd.get_dummies(df["original_language"])
 dfdic_feature["original_language"].head()
def merge_data(train, test, dfs):
for df in dfs:
train = train.merge(df, on='SK_ID_CURR', how='left')
 test = test.merge(df, on='SK_ID_CURR', how='left')
 return train, test
train, test = merge_data(train, test, [safe, prob, loan_type, final_status2, cash_dpd, card_dpd, p_good2, p_bad2, ins_comp] )<concatenate>
le = LabelEncoder()
df['original_language'] = le.fit_transform(df['original_language'] )
def rm_collinear(train, test):
threshold = 0.9
corr_matrix = train.corr().abs()
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool))
to_drop = [column for column in upper.columns if any(upper[column] > threshold)and column != 'SK_ID_CURR']
train = train.drop(columns = to_drop)
 test = test.drop(columns = to_drop)
 return train, test, to_drop
train, test, collin_feat = rm_collinear(train, test )<import_modules>
df["production_names"] = df["production_companies"].apply(lambda x : [ i["name"] for i in x] )
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.svm import SVC
from sklearn.metrics import roc_auc_score, accuracy_score, confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.calibration import CalibratedClassifierCV
from lightgbm import LGBMClassifier
from time import time<normalization>
tmp = count_word_list(df["production_names"] )
x_train = train.drop(columns = ['TARGET','SK_ID_CURR'])
 x_test = test.drop(columns=['SK_ID_CURR'])
 features = list(x_train.columns)
 scaler = MinMaxScaler(feature_range =(0, 1))
scaler.fit(x_train)
 x_train = scaler.transform(x_train)
 x_test = scaler.transform(x_test )<choose_model_class>
def select_top_n(df, topn=9999, nmin=2):
n_word =(df.loc[train["id"]]>0 ).sum().sort_values(ascending=False)
 smallmin = n_word[n_word<nmin].index
smalln = n_word.iloc[topn+1:].index
small = set(smallmin)| set(smalln)
 df["small"] = df[small].sum(axis=1)
 return df.drop(small, axis=1 )
gb = GradientBoostingClassifier(n_estimators=30, learning_rate=0.1, min_samples_split=4, random_state=10, verbose=1)
 ab = AdaBoostClassifier(n_estimators=300, learning_rate=1, random_state=123)
 rf0 = RandomForestClassifier(n_estimators=49, max_depth = 5, random_state=100)
 lr = LogisticRegression(C=1, random_state=100)
 nn = MLPClassifier(hidden_layer_sizes=(32,), alpha=0.01, learning_rate_init=0.01, max_iter=100, batch_size=4082)
 dt = DecisionTreeClassifier(max_features=100)
 lgb = LGBMClassifier(n_estimators=2000, learning_rate=0.02, objective='binary', reg_lambda=0.1, num_leaves=34 )<find_best_model_class>
dfdic_feature["production_companies"] = select_top_n(tmp, topn=9,nmin=2)
 dfdic_feature["production_companies"].head()
x_train_x, x_train_v, y_train_x, y_train_v = train_test_split(x_train, train_labels, test_size=0.1, random_state=168)
 def run_model_try(ml):
start = time()
model = ml
model.fit(x_train_x, y_train_x)
 train_pred = model.predict_proba(x_train_x)[:,1]
v_pred = model.predict_proba(x_train_v)[:,1]
train_auc = roc_auc_score(y_train_x, train_pred)
 v_auc = roc_auc_score(y_train_v, v_pred)
 train_acc = accuracy_score(y_train_x, model.predict(x_train_x))
v_acc = accuracy_score(y_train_v, model.predict(x_train_v))
end = time()
print("Training and validation auc: %.4f, %.4f" %(train_auc, v_auc))
print("Training and validation accuracy: %.4f, %.4f" %(train_acc, v_acc))
print("Time used: %.2f" %(end-start))
return model
def run_model_real(ml):
model = ml
model.fit(x_train, train_labels)
 return model<predict_on_test>
df["country_names"] = df["production_countries"].apply(lambda x : [ i["name"] for i in x])
 df_country = count_word_list(df["country_names"] )
model = run_model_real(lgb)
 test_pred = model.predict_proba(x_test)[:,1]<save_to_csv>
df_country =(df_country.T/df_country.sum(axis=1)).T.fillna(0 )
submit = test[['SK_ID_CURR']]
submit['TARGET'] = test_pred
submit.to_csv('lgb0825.csv', index = False )<set_options>
dfdic_feature["production_countries"] = select_top_n(df_country, topn=10,nmin=2)
 dfdic_feature["production_countries"].head()
warnings.filterwarnings('ignore' )<load_from_csv>
import datetime
app_train = pd.read_csv('.. /input/application_train.csv')
 print('training data shape: ', app_train.shape)
 app_train.head()<load_from_csv>
df[df["release_date"].isnull() ]
app_test = pd.read_csv('.. /input/application_test.csv')
 print('testing data shape: ', app_test.shape)
 app_test.head()<count_missing_values>
df.loc[3829, "release_date"] = "5/1/00"
dataset.isnull().sum()<count_values>
df["release_year"] = pd.to_datetime(df["release_date"] ).dt.year.astype(int)
 df.loc[df["release_year"]>2020, "release_year"] = df.loc[df["release_year"]>2020, "release_year"]-100
df["release_month"] = pd.to_datetime(df["release_date"] ).dt.month.astype(int)
 df["release_day"] = pd.to_datetime(df["release_date"] ).dt.day.astype(int )
dataset.dtypes.value_counts()<count_unique_values>
df["release_date"] = df.apply(lambda s: datetime.datetime(
 year=s["release_year"],month=s["release_month"],day=s["release_day"]), axis=1 )
dataset.select_dtypes('object' ).apply(pd.Series.nunique, axis = 0 )<categorify>
df["release_dayofyear"] = df["release_date"].dt.dayofyear
df["release_dayofweek"] = df["release_date"].dt.dayofweek
le = LabelEncoder()
count = 0
le_vars = []
for col in dataset:
if dataset[col].dtype == 'object':
if len(list(dataset[col].unique())) == 2:
le.fit(dataset[col])
 dataset[col] = le.transform(dataset[col])
 count += 1
le_vars.append(col)
 print('%d columns were label encoded' % count)
 print(le_vars )<categorify>
df['has_homepage'] = 1
df.loc[ pd.isnull(df['homepage']),'has_homepage'] = 0
dataset = pd.get_dummies(dataset)
 print('dataset data shape: ', dataset.shape )<feature_engineering>
df['num_Keywords'] = df['Keywords'].apply(lambda x: len(x)if x != {} else 0 )
dataset['CNT_CHILDREN_outlier'] = dataset['CNT_CHILDREN'] > 6
for i in dataset['CNT_CHILDREN']:
if i > 6:
dataset['CNT_CHILDREN'].replace({i: np.nan}, inplace = True )<feature_engineering>
df['overview_word_count'] = df['overview'].apply(lambda x: len(str(x ).split()))
df['overview_char_count'] = df['overview'].apply(lambda x: len(str(x)) )
dataset['DAYS_EMPLOYED_outlier'] = dataset['DAYS_EMPLOYED'] == 365243
dataset['DAYS_EMPLOYED'].replace({365243: np.nan}, inplace = True )<feature_engineering>
df['tagline_word_count'] = df['tagline'].apply(lambda x: len(str(x ).split()))
df['tagline_char_count'] = df['tagline'].apply(lambda x: len(str(x)))
 df['isTaglineNA'] = 0
df.loc[df['tagline'] == 0 ,"isTaglineNA"] = 1
dataset['OWN_CAR_AGE_outlier'] = dataset['OWN_CAR_AGE'] > 60
for i in dataset['OWN_CAR_AGE']:
if i > 60:
dataset['OWN_CAR_AGE'].replace({i: np.nan}, inplace = True )<define_variables>
df['title_word_count'] = df['title'].apply(lambda x: len(str(x ).split()))
df['title_char_count'] = df['title'].apply(lambda x: len(str(x)) )
print(np.nanpercentile(dataset['CNT_FAM_MEMBERS'], 99))<feature_engineering>
df['num_cast'] = df['cast'].apply(len )
dataset['CNT_FAM_MEMBERS_outlier'] = dataset['CNT_FAM_MEMBERS'] > 5
for i in dataset['CNT_FAM_MEMBERS']:
if i > 5:
dataset['CNT_FAM_MEMBERS'].replace({i: np.nan}, inplace = True )<categorify>
list_of_cast_genders = list(df['cast'].apply(lambda x: [i['gender'] for i in x] if x != {} else [] ).values)
 df['genders_0_cast'] = df['cast'].apply(lambda x: sum([1 for i in x if i['gender'] == 0]))
df['genders_1_cast'] = df['cast'].apply(lambda x: sum([1 for i in x if i['gender'] == 1]))
df['genders_2_cast'] = df['cast'].apply(lambda x: sum([1 for i in x if i['gender'] == 2]))
df[['genders_0_cast', 'genders_1_cast']] = df[['genders_0_cast', 'genders_1_cast']].fillna(df[['genders_0_cast', 'genders_1_cast']].mean() )
for i in dataset['REGION_RATING_CLIENT_W_CITY']:
if i == -1:
dataset['REGION_RATING_CLIENT_W_CITY'].replace({i: 1}, inplace = True )<feature_engineering>
df['num_crew'] = df['crew'].apply(len )
dataset['OBS_30_CNT_SOCIAL_CIRCLE_outlier'] = dataset['OBS_30_CNT_SOCIAL_CIRCLE'] > 17
for i in dataset['OBS_30_CNT_SOCIAL_CIRCLE']:
if i > 17:
dataset['OBS_30_CNT_SOCIAL_CIRCLE'].replace({i: np.nan}, inplace = True )<feature_engineering>
department_count = pd.Series(Counter([job for lst in df["crew"].apply(lambda x : [ i["department"] for i in x] ).values for job in lst]))
department_count.sort_values(ascending=False )
dataset['DEF_30_CNT_SOCIAL_CIRCLE_outlier'] = dataset['DEF_30_CNT_SOCIAL_CIRCLE'] > 5
for i in dataset['DEF_30_CNT_SOCIAL_CIRCLE']:
if i > 5:
dataset['DEF_30_CNT_SOCIAL_CIRCLE'].replace({i: np.nan}, inplace = True )<feature_engineering>
df_crew = { idx : pd.DataFrame([ [crew["department"], crew["job"], crew["name"]]
for crew in x], columns=["department", "job", "name"])
 for idx, x in df["crew"].iteritems() }
dataset['OBS_60_CNT_SOCIAL_CIRCLE_outlier'] = dataset['OBS_60_CNT_SOCIAL_CIRCLE'] > 16
for i in dataset['OBS_60_CNT_SOCIAL_CIRCLE']:
if i > 16:
dataset['OBS_60_CNT_SOCIAL_CIRCLE'].replace({i: np.nan}, inplace = True )<feature_engineering>
df_crew = pd.concat(df_crew)
 df_crew.head()
dataset['DEF_60_CNT_SOCIAL_CIRCLE_outlier'] = dataset['DEF_60_CNT_SOCIAL_CIRCLE'] > 4
for i in dataset['DEF_60_CNT_SOCIAL_CIRCLE']:
if i > 4:
dataset['DEF_60_CNT_SOCIAL_CIRCLE'].replace({i: np.nan}, inplace = True )<feature_engineering>
def select_job(list_dict, key, value):
return [ dic["name"] for dic in list_dict if dic[key]==value]
dataset['AMT_REQ_CREDIT_BUREAU_HOUR_outlier'] = dataset['AMT_REQ_CREDIT_BUREAU_HOUR'] > 1
for i in dataset['AMT_REQ_CREDIT_BUREAU_HOUR']:
if i > 1:
dataset['AMT_REQ_CREDIT_BUREAU_HOUR'].replace({i: np.nan}, inplace = True )<feature_engineering>
for department in department_count.index:
df['dep_{}_num'.format(department)] = df["crew"].apply(select_job, key="department", value=department ).apply(len )
dataset['AMT_REQ_CREDIT_BUREAU_DAY_outlier'] = dataset['AMT_REQ_CREDIT_BUREAU_DAY'] > 2
for i in dataset['AMT_REQ_CREDIT_BUREAU_DAY']:
if i > 2:
dataset['AMT_REQ_CREDIT_BUREAU_DAY'].replace({i: np.nan}, inplace = True )<feature_engineering>
df_crewname = pd.DataFrame([], index=df.index)
 for job in ["Producer", "Director", "Screenplay", "Casting", "Original Music Composer","Writer"]:
col = 'job_{}_list'.format(job)
 df[col] = df["crew"].apply(select_job, key="job", value=job)
 top_list = [m[0] for m in Counter([i for j in df[col] for i in j] ).most_common(15)]
for i in top_list:
df_crewname['{}_{}'.format(job,i)] = df[col].apply(lambda x: i in x )
dataset['AMT_REQ_CREDIT_BUREAU_WEEK_outlier'] = dataset['AMT_REQ_CREDIT_BUREAU_WEEK'] > 2
for i in dataset['AMT_REQ_CREDIT_BUREAU_WEEK']:
if i > 2:
dataset['AMT_REQ_CREDIT_BUREAU_WEEK'].replace({i: np.nan}, inplace = True )<feature_engineering>
for job in ["Sound", "Art", "Costume & Make-Up", "Camera", "Visual Effects"]:
col = 'department_{}_list'.format(job)
 df[col] = df["crew"].apply(select_job, key="department", value=job)
 top_list = [m[0] for m in Counter([i for j in df[col] for i in j] ).most_common(15)]
for i in top_list:
df_crewname['{}_{}'.format(job,i)] = df[col].apply(lambda x: i in x )
dataset['AMT_REQ_CREDIT_BUREAU_MON_outlier'] = dataset['AMT_REQ_CREDIT_BUREAU_MON'] > 5
for i in dataset['AMT_REQ_CREDIT_BUREAU_MON']:
if i > 5:
dataset['AMT_REQ_CREDIT_BUREAU_MON'].replace({i: np.nan}, inplace = True )<feature_engineering>
df['job_Animation_num'] = df["crew"].apply(select_job, key="job", value="Animation" ).apply(len )
dataset['AMT_REQ_CREDIT_BUREAU_QRT_outlier'] = dataset['AMT_REQ_CREDIT_BUREAU_QRT'] > 5
for i in dataset['AMT_REQ_CREDIT_BUREAU_QRT']:
if i > 5:
dataset['AMT_REQ_CREDIT_BUREAU_QRT'].replace({i: np.nan}, inplace = True )<feature_engineering>
df['genders_0_crew'] = df['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 0]))
df['genders_1_crew'] = df['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 1]))
df['genders_2_crew'] = df['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 2]))
df[['genders_0_crew', 'genders_1_crew','genders_2_crew']] = df[['genders_0_crew', 'genders_1_crew','genders_2_crew']].fillna(df[['genders_0_crew', 'genders_1_crew','genders_2_crew']].mean() )
dataset['AMT_REQ_CREDIT_BUREAU_YEAR_outlier'] = dataset['AMT_REQ_CREDIT_BUREAU_YEAR'] > 10
for i in dataset['AMT_REQ_CREDIT_BUREAU_YEAR']:
if i > 10:
dataset['AMT_REQ_CREDIT_BUREAU_YEAR'].replace({i: np.nan}, inplace = True )<feature_engineering>
df['budget_runtime_ratio'] = df['budget']/df['runtime']
dataset['EMPLOY_AGE'] = dataset['DAYS_EMPLOYED'] / dataset['DAYS_BIRTH']
dataset['INCOME_AGE'] = dataset['AMT_INCOME_TOTAL'] / dataset['DAYS_BIRTH']
dataset['CREDIT_AGE'] = dataset['AMT_CREDIT'] / dataset['DAYS_BIRTH']
dataset['CREDIT_INCOME'] = dataset['AMT_CREDIT'] / dataset['AMT_INCOME_TOTAL']
dataset['ANNUITY_INCOME'] = dataset['AMT_ANNUITY'] / dataset['AMT_INCOME_TOTAL']
dataset['ANNUITY_CREDIT'] = dataset['AMT_ANNUITY'] / dataset['AMT_CREDIT']<load_from_csv>
df['budget_popularity_ratio'] = df['budget']/df['popularity']
bureau = pd.read_csv('.. /input/bureau.csv')
 bureau.head()<merge>
rating_na = df[df["rating"].isnull() ]
dataset = dataset.merge(BUREAU_count, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_count'])
 print('%.4f' % corr )<groupby>
df['budget_popularity2_ratio'] = df['budget']/df['popularity2']
DAYS_CREDIT_max = bureau.groupby('SK_ID_CURR', as_index=False)['DAYS_CREDIT'].max().rename(columns = {'DAYS_CREDIT': 'bureau_DAYS_CREDIT_max'})
 DAYS_CREDIT_max.head()<merge>
df['budget_year_ratio'] = df['budget']/df['release_year']
dataset = dataset.merge(DAYS_CREDIT_max, on = 'SK_ID_CURR', how = 'left' )<groupby>
df['production_countries_count'] = df['production_countries'].apply(lambda x : len(x))
df['production_companies_count'] = df['production_companies'].apply(lambda x : len(x))
CREDIT_DAY_OVERDUE_max = bureau.groupby('SK_ID_CURR', as_index=False)['CREDIT_DAY_OVERDUE'].max().rename(columns = {'CREDIT_DAY_OVERDUE': 'bureau_CREDIT_DAY_OVERDUE_max'} )<count_values>
df["collection_name"] = df["belongs_to_collection"].apply(lambda x : x[0]["name"] if len(x)>0 else 0)
 le.fit(list(df['collection_name'].fillna('')))
 df['collection_name'] = le.transform(df['collection_name'].fillna('' ).astype(str))
CREDIT_DAY_OVERDUE_max[CREDIT_DAY_OVERDUE_max['bureau_CREDIT_DAY_OVERDUE_max'] > 0].count()<feature_engineering>
df['mean_pop1_bud'] = df.groupby('popularity')['budget'].transform('mean')
 df['mean_pop2_bud'] = df.groupby('popularity2')['budget'].transform('mean')
 df['mean_year_bud'] = df.groupby('release_year')['budget'].transform('mean')
 df['mean_pop1_rate'] = df.groupby('popularity')['rating'].transform('mean')
 df['mean_pop2_rate'] = df.groupby('popularity2')['rating'].transform('mean')
 df['mean_rate_tV'] = df.groupby('rating')['totalVotes'].transform('mean' )
CREDIT_DAY_OVERDUE_max['bureau_CREDIT_DAY_OVERDUE_max_flag'] = CREDIT_DAY_OVERDUE_max['bureau_CREDIT_DAY_OVERDUE_max'].where(CREDIT_DAY_OVERDUE_max['bureau_CREDIT_DAY_OVERDUE_max']==0,other=1 )<merge>
df['runtime_to_mean_year'] = df['runtime'] / df.groupby("release_year")["runtime"].transform('mean')
 df['popularity_to_mean_year'] = df['popularity'] / df.groupby("release_year")["popularity"].transform('mean')
 df['budget_to_mean_year'] = df['budget'] / df.groupby("release_year")["budget"].transform('mean' )
CREDIT_DAY_OVERDUE_max = CREDIT_DAY_OVERDUE_max.drop('bureau_CREDIT_DAY_OVERDUE_max', axis=1)
 dataset = dataset.merge(CREDIT_DAY_OVERDUE_max, on = 'SK_ID_CURR', how = 'left' )<feature_engineering>
df_features = pd.concat(dfdic_feature, axis=1 )
DAYS_CREDIT_ENDDATE_max['bureau_DAYS_CREDIT_ENDDATE_max_outlier'] = DAYS_CREDIT_ENDDATE_max['bureau_DAYS_CREDIT_ENDDATE_max'] < -10000
for i in DAYS_CREDIT_ENDDATE_max['bureau_DAYS_CREDIT_ENDDATE_max']:
if i < -10000:
DAYS_CREDIT_ENDDATE_max['bureau_DAYS_CREDIT_ENDDATE_max'].replace({i: np.nan}, inplace = True )<merge>
df_features.index = df.index
dataset = dataset.merge(DAYS_CREDIT_ENDDATE_max, on = 'SK_ID_CURR', how = 'left' )<feature_engineering>
df_use = df[['num_cast', 'genders_0_cast','runtime_to_mean_year','budget_to_mean_year',"log_budget",
'genders_1_cast','genders_2_cast', 'num_crew', 'genders_0_crew', 'genders_1_crew','genders_2_crew',
"tagline_word_count","overview_word_count","title_word_count","has_homepage",
'popularity','runtime','release_year', 'release_month','release_dayofweek',"num_genres"
,"popularity2","rating","totalVotes",'isOriginalLanguageEng',
'budget_runtime_ratio','budget_popularity_ratio','budget_year_ratio','budget_popularity2_ratio',
'production_countries_count','production_companies_count','mean_pop1_bud','mean_pop2_bud','mean_year_bud','mean_pop1_rate',
'mean_pop2_rate','mean_rate_tV',
'dep_Directing_num', 'dep_Writing_num', 'dep_Production_num',
'dep_Sound_num', 'dep_Camera_num', 'dep_Editing_num', 'dep_Art_num',
'dep_Costume & Make-Up_num', 'dep_Crew_num', 'dep_Lighting_num',
'dep_Visual Effects_num', 'dep_Actors_num', 'job_Animation_num' ]]
DAYS_ENDDATE_FACT_mean['bureau_DAYS_ENDDATE_FACT_mean_outlier'] = DAYS_ENDDATE_FACT_mean['bureau_DAYS_ENDDATE_FACT_mean'] < -4000
for i in DAYS_ENDDATE_FACT_mean['bureau_DAYS_ENDDATE_FACT_mean']:
if i < -4000:
DAYS_ENDDATE_FACT_mean['bureau_DAYS_ENDDATE_FACT_mean'].replace({i: np.nan}, inplace = True )<merge>
df_use = pd.concat([df_use, df_features], axis=1 )
dataset = dataset.merge(DAYS_ENDDATE_FACT_mean, on = 'SK_ID_CURR', how = 'left' )<merge>
df_use.columns = ["".join(c if c.isalnum() else "_" for c in str(x)) for x in df_use.columns]
dataset = dataset.merge(AMT_CREDIT_MAX_OVERDUE_max, on = 'SK_ID_CURR', how = 'left')
 dataset = dataset.merge(AMT_CREDIT_MAX_OVERDUE_mean, on = 'SK_ID_CURR', how = 'left' )<merge>
df_use.isnull().sum().sum()
dataset = dataset.merge(CNT_CREDIT_PROLONG_sum, on = 'SK_ID_CURR', how = 'left' )<merge>
trainX = df_use.iloc[:train.shape[0],:].reset_index(drop=True)
 test_X = df_use.iloc[train.shape[0]:,:].reset_index(drop=True)
 trainy = np.log1p(train["revenue"] )
dataset = dataset.merge(AMT_CREDIT_SUM_mean, on = 'SK_ID_CURR', how = 'left' )<merge>
X_train, X_test, y_train, y_test = train_test_split(trainX,trainy,test_size=0.3,random_state=100 )
dataset = dataset.merge(AMT_CREDIT_SUM_DEBT_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_AMT_CREDIT_SUM_DEBT_mean'])
 print('%.4f' % corr )<merge>
xgbmodel = xgb.XGBRegressor(max_depth=6,
min_child_weight=3,
alpha = 0.5,
learning_rate=0.05,
n_estimators=150,
objective='reg:linear',
gamma=0.01,
silent=1,
subsample=0.8,
colsample_bytree=0.8 )
dataset = dataset.merge(AMT_CREDIT_SUM_LIMIT_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_AMT_CREDIT_SUM_LIMIT_mean'])
 print('%.4f' % corr )<merge>
xgbmodel.fit(X_train, y_train )
dataset = dataset.merge(AMT_CREDIT_SUM_OVERDUE_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_AMT_CREDIT_SUM_OVERDUE_mean'])
 print('%.4f' % corr )<merge>
pred_train1 = xgbmodel.predict(X_train)
 pred_test1 = xgbmodel.predict(X_test )
dataset = dataset.merge(DAYS_CREDIT_UPDATE_max, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_DAYS_CREDIT_UPDATE_max'])
 print('%.4f' % corr )<merge>
print(np.sqrt(mean_squared_error(y_train, pred_train1)))
 print(np.sqrt(mean_squared_error(y_test, pred_test1)) )
dataset = dataset.merge(AMT_ANNUITY_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_AMT_ANNUITY_mean'])
 print('%.4f' % corr )<categorify>
pred_xgb = xgbmodel.predict(test_X )
bureau_cats = pd.get_dummies(bureau.select_dtypes('object'))
bureau_cats['SK_ID_CURR'] = bureau['SK_ID_CURR']
bureau_cats.head()<groupby>
test_id = test["id"]
bureau_cats_grouped = bureau_cats.groupby('SK_ID_CURR' ).agg('sum')
 bureau_cats_grouped.head()<merge>
pred_xgb = pd.DataFrame(np.exp(pred_xgb)-1,columns=["revenue"])
 pred_xgb
dataset = dataset.merge(bureau_cats_grouped, on = 'SK_ID_CURR', right_index = True, how = 'left')
 dataset.head()<load_from_csv>
sub=pd.concat([test_id, pred_xgb],axis=1 )
bureau_balance = pd.read_csv('.. /input/bureau_balance.csv')
 bureau_balance.head()<merge>
sub.to_csv('TMDB_xgb.csv',index=False )
MONTHS_BAL = MONTHS_BALANCE_mean.merge(MONTHS_BALANCE_count, on = 'SK_ID_BUREAU', right_index = True, how = 'inner')
 MONTHS_BAL.head()<categorify>
lgbmodel = lgb.LGBMRegressor(n_estimators=150,
objective='regression',
metric='rmse',
max_depth = 5,
num_leaves=30,
min_child_samples=30,
learning_rate=0.05,
boosting = 'gbdt',
min_data_in_leaf= 15,
feature_fraction = 0.9,
bagging_freq = 1,
bagging_fraction = 0.9,
importance_type='gain',
lambda_l1 = 0.2,
subsample=.8,
colsample_bytree=.8,
use_best_model=True )
bureau_bal_cats = pd.get_dummies(bureau_balance.select_dtypes('object'))
bureau_bal_cats['SK_ID_BUREAU'] = bureau_balance['SK_ID_BUREAU']
bureau_bal_cats.head()<groupby>
lgbmodel.fit(X_train, y_train )
bureau_bal_cats_grouped = bureau_bal_cats.groupby('SK_ID_BUREAU' ).agg('sum')
 bureau_bal_cats_grouped.head()<merge>
pred_train2 = lgbmodel.predict(X_train)
 pred_test2 = lgbmodel.predict(X_test )
bureau_bal_merged = MONTHS_BAL.merge(bureau_bal_cats_grouped, right_index = True, left_on = 'SK_ID_BUREAU', how = 'outer')
 bureau_bal_merged = bureau_bal_merged.merge(bureau[['SK_ID_BUREAU', 'SK_ID_CURR']], on = 'SK_ID_BUREAU', how = 'left')
 bureau_bal_merged.head()<merge>
print(np.sqrt(mean_squared_error(y_train, pred_train2)))
 print(np.sqrt(mean_squared_error(y_test, pred_test2)) )
bureau_bal_MONTHS_BALANCE_mean_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['bureau_bal_MONTHS_BALANCE_mean'].mean().rename(columns = {'bureau_bal_MONTHS_BALANCE_mean': 'bureau_bal_MONTHS_BALANCE_mean_mean'})
 dataset = dataset.merge(bureau_bal_MONTHS_BALANCE_mean_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_MONTHS_BALANCE_mean_mean'])
 print('%.4f' % corr )<merge>
pred_lgb = lgbmodel.predict(test_X )
bureau_bal_MONTHS_BALANCE_count_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['bureau_bal_MONTHS_BALANCE_count'].mean().rename(columns = {'bureau_bal_MONTHS_BALANCE_count': 'bureau_bal_MONTHS_BALANCE_count_mean'})
 dataset = dataset.merge(bureau_bal_MONTHS_BALANCE_count_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_MONTHS_BALANCE_count_mean'])
 print('%.4f' % corr )<merge>
pred_lgb = pd.DataFrame(np.exp(pred_lgb)-1,columns=["revenue"])
 pred_lgb
bureau_bal_STATUS_0_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_0'].mean().rename(columns = {'STATUS_0': 'bureau_bal_STATUS_0_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_0_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_0_mean'])
 print('%.4f' % corr )<merge>
sub1=pd.concat([test_id, pred_lgb],axis=1 )
bureau_bal_STATUS_1_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_1'].mean().rename(columns = {'STATUS_1': 'bureau_bal_STATUS_1_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_1_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_1_mean'])
 print('%.4f' % corr )<merge>
sub1.to_csv('TMDB_lgb.csv',index=False )
bureau_bal_STATUS_2_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_2'].mean().rename(columns = {'STATUS_2': 'bureau_bal_STATUS_2_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_2_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_2_mean'])
 print('%.4f' % corr )<merge>
catmodel = cat.CatBoostRegressor(iterations=2000,
learning_rate=0.01,
depth=8,
eval_metric='RMSE',
colsample_bylevel=0.8,
bagging_temperature = 0.2,
metric_period = None,
early_stopping_rounds=200 )
bureau_bal_STATUS_3_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_3'].mean().rename(columns = {'STATUS_3': 'bureau_bal_STATUS_3_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_3_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_3_mean'])
 print('%.4f' % corr )<merge>
catmodel.fit(X_train, y_train )
bureau_bal_STATUS_4_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_4'].mean().rename(columns = {'STATUS_4': 'bureau_bal_STATUS_4_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_4_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_4_mean'])
 print('%.4f' % corr )<merge>
pred_train3 = catmodel.predict(X_train)
 pred_test3 = catmodel.predict(X_test )
bureau_bal_STATUS_5_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_5'].mean().rename(columns = {'STATUS_5': 'bureau_bal_STATUS_5_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_5_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_5_mean'])
 print('%.4f' % corr )<merge>
print(np.sqrt(mean_squared_error(y_train, pred_train3)))
 print(np.sqrt(mean_squared_error(y_test, pred_test3)) )
bureau_bal_STATUS_C_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_C'].mean().rename(columns = {'STATUS_C': 'bureau_bal_STATUS_C_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_C_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_C_mean'])
 print('%.4f' % corr )<merge>
pred_cat = catmodel.predict(test_X )
bureau_bal_STATUS_X_mean = bureau_bal_merged.groupby('SK_ID_CURR', as_index=False)['STATUS_X'].mean().rename(columns = {'STATUS_X': 'bureau_bal_STATUS_X_mean'})
 dataset = dataset.merge(bureau_bal_STATUS_X_mean, on = 'SK_ID_CURR', how = 'left')
 corr = dataset['TARGET'].corr(dataset['bureau_bal_STATUS_X_mean'])
 print('%.4f' % corr )<drop_column>
pred_cat = pd.DataFrame(np.exp(pred_cat)-1,columns=["revenue"])
 pred_cat
gc.enable()
del bureau, BUREAU_count, DAYS_CREDIT_max, CREDIT_DAY_OVERDUE_max, DAYS_CREDIT_ENDDATE_max, DAYS_ENDDATE_FACT_mean, AMT_CREDIT_MAX_OVERDUE_max,
AMT_CREDIT_MAX_OVERDUE_mean, CNT_CREDIT_PROLONG_sum, AMT_CREDIT_SUM_mean, AMT_CREDIT_SUM_DEBT_mean, AMT_CREDIT_SUM_LIMIT_mean, AMT_CREDIT_SUM_OVERDUE_mean,
DAYS_CREDIT_UPDATE_max, AMT_ANNUITY_mean, bureau_cats, bureau_cats_grouped, bureau_balance, MONTHS_BALANCE_count, MONTHS_BALANCE_mean, MONTHS_BAL, bureau_bal_cats,
bureau_bal_cats_grouped, bureau_bal_merged, bureau_bal_MONTHS_BALANCE_mean_mean, bureau_bal_MONTHS_BALANCE_count_mean, bureau_bal_STATUS_0_mean,
bureau_bal_STATUS_1_mean, bureau_bal_STATUS_2_mean, bureau_bal_STATUS_3_mean, bureau_bal_STATUS_4_mean, bureau_bal_STATUS_5_mean, bureau_bal_STATUS_C_mean,
bureau_bal_STATUS_X_mean
gc.collect()<load_from_csv>
sub2=pd.concat([test_id, pred_cat],axis=1 )
credit = pd.read_csv('.. /input/credit_card_balance.csv')
 credit.head()<drop_column>
sub2.to_csv('TMDB_cat.csv',index=False )
credit_stats_by_prev = credit[['SK_ID_PREV', 'SK_ID_CURR']]<groupby>
ansamble = 0.4 * pred_lgb["revenue"] + 0.2 * pred_xgb["revenue"] + 0.4 * pred_cat["revenue"]
credit_MONTHS_BALANCE_count = credit.groupby('SK_ID_PREV', as_index=False)['MONTHS_BALANCE'].count().rename(columns = {'MONTHS_BALANCE': 'credit_MONTHS_BALANCE_count'})
 credit_MONTHS_BALANCE_mean = credit.groupby('SK_ID_PREV', as_index=False)['MONTHS_BALANCE'].mean().rename(columns = {'MONTHS_BALANCE': 'credit_MONTHS_BALANCE_mean'} )<merge>
sub3=pd.concat([test_id, ansamble],axis=1 )
credit_stats_by_prev = credit_stats_by_prev.merge(credit_MONTHS_BALANCE_count, on = 'SK_ID_PREV', how = 'left')
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_MONTHS_BALANCE_mean, on = 'SK_ID_PREV', how = 'left')
 credit_stats_by_prev.head()<set_options>
sub3.to_csv('TMDB_ansamble.csv',index=False )
gc.enable()
del credit_MONTHS_BALANCE_count, credit_MONTHS_BALANCE_mean
gc.collect()<merge>
ansamble2 = 0.35 * pred_lgb["revenue"] + 0.3 * pred_xgb["revenue"] + 0.35 * pred_cat["revenue"]
credit_AMT_BALANCE_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_BALANCE'].mean().rename(columns = {'AMT_BALANCE': 'credit_AMT_BALANCE_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_BALANCE_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_BALANCE_mean
gc.collect()<merge>
sub4=pd.concat([test_id, ansamble2],axis=1 )
credit_AMT_CREDIT_LIMIT_ACTUAL_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_CREDIT_LIMIT_ACTUAL'].mean().rename(columns = {'AMT_CREDIT_LIMIT_ACTUAL': 'credit_AMT_CREDIT_LIMIT_ACTUAL_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_CREDIT_LIMIT_ACTUAL_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_CREDIT_LIMIT_ACTUAL_mean
gc.collect()<merge>
sub4.to_csv('TMDB_ansamble2.csv',index=False )
credit_AMT_DRAWINGS_ATM_CURRENT_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_DRAWINGS_ATM_CURRENT'].mean().rename(columns = {'AMT_DRAWINGS_ATM_CURRENT': 'credit_AMT_DRAWINGS_ATM_CURRENT_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_DRAWINGS_ATM_CURRENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_DRAWINGS_ATM_CURRENT_mean
gc.collect()<merge>
ansamble3 = 0.25 * pred_lgb["revenue"] + 0.25 * pred_xgb["revenue"] + 0.5 * pred_cat["revenue"]
credit_AMT_DRAWINGS_CURRENT_mean = credit.groupby('SK_ID_PREV', as_index=False)['AMT_DRAWINGS_CURRENT'].mean().rename(columns = {'AMT_DRAWINGS_CURRENT': 'credit_AMT_DRAWINGS_CURRENT_mean'})
 credit_stats_by_prev = credit_stats_by_prev.merge(credit_AMT_DRAWINGS_CURRENT_mean, on = 'SK_ID_PREV', how = 'left')
 gc.enable()
del credit_AMT_DRAWINGS_CURRENT_mean
gc.collect()<merge>
sub5=pd.concat([test_id, ansamble3],axis=1 )
<merge><EOS> 
sub5.to_csv('TMDB_ansamble3.csv',index=False )