Kaggle competition: TMDB Box Office Prediction
pd.set_option('display.max_columns', None)
 y_preds = y_val_pred_prob[:,1]
results = pd.DataFrame({'target_true':target_val , 'target_pred': y_preds})
 b = results.nlargest(10, columns='target_pred')
 b2 = results.nlargest(1000, columns='target_pred')
 c = results.nsmallest(1000, columns='target_pred')
 c_counts_1 =(c['target_true']==1 ).sum()
b2_counts_1 =(b2['target_true']==1 ).sum()
print('Numeber of 1s in 1000 smallest probabilities:', c_counts_1)
 print('Number of 1s in 1000 largest probabilities:', b2_counts_1)
 display(b)
 display(val.loc[b.index] )<filter>
%matplotlib inline
%precision 3
pd.set_option('precision', 3)
 warnings.filterwarnings('ignore' )
print(imp_features_10large)
 display(val.loc[b.index, imp_features_10large.feature] )<split>
train = pd.read_csv('.. /input/tmdb-box-office-prediction/train.csv')
 test = pd.read_csv('.. /input/tmdb-box-office-prediction/test.csv' )
features = X_train.columns.tolist()
cat_car_05 = [c for c in features if 'car_05_cat' in c]
calc_features = [c for c in features if 'calc' in c]
X_train.drop(calc_features, axis=1, inplace=True)
 X_train.drop(cat_car_05, axis=1, inplace=True)
 X_train['ps_reg_03'].replace(-1, X_train['ps_reg_03'].median() , inplace=True)
 X_train['ps_car_14'].replace(-1, X_train['ps_car_14'].median() , inplace=True)
 scores_gini = []
for train_index, validation_index in cv.split(X_train, y_train):
train, val = X_train.iloc[train_index], X_train.iloc[validation_index]
target_train, target_val = y_train.iloc[train_index], y_train.iloc[validation_index]
clf = LGBMClassifier(random_state=42, n_jobs=-1)
 clf.set_params(**best_params)
 clf.fit(train, target_train)
 y_val_pred = clf.predict(val)
 y_val_pred_prob = clf.predict_proba(val)
 gini_score = gini(target_val, y_val_pred_prob[:, 1])
 print('gini score:', gini_score)
 scores_gini.append(gini_score)
 print('mean gini_score: {}'.format(np.mean(scores_gini)) )<create_dataframe>
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
y_preds = y_val_pred_prob[:,1]
results = pd.DataFrame({'target_true':target_val , 'target_pred': y_preds})
 b = results.nlargest(10, columns='target_pred')
 b2 = results.nlargest(1000, columns='target_pred')
 c = results.nsmallest(1000, columns='target_pred')
 c_counts_1 =(c['target_true']==1 ).sum()
b2_counts_1 =(b2['target_true']==1 ).sum()
print('Numeber of 1s in 1000 smallest probabilities:', c_counts_1)
 print('Number of 1s in 1000 largest probabilities:', b2_counts_1)
<load_from_csv>
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
df_test = pd.read_csv('.. /input/porto-seguro-safe-driver-prediction/test.csv')
 X_test = df_test.drop('id', axis=1)
 X_test = pd.get_dummies(X_test, columns=cat_features, prefix_sep='_', drop_first=True)
 X_test.drop(calc_features, axis=1, inplace=True)
 X_test.drop(cat_car_05, axis=1, inplace=True)
 X_test['ps_reg_03'].replace(-1, X_test['ps_reg_03'].median() , inplace=True)
 X_test['ps_car_14'].replace(-1, X_test['ps_car_14'].median() , inplace=True)
 assert(X_train.shape[1]==X_test.shape[1])
 clf.fit(X_train, y_train )<save_to_csv>
df = pd.concat([train, test] ).set_index("id" )
preds = clf.predict_proba(X_test)[:,1]
df_subm = pd.read_csv('.. /input/porto-seguro-safe-driver-prediction/sample_submission.csv')
 df_subm.loc[:,'target'] = preds
display(df_subm)
 df_subm.to_csv('submission.csv', index=False)
<import_modules>
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
import pandas as pd
import numpy as np
import lightgbm as lgb
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score<prepare_x_and_y>
print(df.columns)
 df = df.drop(["poster_path", "status", "original_title"], axis=1 )
df_train = pd.read_csv('.. /input/train.csv', na_values="-1")
 df_test = pd.read_csv('.. /input/test.csv', na_values="-1")
 y_train = df_train['target']
id_test = df_test['id'].values
nan_count_train = df_train.isnull().sum(axis=1 ).tolist()
nan_count_test = df_test.isnull().sum(axis=1 ).tolist()
calc_retr = ['ps_calc_01']
drop_col = [col for col in df_train.columns if '_calc' in col and col not in calc_retr]
drop_col.extend(["id", "ps_car_05_cat"])
 print(drop_col)
 df_train.drop(drop_col, axis=1, inplace=True)
 df_train.drop(['target'], axis=1, inplace=True)
 df_test.drop(drop_col, axis=1, inplace=True)
 df_train['nan_count'] = nan_count_train
df_test['nan_count'] = nan_count_test<compute_test_metric>
df["log_revenue"] = np.log10(df["revenue"])
 df['has_homepage'] = 1
df.loc[pd.isnull(df['homepage']),"has_homepage"] = 0
def gini(y, pred):
g = np.asarray(np.c_[y, pred, np.arange(len(y)) ], dtype=np.float)
 g = g[np.lexsort(( g[:,2], -1*g[:,1])) ]
gs = g[:,0].cumsum().sum() / g[:,0].sum()
gs -=(len(y)+ 1)/ 2.
 return gs / len(y)
 def gini_normalized(actual, predictions):
return gini(actual, predictions)/ gini(actual, actual)
 def gini_lgb(preds, dtrain):
y = list(dtrain.get_label())
 score = gini(y, preds)/ gini(y, y)
 return 'gini', score, True
