Kaggle competition: IEEE-CIS Fraud Detection
train.loc[train['runtime']<1, 'runtime'] = np.nan
test.loc[test['runtime']<1, 'runtime'] = np.nan<prepare_output>
print(h2o.__version__)
 h2o.init(max_mem_size='16G' )
train.set_index('id', inplace=True)
 test.set_index('id', inplace=True )<prepare_x_and_y>
%%time
train = h2o.import_file(".. /input/standalone-train-and-test-preprocessing/train.csv")
 test = h2o.import_file(".. /input/standalone-train-and-test-preprocessing/test.csv" )
train_y = train['revenue']
train_X = train
del train_X['revenue']<feature_engineering>
train_y = np.log1p(train_y )<define_variables>
x = train.columns[:-1]
y = 'isFraud'
train[y] = train[y].asfactor()
transforms = {
'budget': lambda x: np.log1p(x),
'popularity': lambda x: np.log1p(x),
}
null_encode_cols = [
'belongs_to_collection',
'homepage',
]
date_cols = {
'release_date':('%m/%d/%Y', ['year', 'month', 'day', 'dayofyear', 'dayofweek'])
 }
json_fields = {
'genres': 'name',
'production_companies': 'name',
'production_countries': 'iso_3166_1',
'spoken_languages': 'iso_639_1',
'Keywords': 'name',
'cast': 'name',
'crew': [('name', 'job', 'Director'), 
('name', 'job', 'Producer'), 
('name', 'job', 'Writer'), 
('name', 'job', 'Editor'),],
}
nhot_cols = [
'genres_name',
'original_language',
'production_countries_iso_3166_1',
'spoken_languages_iso_639_1',
]
bayesian_c = 5
te_cols = [
'production_companies_name',
'cast_name',
'crew_job_Director_name',
'crew_job_Producer_name',
'crew_job_Writer_name',
'crew_job_Editor_name',
]
n_pc = 5
bert_cols = [
'overview',
'tagline',
'title',
'Keywords_name',
]
word_count = lambda e: len(e.split(' ')) if isinstance(e, str)else 0
keyword_count = lambda e: len(e.split(',')) if isinstance(e, str)else 0
new_features = {
'budget_runtime_ratio': lambda x: x['budget']/x['runtime'],
'budget_popularity_ratio': lambda x: x['budget']/(x['popularity']+1),
'budget_year_ratio': lambda x: x['budget']/np.square(x['release_date_year']),
'popularity_year_ratio': lambda x: x['popularity']/np.square(x['release_date_year']),
'rating_to_votes_ratio': lambda x: x['avg_rating']/(x['num_rating']+1),
'runtime_rating_ratio': lambda x: x['runtime']/(x['avg_rating']+1),
'overview_word_count': lambda x: x['overview'].apply(word_count),
'tagline_word_count': lambda x: x['tagline'].apply(word_count),
'keyword_count': lambda x: x['Keywords_name'].apply(keyword_count),
}<categorify>
aml = H2OAutoML(max_models=30, seed=55, max_runtime_secs=30000)
 aml.train(x=x, y=y, training_frame=train)
preprocessing = Pipeline([ 
('transforms', LambdaTransformer(transforms)) , 
('join_imbd', JoinTransformer(imdb_df, 'imdb_id', 'imdb_id')) , 
('null_encoder', NullEncoder(null_encode_cols, delete_old=True)) , 
('date_encoder', DateEncoder(date_cols)) , 
('json_encoder', JsonEncoder(json_fields)) , 
('nhot_encoder', NhotEncoder(nhot_cols, top_n=10)) , 
('add_features', LambdaFeatures(new_features)) , 
('targ_encoder', MultiTargetEncoderLOO(te_cols, bayesian_c=bayesian_c)) , 
('bert_encoder', BertEncoder(bert_cols, n_pc=n_pc)) , 
('scaler', Scaler()), 
('imputer', Imputer()),
] )<categorify>
aml.leader
model = Pipeline([ 
('preprocessing', preprocessing), 
('regressor', CatBoostRegressor(verbose=False))
])
 fit_model = model.fit(train_X, train_y)
 preds = fit_model.predict(test)
 preds_df = pd.DataFrame(index=test.index)
 preds_df['revenue'] = np.maximum(0, np.expm1(preds))
preds_df.to_csv('predictions_baseline.csv' )<categorify>
h2o.remove(train )
train_pp = preprocessing.fit_transform(train_X, train_y)
 test_pp = preprocessing.transform(test )<create_dataframe>
preds = aml.predict(test)
 preds['p1'].as_data_frame().values.flatten().shape
%%time
catboost = Pipeline([ 
('regressor', CatBoostRegressor(verbose=False))
])
 imp_df = permutation_importance_cv(train_pp,
train_y.copy() ,
estimator=catboost,
metric='rmse',
n_jobs=1 )<statistical_test>
sample_submission = pd.read_csv('.. /input/ieee-fraud-detection/sample_submission.csv')
 sample_submission.shape
<categorify><EOS> 
sample_submission['isFraud'] = preds['p1'].as_data_frame().values
sample_submission.to_csv('h2o_automl_submission_15.csv', index=False )