Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<save_to_csv>
seed = 10
np.random.seed(seed)
 random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
output2.to_csv('my_submission.csv', index=False )<set_options>
%%time
train_transaction = pd.read_csv('.. /input/ieee-fraud-detection/train_transaction.csv', index_col='TransactionID')
 test_transaction = pd.read_csv('.. /input/ieee-fraud-detection/test_transaction.csv', index_col='TransactionID')
 sample_submission = pd.read_csv('.. /input/ieee-fraud-detection/sample_submission.csv', index_col='TransactionID' )
%matplotlib inline 
<import_modules>
train_transaction['hour'] = train_transaction['TransactionDT'].map(lambda x:(x//3600)%24)
 test_transaction['hour'] = test_transaction['TransactionDT'].map(lambda x:(x//3600)%24)
 train_transaction['weekday'] = train_transaction['TransactionDT'].map(lambda x:(x//(3600 * 24)) %7)
 test_transaction['weekday'] = test_transaction['TransactionDT'].map(lambda x:(x//(3600 * 24)) %7 )
from sklearn.preprocessing import OneHotEncoder, LabelEncoder, label_binarize, StandardScaler<import_modules>
cols = "TransactionDT,TransactionAmt,ProductCD,card1,card2,card3,card4,card5,card6,addr1,addr2,C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,C14,M1,M2,M3,M4,M5,M6,M7,M8,M9".split(",")
 train_test = train_transaction[cols].append(test_transaction[cols])
 for col in "ProductCD,card1,card2,card3,card4,card5,card6,addr1,addr2,C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,C14".split(","):
col_count = train_test.groupby(col)['TransactionDT'].count()
train_transaction[col+'_count'] = train_transaction[col].map(col_count)
 test_transaction[col+'_count'] = test_transaction[col].map(col_count)
 for col in "card1,card2,card5,addr1,addr2".split(","):
col_count = train_test.groupby(col)['TransactionAmt'].mean()
train_transaction[col+'_amtcount'] = train_transaction[col].map(col_count)
 test_transaction[col+'_amtcount'] = test_transaction[col].map(col_count)
 col_count1 = train_test[train_test['C5'] == 0].groupby(col)['C5'].count()
col_count2 = train_test[train_test['C5'] != 0].groupby(col)['C5'].count()
train_transaction[col+'_C5count'] = train_transaction[col].map(col_count2)/(train_transaction[col].map(col_count1)+ 0.01)
 test_transaction[col+'_C5count'] = test_transaction[col].map(col_count2)/(test_transaction[col].map(col_count1)+ 0.01)
 del train_test
from sklearn.metrics import mean_absolute_error as MAE
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import OneHotEncoder<import_modules>
train_identity = pd.read_csv('.. /input/ieee-fraud-detection/train_identity.csv', index_col='TransactionID')
 test_identity = pd.read_csv('.. /input/ieee-fraud-detection/test_identity.csv', index_col='TransactionID')
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn import svm
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import GradientBoostingClassifier<import_modules>
col_del = []
for i in range(339):
col = "V" + str(i+1)
 s = train_transaction[col].fillna(0 ).map(lambda x:0 if x%1 == 0 else 1 ).sum()
if s > 100:
print(col,s)
 col_del.append(col)
from catboost import CatBoostClassifier, Pool, cv<import_modules>
train = train_transaction.merge(train_identity, how='left', left_index=True, right_index=True)
 test = test_transaction.merge(test_identity, how='left', left_index=True, right_index=True)
 print(train.shape)
 print(test.shape)
 y_train = train['isFraud'].copy()
del train_transaction, train_identity, test_transaction, test_identity
X_train = train.drop('isFraud', axis=1)
 X_test = test.copy()
del train, test
for f in X_train.columns:
if X_train[f].dtype=='object' or X_test[f].dtype=='object':
lbl = preprocessing.LabelEncoder()
lbl.fit(list(X_train[f].values)+ list(X_test[f].values))
X_train[f] = lbl.transform(list(X_train[f].values))
X_test[f] = lbl.transform(list(X_test[f].values))
from sklearn.ensemble import VotingClassifier<import_modules>
%%time
X_train = reduce_mem_usage(X_train)
 X_test = reduce_mem_usage(X_test)
 debug = False
if debug:
split_pos = X_train.shape[0]*4//5
y_test = y_train.iloc[split_pos:]
y_train = y_train.iloc[:split_pos]
X_test = X_train.iloc[split_pos:,:]
X_train = X_train.iloc[:split_pos,:]
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV<define_variables>
gc.collect()
train_data_path = ".. /input/titanic/train.csv"
test_data_path = ".. /input/titanic/test.csv"<load_from_csv>
%%time
folds = 3
kf = KFold(n_splits = folds, shuffle = True, random_state=seed)
 y_preds = np.zeros(X_test.shape[0])
 i = 0
for tr_idx, val_idx in kf.split(X_train, y_train):
i+=1
clf = xgb.XGBClassifier(
 n_estimators=700,
max_depth=9,
learning_rate=0.03,
subsample=0.9,
colsample_bytree=0.9,
tree_method='gpu_hist' 
)
 X_tr = X_train.iloc[tr_idx, :]
y_tr = y_train.iloc[tr_idx]
clf.fit(X_tr, y_tr)
 del X_tr
y_preds+= clf.predict_proba(X_test)[:,1] / folds
if debug:
print("debug:",roc_auc_score(y_test, clf.predict_proba(X_test)[:,1] / folds))
del clf
if debug:
print("debug:",roc_auc_score(y_test, y_preds))
gc.collect()
train_data = pd.read_csv(train_data_path)
 test_data = pd.read_csv(test_data_path )<define_variables>
features = [x for x in X_train.columns if x not in col_del]
folds = 3
kf = KFold(n_splits = folds, shuffle = True, random_state=seed)
 y_preds11 = np.zeros(X_test.shape[0])
 i = 0
for tr_idx, val_idx in kf.split(X_train, y_train):
i+=1
clf = xgb.XGBClassifier(
 n_estimators=800,
max_depth=9,
learning_rate=0.03,
subsample=0.9,
colsample_bytree=0.9,
tree_method='gpu_hist' 
)
 X_tr = X_train[features].iloc[tr_idx, :]
y_tr = y_train.iloc[tr_idx]
clf.fit(X_tr, y_tr)
 del X_tr
y_preds11+= clf.predict_proba(X_test[features])[:,1] / folds
if debug:
print("debug:",roc_auc_score(y_test, clf.predict_proba(X_test[features])[:,1] / folds))
del clf
gc.collect()
if debug:
print("debug:",roc_auc_score(y_test, y_preds11))
print("debug:",roc_auc_score(y_test, y_preds11*0.5 + y_preds*0.5))
num_var = ['Age', 'SibSp', 'Parch', 'Fare']
cat_var = ['Survived', 'Pclass', 'Sex', 'Ticket', 'Cabin', 'Embarked']<split>
cate = [x for x in X_train.columns if(x == 'ProductCD' or x.startswith("addr")or x.startswith("card")or
x.endswith("domain")or x.startswith("Device")) and not x.endswith("count")]
print(cate)
 params = {'application': 'binary',
'boosting': 'gbdt',
'metric': 'auc',
'max_depth': 16,
'learning_rate': 0.05,
'bagging_fraction': 0.9,
'feature_fraction': 0.9,
'verbosity': -1,
'lambda_l1': 0.1,
'lambda_l2': 0.01,
'num_leaves': 500,
'min_child_weight': 3,
'data_random_seed': 17,
'nthreads':4}
early_stop = 500
verbose_eval = 30
num_rounds = 600
folds = 3
kf = KFold(n_splits = folds, shuffle = True, random_state=seed)
 y_preds2 = np.zeros(X_test.shape[0])
 feature_importance_df = pd.DataFrame()
i = 0
for tr_idx, val_idx in kf.split(X_train, y_train):
X_tr = X_train.iloc[tr_idx, :]
y_tr = y_train.iloc[tr_idx]
d_train = lgb.Dataset(X_tr, label=y_tr,categorical_feature = cate)
 watchlist = []
if debug:
d_test = lgb.Dataset(X_test, label=y_test,categorical_feature = cate)
 watchlist.append(d_test)
 model = lgb.train(params,
train_set=d_train,
num_boost_round=num_rounds,
valid_sets=watchlist,
verbose_eval=verbose_eval)
 y_preds2+= model.predict(X_test)/ folds
fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = X_tr.columns
fold_importance_df["importance"] = model.feature_importance()
fold_importance_df["fold"] = i + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 if debug:
print("debug:",roc_auc_score(y_test, model.predict(X_test)/ folds))
i+=1
del X_tr,d_train
if debug:
print("debug:",roc_auc_score(y_test, y_preds2))
print("debug:",roc_auc_score(y_test,(y_preds + y_preds2)*0.5))
df_num = train_data[num_var]
df_cat = train_data[cat_var]<create_dataframe>
if debug:
print("debug:",roc_auc_score(y_test, y_preds))
print("debug:",roc_auc_score(y_test, y_preds2))
print("debug:",roc_auc_score(y_test,(y_preds + y_preds2)*0.5))
pd.pivot_table(train_data, index='Survived', values = num_var )<define_variables>
features = [x for x in X_train.columns]
cate = [x for x in X_train.columns if(x == 'ProductCD' or x in ['card1','card2'] or x.startswith("addr")or
x.endswith("domain")or x.startswith("Device")) and not x.endswith("count")and not x == "id_11" ]
print(cate)
 verbose_eval = 30
num_rounds = 800
folds = 3
kf = KFold(n_splits = folds, shuffle = True, random_state=seed+1)
 y_preds3 = np.zeros(X_test.shape[0])
 feature_importance_df = pd.DataFrame()
i = 0
for tr_idx, val_idx in kf.split(X_train, y_train):
X_tr = X_train[features].iloc[tr_idx, :].fillna(-1)
 y_tr = y_train.iloc[tr_idx]
model=cb.CatBoostClassifier(iterations=num_rounds,depth=14,learning_rate=0.04,loss_function='Logloss',eval_metric='Logloss'
,task_type = "GPU")
 if debug:
model.fit(X_tr,y_tr,cat_features=cate,verbose_eval = 30)
 else:
model.fit(X_tr,y_tr,cat_features=cate,verbose_eval = 30)
 del X_tr
y_preds3+= model.predict_proba(X_test[features].fillna(-1)) [:,1] / folds
if debug:
print("debug:",roc_auc_score(y_test, model.predict_proba(X_test[features].fillna(-1)) [:,1] / folds))
i+=1
if debug:
print("debug:",roc_auc_score(y_test, y_preds3))
print("debug:",roc_auc_score(y_test,(y_preds + y_preds3)*0.5))
print("debug:",roc_auc_score(y_test,(y_preds + y_preds2 + y_preds3)*0.33))
[i for i in cat_var if i not in ['Survived', 'Ticket']]<count_values>
if debug:
print("debug:",roc_auc_score(y_test, y_preds))
print("debug:",roc_auc_score(y_test, y_preds2))
print("debug:",roc_auc_score(y_test, y_preds3))
print("debug:",roc_auc_score(y_test,(y_preds + y_preds3)*0.5))
print("debug:",roc_auc_score(y_test,(y_preds + y_preds2 + y_preds3*0.5)*0.33))
print("debug:",roc_auc_score(y_test,(y_preds11*0.5 + y_preds*0.5 + y_preds2 + y_preds3*0.5)*0.33))
<create_dataframe><EOS> 
if not debug:
sample_submission['isFraud'] =(y_preds11*0.5 + y_preds*0.5 + y_preds2 + y_preds3*0.5)*0.33
sample_submission.to_csv('simple_ensemble6.csv')