scorer_gini = make_scorer(gini_normalized, greater_is_better=True, needs_proba=True )<categorify>
dfdic_feature = {}
bin_cols = [col for col in df_train.columns if 'bin' in col]
cat_cols = [col for col in df_train.columns if '_cat' in col]
con_cols = [col for col in df_train.columns if col not in bin_cols + cat_cols]
print(cat_cols)
 for col in bin_cols:
df_train[col].fillna(value=df_train[col].mode() [0], inplace=True)
 df_test[col].fillna(value=df_test[col].mode() [0], inplace=True)
 for col in con_cols:
df_train[col].fillna(value=df_train[col].mean() , inplace=True)
 df_test[col].fillna(value=df_test[col].mean() , inplace=True)
 def make_dummies(columns):
global df_train
global df_test
for i in columns:
dummies = pd.get_dummies(df_train[i], prefix=i)
 df_train = pd.concat([df_train, dummies], axis=1)
 df_train.drop(i, axis=1, inplace=True)
 dummies = pd.get_dummies(df_test[i], prefix=i)
 df_test = pd.concat([df_test, dummies], axis=1)
 df_test.drop(i, axis=1, inplace=True)
 make_dummies(cat_cols )<feature_engineering>
%%time
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))
print(df_train.shape)
 print(df_test.shape )<train_model>
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
part1 = df_train[:297606]
part1_y = y_train[:297606]
part2 = df_train[297606:]
part2_y = y_train[297606:]
lr= LogisticRegression(C=1.5)
 lr.fit(df_train, y_train)
 predictions_lr = lr.predict_proba(df_test)
 lr1= LogisticRegression(C=1.5)
 lr1.fit(part1, part1_y)
 lr2= LogisticRegression(C=1.5)
 lr2.fit(part2, part2_y)
 predictions_lr2 = lr2.predict_proba(part1)
<normalization>
df["genre_names"] = df["genres"].apply(lambda x : [ i["name"] for i in x])
print(df_train.shape)
 print(df_test.shape)
 indexes_train = df_train.axes
indexes_test = df_test.axes
scaler_tr = StandardScaler()
X_train = pd.DataFrame(scaler_tr.fit_transform(df_train, y_train), index=indexes_train[0], columns=indexes_train[1])
 X_test = pd.DataFrame(scaler_tr.transform(df_test), index=indexes_test[0], columns=indexes_test[1])
 print(X_train.shape)
 print(X_test.shape)
 X_train.head()<predict_on_test>
dfdic_feature["genre"] = count_word_list(df["genre_names"])
 dfdic_feature["genre"] = dfdic_feature["genre"].drop("TV Movie", axis=1)
 dfdic_feature["genre"].head()
part1 = X_train[:297606]
part1_y = y_train[:297606]
part2 = X_train[297606:]
part2_y = y_train[297606:]
k2 = KMeans(n_clusters=6, n_jobs=2)
 k2.fit(X_train)
 predictions_k2 = k2.predict(X_test)[None].T
print(predictions_k2.shape)
 k2_1 = KMeans(n_clusters=6, n_jobs=2)
 k2_1.fit(part1)
 predictions_k2_1 = k2_1.predict(part2)[None].T
k2_2 = KMeans(n_clusters=6, n_jobs=2)
 k2_2.fit(part2)
 predictions_k2_2 = k2_2.predict(part1)[None].T
X_test['k2'] = predictions_k2
pred_train_km = np.append(predictions_k2_1, predictions_k2_2)
 print(pred_train_km.shape)
 X_train['k2'] = pred_train_km
dummies = pd.get_dummies(X_train['k2'], prefix='k2')
 X_train = pd.concat([X_train, dummies], axis=1)
 X_train.drop(['k2'], axis=1, inplace=True)
 dummies = pd.get_dummies(X_test['k2'], prefix='k2')
 X_test = pd.concat([X_test, dummies], axis=1)
 X_test.drop(['k2'], axis=1, inplace=True)
 print(X_train.shape)
 print(X_test.shape)
 X_train.head()<train_on_grid>
n_language = df.loc[:train.index[-1], "original_language"].value_counts()
large_language = n_language[n_language>=10].index
df.loc[~df["original_language"].isin(large_language), "original_language"] = "small"
params={'learning_rate':0.01,'max_depth': 6,'boosting':'gbdt','objective':'binary','metric':'auc',
'is_training_metric': False,'seed': 99,'feature_fraction': 0.3,'bagging_fraction':0.9,
'bagging_freq':10,'min_data':600,'num_threads': 2,'scale_pos_weight': 1.1}
x1, x2, y1, y2 = train_test_split(X_train, y_train, test_size=0.25, random_state=99 )<train_model>
df["original_language"] = df["original_language"].astype("category" )
lgb1=lgb.train(params,lgb.Dataset(x1,label=y1),3000,lgb.Dataset(x2,label=y2),verbose_eval=100,
feval=gini_lgb,early_stopping_rounds=200 )<predict_on_test>
predictions_lgb = lgb1.predict(X_test, num_iteration=lgb1.best_iteration )<prepare_output>
df["production_names"] = df["production_companies"].apply(lambda x : [ i["name"] for i in x])
print(check_output(["ls", ".. /input"] ).decode("utf8"))
sub1 = pd.DataFrame()
sub1['id'] = id_test
sub1['target'] = predictions_lgb
print(sub1.head() )<save_to_csv>
tmp = count_word_list(df["production_names"] )
sub1.to_csv('lgb1.csv', index=False, header=True )<load_from_csv>
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 )
train_data=pd.read_csv("/kaggle/input/forest-cover-type-prediction/train.csv")
 train_data.head()<load_from_csv>
df["country_names"] = df["production_countries"].apply(lambda x : [ i["name"] for i in x])
 df_country = count_word_list(df["country_names"] )
test_data=pd.read_csv("/kaggle/input/forest-cover-type-prediction/test.csv" )<count_values>
df_country =(df_country.T/df_country.sum(axis=1)).T.fillna(0 )
train_data['Cover_Type'].value_counts()<drop_column>
x=train_data.drop(["Id","Cover_Type"],axis=1 )<prepare_x_and_y>
df["keyword_list"] = df["Keywords"].apply(lambda x : [ i["name"] for i in x] )
y=train_data['Cover_Type']<import_modules>
df["num_Keywords"] = df["keyword_list"].apply(len )
from sklearn.model_selection import train_test_split<import_modules>
df_lang = pd.DataFrame(df['spoken_languages'])
 list_lang_names = list(df_lang['spoken_languages'].apply(lambda x: [i['name'] for i in x] if x != {} else [] ).values)
 df_lang['num_spoken_languages'] = df_lang['spoken_languages'].apply(lambda x: len(x)if x != {} else 0)
 df_lang['all_spoken_languages'] = df_lang['spoken_languages'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 top_lang_names = [m[0] for m in Counter([i for j in list_lang_names for i in j] ).most_common(15)]
for g in top_lang_names:
df_lang[g] = df_lang['all_spoken_languages'].apply(lambda x: 1 if g in x else 0 )
from sklearn.model_selection import train_test_split<split>
df_lang.rename(columns ={'English':'spoken_en',
'Français':'spoken_fr',
'Español':'spoken_es',
'Deutsch':'spoken_gr',
'Pусский':'spoken_sv',
'Italiano':'spoken_it',
'日本語':'spoken_ja',
'普通话':'spoken_ch1',
'हिन्दी':'spoken_in',
'':'spoken_unknown',
'العربية':'spoken_arb',
'Português':'spoken_por',
'广州话 / 廣州話':'spoken_ch2',
'한국어/조선말':'spoken_kr',
'Polski':'spoken_pol'}, inplace =True )
x_train,x_val,y_train,y_val=train_test_split(x,y,random_state=40 )<import_modules>
df_lang.drop(columns=['spoken_languages', 'all_spoken_languages'], inplace = True )
from sklearn.ensemble import RandomForestClassifier<train_model>
df["language_names"] = df["spoken_languages"].apply(lambda x : [ i["name"] for i in x])
 df["n_language"] = df["language_names"].apply(len)
 df.loc[df["n_language"]==0, "n_language"] = 1
rfc=RandomForestClassifier(n_estimators=70)
 rfc.fit(x_train,y_train )<compute_test_metric>
df["speak_English"] = df["language_names"].apply(lambda x : "English" in x )
rfc.score(x_val,y_val )<predict_on_test>
df['speak_English'] = pd.get_dummies(df['speak_English'] )
predict=rfc.predict(test_data.drop("Id",axis=1))<create_dataframe>
df[['release_month','release_day','release_year']]=df['release_date'].str.split('/',expand=True ).replace(np.nan, -1 ).astype(int)
 df.loc[(df['release_year'] <= 19)&(df['release_year'] < 100), "release_year"] += 2000
df.loc[(df['release_year'] > 19)&(df['release_year'] < 100), "release_year"] += 1900
releaseDate = pd.to_datetime(df['release_date'])
 df['release_dayofweek'] = releaseDate.dt.dayofweek
df['release_quarter'] = releaseDate.dt.quarter
submission=pd.DataFrame(data=predict,columns=['Cover_Type'] )<prepare_output>
submission["Id"]=test_data["Id"]
submission.set_index("Id",inplace=True )<save_to_csv>
df["collection_name"] = df["belongs_to_collection"].apply(lambda x : x[0]["name"] if len(x)>0 else "nashi")
submission.to_csv("Submission.csv" )<load_from_csv>
df["count_collection"] = df["collection_name"].apply(lambda x :(df["collection_name"]==x ).sum())
 df.loc[df["collection_name"]=="nashi", "count_collection"] = 0
train_dataset=pd.read_csv('/kaggle/input/forest-cover-type-prediction/train.csv')
 test_dataset=pd.read_csv('/kaggle/input/forest-cover-type-prediction/test.csv' )<count_missing_values>
df["number_in_collection"] = df.sort_values("release_date" ).groupby("collection_name" ).cumcount() +1
df.loc[df["collection_name"]=="nashi", "number_in_collection"] = 0
train_dataset.isnull().sum()<count_missing_values>
df["collection_av_logrevenue"] = [ df.loc[(df["collection_name"]==row["collection_name"])& 
(df["number_in_collection"]<row["number_in_collection"]),
"log_revenue"].mean()
for key,row in df.iterrows() ]
test_dataset.isnull().sum()<prepare_x_and_y>
df.loc[df["collection_name"]=="nashi", "collection_av_logrevenue"] = df.loc[df["collection_name"]=="nashi", "log_revenue"].mean()
X=train_dataset.drop(labels=['Id','Cover_Type'],axis=1)
 y=train_dataset['Cover_Type']<import_modules>
collection_mean = df.loc[df["collection_name"]!="nashi", "log_revenue"].mean()
df["collection_av_logrevenue"] = df["collection_av_logrevenue"].fillna(collection_mean)
from sklearn.model_selection import train_test_split<import_modules>
df_features = pd.concat(dfdic_feature, axis=1 )
from sklearn.model_selection import train_test_split<split>
df[["original_language", "collection_name"]] = df[["original_language", "collection_name"]].astype("category" )
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=40 )<train_model>
df_use = pd.get_dummies(df_use )
classifier = RandomForestClassifier(n_estimators = 100, criterion = 'entropy', random_state = 0)
 classifier.fit(X_train, y_train)
 y_pred = classifier.predict(X_test)
 acc=accuracy_score(y_test, y_pred)
 print(acc)
<predict_on_test>
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_add.head()
predict=classifier.predict(test_dataset.drop(labels=['Id'],axis=1))
Submission=pd.DataFrame(data=predict,columns=['Cover_Type'])
 Submission.head()<prepare_output>
df = pd.merge(df, pd.concat([train_add, test_add]), on="imdb_id", how="left" )
Submission['Id']=test_dataset['Id']
Submission.set_index('Id',inplace=True )<save_to_csv>
add_cols = ["popularity2", "rating", "totalVotes"]
df[add_cols] = df[add_cols].fillna(df[add_cols].mean() )
Submission.to_csv('Submission.csv' )<install_modules>
train2 = pd.read_csv('.. /input/tmdb-box-office-prediction-more-training-data/additionalTrainData.csv')
 train3 = pd.read_csv('.. /input/tmdb-box-office-prediction-more-training-data/trainV3.csv')
 train3.head()
!pip install seaborn<import_modules>
def lower_text(text):
return text.lower()
def remove_punct(text):
text = text.replace('-', ' ')
 table=str.maketrans('','',string.punctuation)
 return text.translate(table)
 def remove_stopwords(words, stopwords):
words = [word for word in words if word not in stopwords]
return words
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler, StandardScaler
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from xgboost import XGBClassifier<load_from_csv>
submission_sample = pd.read_csv('.. /input/forest-cover-type-prediction/sampleSubmission.csv')
 train = pd.read_csv('.. /input/forest-cover-type-prediction/train.csv')
 test = pd.read_csv('.. /input/forest-cover-type-prediction/test.csv' )<count_unique_values>
no_english_overview_id = [157, 2863, 4616]
no_english_tagline_id = [3255, 3777, 4937]
train.nunique()<prepare_output>
df['overview_word_count'] = df['overview'].apply(lambda x: len(str(x ).split()))
x = train.iloc[:,15:55]
y = train.iloc[:,11:15]
y = pd.DataFrame(y)
 x = pd.DataFrame(x)
 s2 = pd.Series(x.columns[np.where(x!=0)[1]])
 s3 = pd.Series(y.columns[np.where(y!=0)[1]])
 train['soil_type'] = s2
train['Wilderness_Area'] = s3
train.head()<drop_column>
df['_overview']=df['overview'].apply(lambda x : str(x)
 ).apply(lambda x : lower_text(x)).apply(lambda x : remove_punct(x))
df_viz = train.iloc[:, 0:15]
df_viz = df_viz.drop(['Wilderness_Area1', 'Wilderness_Area2', 'Wilderness_Area3',
'Wilderness_Area4'], axis = 1)
 df_viz.head()<feature_engineering>
df['tagline_word_count'] = df['tagline'].apply(lambda x: len(str(x ).split()))
def add_feature(data):
data['Ele_minus_VDtHyd'] = data.Elevation-data.Vertical_Distance_To_Hydrology
data['Ele_plus_VDtHyd'] = data.Elevation+data.Vertical_Distance_To_Hydrology
data['Distanse_to_Hydrolody'] =(data['Horizontal_Distance_To_Hydrology']**2+data['Vertical_Distance_To_Hydrology']**2)**0.5
data['Hydro_plus_Fire'] = data['Horizontal_Distance_To_Hydrology']+data['Horizontal_Distance_To_Fire_Points']
data['Hydro_minus_Fire'] = data['Horizontal_Distance_To_Hydrology']-data['Horizontal_Distance_To_Fire_Points']
data['Hydro_plus_Road'] = data['Horizontal_Distance_To_Hydrology']+data['Horizontal_Distance_To_Roadways']
data['Hydro_minus_Road'] = data['Horizontal_Distance_To_Hydrology']-data['Horizontal_Distance_To_Roadways']
data['Fire_plus_Road'] = data['Horizontal_Distance_To_Fire_Points']+data['Horizontal_Distance_To_Roadways']
data['Fire_minus_Road'] = data['Horizontal_Distance_To_Fire_Points']-data['Horizontal_Distance_To_Roadways']
return data<feature_engineering>
df['_tagline']=df['tagline'].apply(lambda x : str(x)
 ).apply(lambda x : lower_text(x)).apply(lambda x : remove_punct(x))
train = add_feature(train)
 test = add_feature(test )<prepare_x_and_y>
df['title_word_count'] = df['title'].apply(lambda x: len(str(x ).split()))
X_train = train.drop(['Id','Cover_Type','soil_type','Wilderness_Area'], axis = 1)
 y_train = train.Cover_Type
X_test = test.drop(['Id'], axis = 1 )<train_on_grid>
df_use2 = df[["has_homepage","runtime",'budget']]
%%time
lr_pipe = Pipeline(
 steps = [ 
('scaler', MinMaxScaler()), 
('classifier', LogisticRegression(solver='lbfgs', n_jobs=-1))
] 
)
 lr_param_grid = {
'classifier__C': [1, 10, 100,1000],
}
np.random.seed(1)
 grid_search = GridSearchCV(lr_pipe, lr_param_grid, cv=5, refit='True')
 grid_search.fit(X_train, y_train)
 print(grid_search.best_score_)
 print(grid_search.best_params_ )<train_on_grid>
list_of_cast_names = list(df['cast'].apply(lambda x: [i['name'] for i in x] if x != {} else [] ).values)
 df['num_cast'] = df['cast'].apply(lambda x: len(x)if x != {} else 0)
%%time
rf_pipe = Pipeline(
 steps = [ 
('classifier', RandomForestClassifier(n_estimators=500))
] 
)
 param_grid = {
'classifier__min_samples_leaf': [2, 3, 4, 8],
'classifier__max_depth': [30, 32, 34],
}
np.random.seed(1)
 rf_grid_search = GridSearchCV(rf_pipe, param_grid, cv=5, refit='True', n_jobs=-1)
 rf_grid_search.fit(X_train, y_train)
 print(rf_grid_search.best_score_)
 print(rf_grid_search.best_params_ )<compute_train_metric>
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]))
rf_model = rf_grid_search.best_estimator_
cv_score = cross_val_score(rf_model, X_train, y_train, cv = 5)
 print(cv_score)
 print("Accuracy: %0.2f(+/- %0.2f)" %(cv_score.mean() , cv_score.std() * 2))<find_best_params>
list_of_crew_names = list(df['crew'].apply(lambda x: [i['name'] for i in x] if x != {} else [] ).values)
 df['num_crew'] = df['crew'].apply(lambda x: len(x)if x != {} else 0)
rf = rf_grid_search.best_estimator_.steps[0][1]<train_on_grid>
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() }
%%time
xgd_pipe = Pipeline(
 steps = [ 
('classifier', XGBClassifier(n_estimators=50, subsample=0.5))
] 
)
 param_grid = {
'classifier__learning_rate' : [0.45],
'classifier__min_samples_split' : [8, 16, 32],
'classifier__min_samples_leaf' : [2],
'classifier__max_depth': [15]
}
np.random.seed(1)
 xgd_grid_search = GridSearchCV(xgd_pipe, param_grid, cv=5,
refit='True', verbose = 10, n_jobs=-1)
 xgd_grid_search.fit(X_train, y_train)
 print(xgd_grid_search.best_score_)
 print(xgd_grid_search.best_params_ )<compute_train_metric>
df_crew = pd.concat(df_crew )
xgd_model = xgd_grid_search.best_estimator_
cv_score = cross_val_score(xgd_model, X_train, y_train, cv = 5)
 print(cv_score)
 print("Accuracy: %0.2f(+/- %0.2f)" %(cv_score.mean() , cv_score.std() * 2))<find_best_params>
def select_job(list_dict, key, value):
return [ dic["name"] for dic in list_dict if dic[key]==value]
final_model = xgd_grid_search.best_estimator_.steps[0][1]<predict_on_test>
for department in department_count.index:
df['dep_{}_num'.format(department)] = df["crew"].apply(select_job, key="department", value=department ).apply(len )
final_model.fit(X_train, y_train)
 y_pred = final_model.predict(X_test )<count_values>
df_crewname = pd.DataFrame([], index=df.index)
 for job in ["Producer", "Director", "Screenplay", "Casting", "Original Music Composer"]:
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 )
Counter(y_pred )<prepare_output>
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 )
submission = pd.DataFrame({'Id': test.Id,
'Cover_Type': y_pred})
 submission.head()<save_to_csv>
with open("/kaggle/input/private-jhk/df_use_nagano.pkl","rb")as fr:
df_use_nagano = pickle.load(fr )
submission.to_csv('submission.csv', index=False )<save_to_csv>
df_use_nagano = df_use_nagano[['production_countries_count', 'production_companies_count']]
submission.to_csv('submission.csv', index=False )<import_modules>
df_use4 = df[add_cols]
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier<load_from_csv>
df_input = pd.concat([df_use, df_use4, df_features,df_use_nagano], axis=1 )
test = pd.read_csv(".. /input/forest-cover-type-prediction/test.csv")
 train = pd.read_csv(".. /input/forest-cover-type-prediction/train.csv" )<prepare_x_and_y>
df_input.isnull().sum().sum()
X_train = train.drop(['Id', 'Cover_Type'], axis=1)
 y_train = train.Cover_Type
X_test = test.drop('Id', axis=1)
 test_id = test.Id
print(X_train.shape)
 print(X_test.shape )<normalization>
df["ln_revenue"] = np.log(df["revenue"]+1 )
scaler = MinMaxScaler()
Xs_train = scaler.fit_transform(X_train)
 Xs_test = scaler.transform(X_test )<train_on_grid>
df_input['log_budget'] = np.log10(df_input['budget'] )
lr_model = LogisticRegression(solver='liblinear')
 param_grid = {
'penalty': ['l1', 'l2'],
'C': [10, 100, 1000],
}
np.random.seed(1)
 grid_search = GridSearchCV(lr_model, param_grid, cv=5, refit='True', verbose=15, n_jobs=-1)
 grid_search.fit(Xs_train, y_train)
 print(grid_search.best_score_)
 print(grid_search.best_params_ )<train_on_grid>
df_input['budget/popularity1'] = df_input['budget']/df_input['popularity']
df_input['budget/popularity2'] = df_input['budget']/df_input['popularity2']
df_input['budget/runtime'] = df_input['budget']/df_input['runtime']
rf_model = RandomForestClassifier(n_estimators=500)
 param_grid = {
'min_samples_leaf' : [2, 4, 8, 16],
'max_depth' : [12, 16, 24]
}
np.random.seed(1)
 grid_search = GridSearchCV(rf_model, param_grid, cv=5, refit='True', verbose=15, n_jobs=-1)
 grid_search.fit(X_train, y_train)
 print(grid_search.best_score_)
 print(grid_search.best_params_ )<predict_on_test>
df_input['_popularity_mean_year']=df['popularity']/df.groupby("release_year")["popularity"].transform('mean')
 df_input['_budget_runtime_ratio']=df['budget']/df['runtime']
df_input['_budget_popularity_ratio']=df['budget']/df['popularity']
df_input['_budget_year_ratio']=df['budget']/(df['release_year']*df['release_year'])
 df_input['_releaseYear_popularity_ratio']=df['release_year']/df['popularity']
df_input['_releaseYear_popularity_ratio2']=df['popularity']/df['release_year']
df_input['_popularity_totalVotes_ratio']=df['totalVotes']/df['popularity']
df_input['_rating_popularity_ratio']=df['rating']/df['popularity']
df_input['_rating_totalVotes_ratio']=df['totalVotes']/df['rating']
df_input['_totalVotes_releaseYear_ratio']=df['totalVotes']/df['release_year']
df_input['_budget_rating_ratio']=df['budget']/df['rating']
df_input['_runtime_rating_ratio']=df['runtime']/df['rating']
df_input['_budget_totalVotes_ratio']=df['budget']/df['totalVotes']
test_pred = grid_search.predict(X_test )<count_values>
cols = df_input.loc[:, df_input.isnull().sum() >0].columns
df_input.loc[:, cols] = df_input[cols].fillna(df_input[cols].mean() )
for i in range(1,8):
print(list(test_pred ).count(i)) 
<prepare_output>
with open('df_input.pkl', 'wb')as f:
pickle.dump(df_input , f )
submission = pd.DataFrame({
'Id':test_id,
'Cover_Type':test_pred
})
 submission.head()<save_to_csv>
no_numeric = df_input.apply(lambda s:pd.to_numeric(s, errors='coerce')).isnull().all()
no_numeric[no_numeric]
submission.to_csv('my_submission.csv', index=False )<import_modules>
df_input.columns = ["".join(c if c.isalnum() else "_" for c in str(x)) for x in df_input.columns]
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<load_from_csv>
[df_input.isnull().sum() ]
data_train = pd.read_csv(".. /input/train.csv")
 data_test = pd.read_csv(".. /input/test.csv")
 data_train.head()<define_variables>
X_all = df_input
X_all.drop([0],inplace = True)
 y_all = df["ln_revenue"]
y_all.index = X_all.index
features_soil = ['Soil_Type1', 'Soil_Type2', 'Soil_Type3',
'Soil_Type4', 'Soil_Type5', 'Soil_Type6', 'Soil_Type7', 'Soil_Type8',
'Soil_Type9', 'Soil_Type10', 'Soil_Type11', 'Soil_Type12',
'Soil_Type13', 'Soil_Type14', 'Soil_Type15', 'Soil_Type16',
'Soil_Type17', 'Soil_Type18', 'Soil_Type19', 'Soil_Type20',
'Soil_Type21', 'Soil_Type22', 'Soil_Type23', 'Soil_Type24',
'Soil_Type25', 'Soil_Type26', 'Soil_Type27', 'Soil_Type28',
'Soil_Type29', 'Soil_Type30', 'Soil_Type31', 'Soil_Type32',
'Soil_Type33', 'Soil_Type34', 'Soil_Type35', 'Soil_Type36',
'Soil_Type37', 'Soil_Type38', 'Soil_Type39', 'Soil_Type40']
data_train["Soil_Count"] = data_train[features_soil].apply(sum, axis=1)
 data_train.head()<feature_engineering>
X_all.drop(columns = ['budget'],inplace = True )
data_train["Soil_Type"] = data_train[features_soil].apply(np.argmax, axis=1)
 data_train.head()<feature_engineering>
data_train["Soil_Type"] = data_train["Soil_Type"].apply(lambda x: x.split("Soil_Type")[-1])
 data_train.head()<feature_engineering>
test_X = X_all.iloc[3000:]
data_train["Wilderness_Area"] = data_train[features_wilderness].apply(np.argmax, axis=1)
 data_train["Wilderness_Area"] = data_train["Wilderness_Area"].apply(lambda x: x.split("Wilderness_Area")[-1])
 data_train.Wilderness_Area.head()<feature_engineering>
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
data_train["Elevation_Aspect"] = np.sqrt(data_train.Elevation * data_train.Aspect)
 sns.distplot(data_train["Elevation_Aspect"])
 plt.show()<drop_column>
train_X, val_X, train_y, val_y = train_test_split(X_all.iloc[:3000],
y_all.iloc[:3000],
test_size=0.25, random_state=1 )
def clear_dataset(dataset):
features_soil = ['Soil_Type1', 'Soil_Type2', 'Soil_Type3',
'Soil_Type4', 'Soil_Type5', 'Soil_Type6', 'Soil_Type7', 'Soil_Type8',
'Soil_Type9', 'Soil_Type10', 'Soil_Type11', 'Soil_Type12',
'Soil_Type13', 'Soil_Type14', 'Soil_Type15', 'Soil_Type16',
'Soil_Type17', 'Soil_Type18', 'Soil_Type19', 'Soil_Type20',
'Soil_Type21', 'Soil_Type22', 'Soil_Type23', 'Soil_Type24',
'Soil_Type25', 'Soil_Type26', 'Soil_Type27', 'Soil_Type28',
'Soil_Type29', 'Soil_Type30', 'Soil_Type31', 'Soil_Type32',
'Soil_Type33', 'Soil_Type34', 'Soil_Type35', 'Soil_Type36',
'Soil_Type37', 'Soil_Type38', 'Soil_Type39', 'Soil_Type40']
features_wilderness = ['Wilderness_Area1', 'Wilderness_Area2', 'Wilderness_Area3','Wilderness_Area4']
dataset["Soil_Type"] = dataset[features_soil].apply(np.argmax, axis=1)
 dataset["Soil_Type"] = dataset["Soil_Type"].apply(lambda x: x.split("Soil_Type")[-1] ).astype(int)
 dataset = dataset.drop(["Soil_Type15", "Soil_Type7"], axis=1)
 dataset["Wilderness_Area"] = dataset[features_wilderness].apply(np.argmax, axis=1)
 dataset["Wilderness_Area"] = dataset["Wilderness_Area"].apply(lambda x: x.split("Wilderness_Area")[-1] ).astype(int)
 dataset["Hillshade_1"] =(dataset.Hillshade_Noon * dataset.Hillshade_9am)
 dataset["Hillshade_1_sqrt"] = np.sqrt(dataset["Hillshade_1"])
 dataset["Hillshade_2"] =(dataset.Hillshade_3pm * dataset.Hillshade_9am)
 dataset["Hillshade_2_sqrt"] = np.sqrt(dataset["Hillshade_2"])
 dataset["Hillshade_3"] =(dataset.Hillshade_3pm * dataset.Hillshade_Noon * dataset.Hillshade_9am)
 dataset["Hillshade_3_sqrt"] = np.sqrt(dataset["Hillshade_3"])
 dataset.Hillshade_1 = dataset.Hillshade_1.astype(float)
 dataset["DistanceToHydrology"] = np.sqrt(dataset.Horizontal_Distance_To_Hydrology ** 2 + dataset.Vertical_Distance_To_Hydrology ** 2)
 dataset["Horizontal_Distance_To_Roadways_sqrt"]= np.sqrt(dataset["Horizontal_Distance_To_Roadways"])
 dataset["Horizontal_Distance_To_Fire_Points_sqrt"]= np.sqrt(dataset["Horizontal_Distance_To_Fire_Points"])
 dataset["Slope_sqrt"] = np.sqrt(dataset["Slope"])
 dataset["Hillshade_9am_cube"] = dataset["Hillshade_9am"].apply(lambda x: x**3)
 dataset["Hillshade_Noon_cube"] = dataset["Hillshade_Noon"].apply(lambda x: x**3)
 dataset["Aspect_Slope_cbrt"] = np.cbrt(dataset.Aspect * dataset.Slope)
 dataset["Elevation_Slope_sqrt"] = np.sqrt(dataset.Elevation * dataset.Slope)
 dataset["Elevation_Aspect_sqrt"] = np.sqrt(dataset.Elevation * dataset.Aspect)
 dataset["Elevation_sqrt"] = np.sqrt(dataset.Elevation)
 return dataset<load_from_csv>
random_seed = 2019
k = 10
fold = list(KFold(k, shuffle = True, random_state = random_seed ).split(train))
np.random.seed(random_seed )
data_train = pd.read_csv(".. /input/train.csv")
 final_train = clear_dataset(data_train )<prepare_x_and_y>
xgb = XGBRegressor()
x_data = final_train.drop(["Cover_Type", "Id"], axis=1)
 y_data = final_train["Cover_Type"]<normalization>
xgb_model = XGBRegressor(max_depth=4,
min_child_weight=4,
learning_rate=0.03,
n_estimators=500,
objective='reg:linear',
nthread = 4,
gamma=1.3,
silent=1,
subsample=0.7,
colsample_bytree=0.3,
colsample_bylevel=0.5)
 xgb_model.fit(train_X,train_y)
 xgb_prediction = xgb_model.predict(val_X)
 xgb_rmse = mean_squared_error(val_y, xgb_prediction )
pca = PCA(n_components = 2)
 x_pca = pca.fit_transform(x_data)
 print("variance ratio: ", pca.explained_variance_ratio_)
 print("sum: ",sum(pca.explained_variance_ratio_))<split>
math.sqrt(xgb_rmse )
x_train, x_val, y_train, y_val = train_test_split(x_data, y_data, test_size=0.25, random_state=42 )<compute_test_metric>
xgb_pred = xgb_model.predict(test_X )
def get_metrics(y_test, y_predicted):
precision = precision_score(y_test, y_predicted, pos_label=None,
average='weighted')
 recall = recall_score(y_test, y_predicted, pos_label=None,
average='weighted')
 f1 = f1_score(y_test, y_predicted, pos_label=None, average='weighted')
 accuracy = accuracy_score(y_test, y_predicted)
 return accuracy, precision, recall, f1<train_model>
pred_xgb = pd.DataFrame(np.exp(xgb_pred)-1,columns=["revenue"])
 pred_xgb
clf = RandomForestClassifier(n_estimators=100, max_depth=19, max_features=11,n_jobs=-1, random_state=42)
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val )<compute_test_metric>
test_id = test["id"]
accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))<train_model>
sub=pd.concat([test_id, pred_xgb],axis=1)
 sub.to_csv('TMDB_xgb.csv',index=False )
clf = XGBClassifier(n_estimators=200, learning_rate=0.3, max_depth=3,n_jobs=-1, seed=42, objective="multi:softmax")
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val)
 accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))<compute_train_metric>
lgbm = LGBMRegressor()
clf = LGBMClassifier(n_estimators=200, learning_rate=0.3, max_depth=3,n_jobs=-1, seed=42, objective="multi:softmax")
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val)
 accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))<load_from_csv>
lgbm_model = LGBMRegressor(n_estimators= 500,
objection ='regressor',
metric='rmse',
max_depth = 2,
num_leaves=10,
min_child_samples=500,
learning_rate=0.01,
boosting = 'gbdt',
num_iterations = 1500,
min_data_in_leaf= 10,
bagging_freq = 1,
bagging_fraction = 0.9,
feature_fraction = 0.7,
importance_type='gain',
use_best_model=True)
 lgbm_model.fit(train_X, train_y)
 lgbm_prediction = lgbm_model.predict(val_X)
 lgbm_rmse = mean_squared_error(val_y, lgbm_prediction )
data_test = pd.read_csv(".. /input/test.csv")
 final_test = clear_dataset(data_test )<train_model>
math.sqrt(lgbm_rmse )
clf = RandomForestClassifier(n_estimators=100,max_depth=11, max_features=21,min_samples_leaf=0.001,criterion="entropy",n_jobs=-1, random_state=42)
 clf.fit(x_train, y_train)
 y_predicted = clf.predict(x_val)
 accuracy, precision, recall, f1 = get_metrics(y_val, y_predicted)
 print("accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))<compute_train_metric>
lgbm_pred = lgbm_model.predict(test_X )
y_predicted = clf.predict(x_train)
 accuracy, precision, recall, f1 = get_metrics(y_train, y_predicted)
 print("train accuracy = %.3f, precision = %.3f, recall = %.3f, f1 = %.3f" %(accuracy, precision, recall, f1))<save_to_csv>
pred_lgbm = pd.DataFrame(np.exp(lgbm_pred)-1,columns=["revenue"])
 pred_lgbm
clf = RandomForestClassifier(n_estimators=100,max_depth=11, max_features=21,min_samples_leaf=0.001,criterion="entropy",n_jobs=-1, random_state=42)
 clf.fit(x_data, y_data)
 test_preds = clf.predict(final_test.drop(["Id"], axis=1))
output = pd.DataFrame({'Id': data_test.Id,
'Cover_Type': test_preds})
 output.to_csv('rf_submission.csv', index=False )<load_from_csv>
sub=pd.concat([test_id, pred_lgbm],axis=1)
 sub.to_csv('TMDB_lgbm.csv',index=False )
train = pd.read_csv('.. /input/train.csv' , index_col='Id')
 test = pd.read_csv('.. /input/test.csv' , index_col='Id')
 labels = train.Cover_Type
train.drop('Cover_Type' , axis = 1 , inplace =True)
 train.head(3 )<count_missing_values>
train.isna().sum()<count_missing_values>
cat_model = CatBoostRegressor(iterations=2000,
learning_rate=0.01,
depth=6,
eval_metric='RMSE',
colsample_bylevel=0.6,
bagging_temperature = 0.1,
metric_period = None,
early_stopping_rounds=200)
 cat_model.fit(train_X, train_y)
 cat_prediction = cat_model.predict(val_X)
 cat_rmse = mean_squared_error(val_y, cat_prediction )
test.isna().sum()<normalization>
math.sqrt(cat_rmse )
scaler = RobustScaler()
train = scaler.fit_transform(train)
 test = scaler.transform(test )<compute_train_metric>
cat_pred = cat_model.predict(test_X )
clf = GradientBoostingClassifier(learning_rate=0.65 , n_estimators= 250 ,max_depth = 9)
 cross_val_score(clf , train , labels , cv = 3 )<train_model>
pred_cat = pd.DataFrame(np.exp(cat_pred)-1,columns=["revenue"])
 pred_cat
clf.fit(train, labels )<predict_on_test>
sub=pd.concat([test_id, pred_cat],axis=1)
 sub.to_csv('TMDB_cat.csv',index=False )
pre = clf.predict(test )<save_to_csv>
ansamble = 0.4 * pred_lgbm["revenue"] + 0.2 * pred_xgb["revenue"] + 0.4 * pred_cat["revenue"]
ansdf = pd.read_csv('.. /input/sampleSubmission.csv')
 ansdf['Cover_Type'] = pre
ansdf.to_csv('submit.csv', index = False )<prepare_x_and_y>
sub3=pd.concat([test_id, ansamble],axis=1)
 sub3
df = pd.read_pickle('.. /input/save-dromosys-features/df.pkl.gz')
 print("Raw shape: ", df.shape)
 df.set_index('SK_ID_CURR', inplace=True)
 y = df['TARGET'].copy()
feats = [f for f in df.columns if f not in ['TARGET','SK_ID_CURR','SK_ID_BUREAU','SK_ID_PREV','index']]
df.drop(['index', 'TARGET'], axis=1, inplace=True)
 print("X shape: ", df.shape, " y shape:", y.shape)
 print("
Preparing data...")
 for feat in feats:
df[feat] = df[feat].fillna(df[feat].mean() )<compute_test_metric>
sub3.to_csv('TMDB_ansamble.csv',index=False )
def rank_gauss(x):
N = x.shape[0]
temp = x.argsort()
rank_x = temp.argsort() / N
rank_x -= rank_x.mean()
rank_x *= 2
efi_x = erfinv(rank_x)
 efi_x -= efi_x.mean()
return efi_x<feature_engineering>
ansamble2 = 0.35 * pred_lgbm["revenue"] + 0.3 * pred_xgb["revenue"] + 0.35 * pred_cat["revenue"]
for i in df.columns:
df[i] = rank_gauss(df[i].values )<train_model>
sub4=pd.concat([test_id, ansamble2],axis=1)
 sub4
training = y.notnull()
testing = y.isnull()
X_train = df[training].values
X_test = df[testing].values
y_train = np.array(y[training])
 print(X_train.shape, X_test.shape, y_train.shape)
 gc.collect()<compute_train_metric>
sub4.to_csv('TMDB_ansamble2.csv',index=False )
class IntervalEvaluation(Callback):
def __init__(self, validation_data=() , interval=10):
super(Callback, self ).__init__()
self.interval = interval
self.X_val, self.y_val = validation_data
def on_epoch_end(self, epoch, logs={}):
y_pred = self.model.predict_proba(self.X_val, verbose=0)
 score = roc_auc_score(self.y_val, y_pred)
 logging.info("interval evaluation - epoch: {:d} - score: {:.6f}".format(epoch, score))
print("interval evaluation - epoch: {:d} - score: {:.6f}".format(epoch, score))
logs['val_auc'] = score 
<prepare_x_and_y>
ansamble = 0.2 * pred_lgbm["revenue"] + 0.2 * pred_xgb["revenue"] + 0.6 * pred_cat["revenue"]
n_folds = 10
folds = KFold(n_splits=n_folds, shuffle=True, random_state=42)
 sub_preds = np.zeros(X_test.shape[0])
 oof_preds = np.zeros(X_train.shape[0])
 for n_fold,(trn_idx, val_idx)in enumerate(folds.split(X_train)) :
trn_x, trn_y = X_train[trn_idx], y_train[trn_idx]
val_x, val_y = X_train[val_idx], y_train[val_idx]
earlystop = EarlyStopping(monitor='val_auc', min_delta=0, patience=3, verbose=0, mode='max')
 file_path = "fold " + str(n_fold+1)+ " best_model.hdf5"
check_point = ModelCheckpoint(file_path, monitor = "val_auc", verbose = 1, save_best_only = True, mode = "max")
 print('Setting up neural network...')
 nn = Sequential()
nn.add(Dense(units = 400 , kernel_initializer = 'normal', input_dim = df.shape[1]))
nn.add(PReLU())
 nn.add(Dropout (.3))
nn.add(Dense(units = 160 , kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.3))
nn.add(Dense(units = 64 , kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.3))
nn.add(Dense(units = 26, kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.3))
nn.add(Dense(units = 12, kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.3))
nn.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
nn.compile(loss='binary_crossentropy', optimizer='adam')
 print('Fitting neural network...')
 ival = IntervalEvaluation(validation_data=(val_x, val_y), interval=10)
 nn.fit(trn_x, trn_y, validation_data =(val_x, val_y), epochs=20, verbose=0, callbacks=[ival, earlystop, check_point], batch_size=128)
 best_model = load_model(file_path)
 oof_preds[val_idx] = best_model.predict(val_x ).flatten()
print(roc_auc_score(val_y, oof_preds[val_idx]))
print('Predicting...')
 sub_preds += best_model.predict(X_test ).flatten().clip(0,1)/ folds.n_splits
gc.collect()
print('FULL AUC: {}'.format(roc_auc_score(y_train, oof_preds)) )<save_to_csv>
sub5=pd.concat([test_id, ansamble],axis=1)
 sub5
<load_from_csv><EOS> 
sub3.to_csv('TMDB_ansamble3.csv',index=False )