Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<save_to_csv>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
test, test_pId = prep_nn_1(test_raw.copy(deep=True))
test_dl = learn.dls.test_dl(test)
 preds, _ = learn.get_preds(dl=test_dl)
 preds = np.argmax(preds, 1 ).numpy()
submission = pd.DataFrame(
 {'PassengerId': test_pId,
'Survived': preds} 
)
 submission.to_csv('submission_trivial.csv', index=False )<feature_engineering>
train_transaction = pd.read_csv('/kaggle/input/ieee-fraud-detection/train_transaction.csv')
 train_identity = pd.read_csv('/kaggle/input/ieee-fraud-detection/train_identity.csv')
 test_transaction = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_transaction.csv')
 test_identity = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_identity.csv' )
full['Title'] = full['Name'].apply(title)
 full['Title'] = full['Title'].replace({'Ms': 'Miss', 'Mlle': 'Miss', 'Mme': 'Mrs'})
 RARE_TITLE = list(( full['Title'].value_counts() < 8 ).replace({False: np.nan} ).dropna().index)
 full['Title'] = full['Title'].replace(RARE_TITLE, 'Rare')
 def extract_title(df, reduce=True):
df['Title'] = df['Name'].apply(title)
 if reduce:
df['Title'] = df['Title'].replace({'Ms': 'Miss', 'Mlle': 'Miss', 'Mme': 'Mrs'})
 df['Title'] = df['Title'].replace(RARE_TITLE, 'Rare')
 df['Title'] = df['Title'].fillna('Unknown')
 return df<feature_engineering>
train_transaction = reduce_mem_usage(train_transaction, verbose = True)
 train_identity = reduce_mem_usage(train_identity,verbose = True)
 test_transaction = reduce_mem_usage(test_transaction, verbose=True)
 test_identity = reduce_mem_usage(test_identity,verbose = True )
def extract_name_length(df):
df['Name_Length'] = df['Name'].str.len()
return df<categorify>
pd.set_option('display.max_columns', None )
AGE_TITLE = full.groupby(['Title'])['Age'].agg(['mean', 'std'])
 AGE_TITLE.columns = [f'AGE_{i}' for i in AGE_TITLE.columns]
def fill_age_with_normal_rand(df):
df = df.merge(AGE_TITLE,
how='left',
left_on='Title',
right_index=True)
 df['RAND_AGE'] = df.apply(lambda x: np.random.normal(x['AGE_mean'], x['AGE_std']/2.0), axis=1)
 df['Age'] = df['Age'].fillna(df['RAND_AGE'])
 df = df.drop(columns=['RAND_AGE', 'AGE_mean', 'AGE_std'])
 return df<data_type_conversions>
final_train = pd.merge(train_transaction, train_identity, how = 'left', on = 'TransactionID')
 final_test = pd.merge(test_transaction, test_identity, how = 'left', on = 'TransactionID')
def add_family_size(df):
df['FamilySize'] = df['Parch'] + df['SibSp'] + 1
df['Single'] =(df['FamilySize'] == 1 ).astype(int)
 df['FamilySize'] = df['FamilySize'].astype(str)
 return df<groupby>
del train_transaction
del train_identity
del test_transaction
del test_identity
full['HasCabin'] = full['Cabin'].notna()
full.groupby('HasCabin')['Survived'].mean()<data_type_conversions>
final_test = final_test.rename(columns = {"id-01": "id_01", "id-02": "id_02", "id-03": "id_03",
"id-06": "id_06", "id-05": "id_05", "id-04": "id_04",
"id-07": "id_07", "id-08": "id_08", "id-09": "id_09",
"id-10": "id_10", "id-11": "id_11", "id-12": "id_12",
"id-15": "id_15", "id-14": "id_14", "id-13": "id_13",
"id-16": "id_16", "id-17": "id_17", "id-18": "id_18",
"id-21": "id_21", "id-20": "id_20", "id-19": "id_19",
"id-22": "id_22", "id-23": "id_23", "id-24": "id_24",
"id-27": "id_27", "id-26": "id_26", "id-25": "id_25",
"id-28": "id_28", "id-29": "id_29", "id-30": "id_30",
"id-31": "id_31", "id-32": "id_32", "id-33": "id_33",
"id-34": "id_34", "id-35": "id_35", "id-36": "id_36",
"id-37": "id_37", "id-38": "id_38"})
 final_test.head()
def cabin_information(df):
df['HasCabin'] = df['Cabin'].notna().astype(str)
 df['CabinType'] = df['Cabin'].str.split(r'(^[A-Z])',expand = True)[1]
df['CabinType'] = df['CabinType'].fillna('Unknown')
 df = df.drop(columns='Cabin')
 return df<categorify>
def getNulls(data):
total = data.isnull().sum()
percent = data.isnull().sum() / data.isnull().count()
missing_data = pd.concat([total, percent], axis = 1, keys = ['total', 'precent'])
 return missing_data
EMBARKED_MODE = full['Embarked'].mode() [0]
def fill_embarked(df):
df['Embarked'] = df['Embarked'].fillna(EMBARKED_MODE)
 return df<data_type_conversions>
missing_data_train = getNulls(final_train)
 missing_data_train.head(434 ).T
FARE_MEDIAN = full['Fare'].median()
def fill_fare(df):
df['Fare'] = df['Fare'].fillna(FARE_MEDIAN)
 return df<count_values>
del missing_data_train
full['TicketCat'] = full['Ticket'].str[0:2]
RARE_TICKET = list(( full['Ticket'].value_counts() < 20 ).replace({False: np.nan} ).dropna().index )<feature_engineering>
ntrain = final_train.shape[0]
ntest = final_test.shape[0]
all_data = pd.concat([final_train, final_test], axis = 0, sort = False )
def ticket_cat(df, reduce=True):
df['TicketCat'] = df['Ticket'].str[0:2]
df['TicketCat'] = df['TicketCat'].replace(RARE_TICKET, 'Rare')
 df['TicketCat'] = df['TicketCat'].fillna('Unknown')
 return df<categorify>
all_data = all_data.drop(columns=['isFraud'] )
torch.device('cuda')
 preps = [extract_title,
extract_name_length,
fill_age_with_normal_rand,
add_family_size,
cabin_information,
fill_embarked,
fill_fare,
ticket_cat,
remove_wordy_cols,
split_data]
prep_nn_2 = lambda x : apply_all(x, preps)
 train = train_raw.copy(deep=True)
 train, train_pId = prep_nn_2(train)
 cont_names = ['Pclass', 'Age', 'Name_Length', 'Parch', 'SibSp']
cat_names = ['Sex', 'TicketCat', 'CabinType', 'HasCabin', 'Single', 'Title', 'Embarked', 'Fare', 'FamilySize']
procs = [Categorify, Normalize]
dep_var = 'Survived'
splits = RandomSplitter(valid_pct=0.2, seed=42 )(train.index)
 cfg = tabular_config(embed_p=0.25, ps=0.05)
 dls = TabularPandas(train,
cont_names=cont_names,
cat_names=cat_names,
procs=procs,
y_names=dep_var,
splits=splits ).dataloaders(bs=64)
 callbacks = [SaveModelCallback(min_delta=0.005, monitor='accuracy', fname='model_feat01_best')]
learn = tabular_learner(dls, layers=[500,200,100], metrics=[accuracy], config=cfg)
 learn.lr_find()<train_model>
del numerical
del categorical
del final_train
del final_test
learn.fit_one_cycle(5, lr_max=slice(0.004, 5e-2, 0.05))
learn.fit_one_cycle(45, lr_max=slice(0.005, 1e-2, 0.005), cbs=callbacks )<save_to_csv>
n =(all_data.dtypes != 'object')
 num_all_cols = list(n[n].index)
 print(num_all_cols )
test, test_pId = prep_nn_2(test_raw.copy(deep=True))
test_dl = learn.dls.test_dl(test)
 preds, _ = learn.get_preds(dl=test_dl)
 preds = np.argmax(preds, 1 ).numpy()
submission = pd.DataFrame(
 {'PassengerId': test_pId,
'Survived': preds} 
)
 submission.to_csv('submission_feature_eng.csv', index=False )<create_dataframe>
num_cols_to_shrink_all = detect_num_cols_to_shrink(num_all_cols, all_data)
 convert_to_int8 = num_cols_to_shrink_all[0]
convert_to_int16 = num_cols_to_shrink_all[1]
convert_to_int32 = num_cols_to_shrink_all[2]
convert_to_float16 = num_cols_to_shrink_all[3]
convert_to_float32 = num_cols_to_shrink_all[4]
splits = RandomSplitter(valid_pct=0.01, seed=42 )(train.index)
 cfg = tabular_config(embed_p=0.25, ps=0.05)
 dls = TabularPandas(train,
cont_names=cont_names,
cat_names=cat_names,
procs=procs,
y_names=dep_var,
splits=splits ).dataloaders(bs=64)
 learn = tabular_learner(dls, layers=[500,200,100], metrics=[accuracy], config=cfg )<train_model>
print("starting with converting process.... ")
 for col in convert_to_int16:
all_data[col] = all_data[col].astype('int16')
 for col in convert_to_int32:
all_data[col] = all_data[col].astype('int32')
 for col in convert_to_float16:
all_data[col] = all_data[col].astype('float16')
 for col in convert_to_float32:
all_data[col] = all_data[col].astype('float32')
 print("successfully converted!" )
learn.fit_one_cycle(5, lr_max=slice(0.004, 5e-2, 0.05))
learn.fit_one_cycle(15, lr_max=slice(0.005, 1e-2, 0.005))<save_to_csv>
v = [1, 3, 4, 6, 8, 11]
v += [13, 14, 17, 20, 23, 26, 27, 30]
v += [36, 37, 40, 41, 44, 47, 48]
v += [54, 56, 59, 62, 65, 67, 68, 70]
v += [76, 78, 80, 82, 86, 88, 89, 91]
v += [96, 98, 99, 104]
v += [107, 108, 111, 115, 117, 120, 121, 123]
v += [124, 127, 129, 130, 136]
v += [138, 139, 142, 147, 156, 162]
v += [165, 160, 166]
v += [178, 176, 173, 182]
v += [187, 203, 205, 207, 215]
v += [169, 171, 175, 180, 185, 188, 198, 210, 209]
v += [218, 223, 224, 226, 228, 229, 235]
v += [240, 258, 257, 253, 252, 260, 261]
v += [264, 266, 267, 274, 277]
v += [220, 221, 234, 238, 250, 271]
v += [294, 284, 285, 286, 291, 297]
v += [303, 305, 307, 309, 310, 320]
v += [281, 283, 289, 296, 301, 314]
v += [332, 325, 335, 338]
test, test_pId = prep_nn_2(test_raw.copy(deep=True))
test_dl = learn.dls.test_dl(test)
 preds, _ = learn.get_preds(dl=test_dl)
 preds = np.argmax(preds, 1 ).numpy()
submission = pd.DataFrame(
 {'PassengerId': test_pId,
'Survived': preds} 
)
 submission.to_csv('submission_feature_eng_full.csv', index=False )<set_options>
cols = ['V'+str(x)for x in v]
for i in all_data_cols:
if(i.startswith("V")) and i not in cols:
all_data = all_data.drop(columns=[i])
 all_data
pd.options.mode.chained_assignment = None
%matplotlib inline
warnings.filterwarnings('ignore' )<load_from_csv>
for c in ['C3','M5','id_08','id_33']:
cols.remove(c)
 all_data = all_data.drop(columns=[i])
 for c in ['card4','id_07','id_14','id_21','id_30','id_32','id_34']:
cols.remove(c)
 all_data = all_data.drop(columns=[i])
 for c in ['id_'+str(x)for x in range(22,28)]:
cols.remove(c)
 all_data = all_data.drop(columns=[i])
 for c in ['D6','D7','D8','D9','D12','D13','D14']:
cols.remove(c)
 all_data = all_data.drop(columns=[i] )
train_data = pd.read_csv(".. /input/titanic/train.csv", index_col="PassengerId")
 test_data = pd.read_csv(".. /input/titanic/test.csv", index_col="PassengerId" )<count_missing_values>
numerical = all_data.select_dtypes(include='number')
 categorical = all_data.select_dtypes(exclude = 'number')
 numeric_transformer = Pipeline(steps=[('mean',SimpleImputer(strategy='constant',fill_value=-1)) ])
 categorical_transformer = Pipeline(steps=[('constant', SimpleImputer(strategy='constant',fill_value=-1)) ] )
print(train_data.isna().sum() )<count_missing_values>
final_train = all_data[ : ntrain]
final_test = all_data[ntrain : ]
print(test_data.isna().sum() )<sort_values>
del all_data
print(train_data["Pclass"].unique())
 train_data[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<string_transform>
for i,f in enumerate(final_train.columns):
if(np.str(final_train[f].dtype)=='category')|(final_train[f].dtype=='object'):
df_comb = pd.concat([final_train[f],final_test[f]],axis=0)
 df_comb,_ = df_comb.factorize(sort=True)
 if df_comb.max() >32000: print(f,'needs int32')
 final_train[f] = df_comb[:len(final_train)].astype('int16')
 final_test[f] = df_comb[len(final_train):].astype('int16')
 elif f not in ['TransactionAmt','TransactionDT']:
mn = np.min(( final_train[f].min() ,final_test[f].min()))
final_train[f] -= np.float32(mn)
 final_test[f] -= np.float32(mn)
 final_train[f].fillna(-999,inplace=True)
 final_test[f].fillna(-999,inplace=True )
train_data.Name[1].split()<feature_engineering>
y.value_counts()
train_data = train_data.assign(fname = train_data.Name.str.split("," ).str[0])
 train_data["title"] = pd.Series([i.split(",")[1].split(".")[0].strip() for i in train_data.Name], index=train_data.index )<feature_engineering>
model = xgb.XGBClassifier(n_estimators=2000,
max_depth=12,
learning_rate=0.02,
subsample=0.8,
colsample_bytree=0.4,
missing=-1,
eval_metric='auc',
tree_method='hist'
)
test_data = test_data.assign(fname = test_data.Name.str.split("," ).str[0])
 test_data["title"] = pd.Series([i.split(",")[1].split(".")[0].strip() for i in test_data.Name], index=test_data.index)
 train_data.drop("Name", axis=1, inplace=True)
 test_data.drop("Name", axis=1, inplace=True )<count_unique_values>
X_train = final_train
y_train = y
X_test = final_test
print(test_data.fname.nunique())
 print(test_data.title.nunique() )<categorify>
del final_train
del y
del final_test
train_data["title"] = train_data['title'].replace(other_titles, 'Other')
 train_data["title"] = train_data["title"].map({"Mr":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Master":2, "Other":3})
 test_data["title"] = test_data['title'].replace(other_titles, 'Other')
 test_data["title"] = test_data["title"].map({"Mr":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Master":2, "Other":3} )<count_missing_values>
final_col = X_train['TransactionID']
X_train = X_train.drop(columns=['TransactionID'] )
print(train_data.title)
 print(test_data.title.isna().sum() )<categorify>
START_DATE = datetime.datetime.strptime('2017-11-30', '%Y-%m-%d')
 X_train['DT_M'] = X_train['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 X_train['DT_M'] =(X_train['DT_M'].dt.year-2017)*12 + X_train['DT_M'].dt.month
X_test['DT_M'] = X_test['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 X_test['DT_M'] =(X_test['DT_M'].dt.year-2017)*12 + X_test['DT_M'].dt.month
oh = OneHotEncoder(handle_unknown="ignore", sparse = False)
 train_data = train_data.join(pd.DataFrame(oh.fit_transform(train_data[["fname", "title"]]), index = train_data.index))
test_data = test_data.join(pd.DataFrame(oh.transform(test_data[["fname", "title"]]), index = test_data.index))
train_data.drop("fname", axis = 1, inplace = True)
 test_data.drop("fname", axis = 1, inplace = True )<sort_values>
cols = list(X_train.columns)
 cols.remove('TransactionDT' )
print(train_data["Sex"].unique())
 train_data[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
idxT = X_train.index[:3*len(X_train)//4]
idxV = X_train.index[3*len(X_train)//4:]
interactions = train_data.assign(sex_class = train_data['Sex'] + "_" + train_data['Pclass'].astype("str"))
interactions[['sex_class', 'Survived']].groupby(['sex_class'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<data_type_conversions>
oof = np.zeros(len(X_train))
preds = np.zeros(len(X_test))
skf = GroupKFold(n_splits=6)
 for i,(idxT, idxV)in enumerate(skf.split(X_train, y_train, groups=X_train['DT_M'])) :
month = X_train.iloc[idxV]['DT_M'].iloc[0]
print('Fold',i,'withholding month',month)
 print(' rows of train =',len(idxT),'rows of holdout =',len(idxV))
clf = xgb.XGBClassifier(
 n_estimators=5000,
max_depth=12,
learning_rate=0.02,
subsample=0.8,
colsample_bytree=0.4,
missing=-1,
eval_metric='auc',
tree_method='hist' 
)
 h = clf.fit(X_train[cols].iloc[idxT], y_train.iloc[idxT],
eval_set=[(X_train[cols].iloc[idxV],y_train.iloc[idxV])],
verbose=100, early_stopping_rounds=200)
 oof[idxV] += clf.predict_proba(X_train[cols].iloc[idxV])[:,1]
preds += clf.predict_proba(X_test[cols])[:,1]/skf.n_splits
print('
print('XGB96 OOF CV=',roc_auc_score(y_train,oof))
train_data = train_data.assign(sex_class = train_data['Sex'] + "_" + train_data['Pclass'].astype("str"))
test_data = test_data.assign(sex_class = test_data['Sex'] + "_" + test_data['Pclass'].astype("str"))<categorify>
def encode_FE(df1, df2, cols):
for col in cols:
df = pd.concat([df1[col],df2[col]])
 vc = df.value_counts(dropna=True, normalize=True ).to_dict()
vc[-1] = -1
nm = col+'_FE'
df1[nm] = df1[col].map(vc)
 df1[nm] = df1[nm].astype('float32')
 df2[nm] = df2[col].map(vc)
 df2[nm] = df2[nm].astype('float32')
 print(nm,', ',end='')
 def encode_LE(col,train=X_train,test=X_test,verbose=True):
df_comb = pd.concat([train[col],test[col]],axis=0)
 df_comb,_ = df_comb.factorize(sort=True)
 nm = col
if df_comb.max() >32000:
train[nm] = df_comb[:len(train)].astype('int32')
 test[nm] = df_comb[len(train):].astype('int32')
 else:
train[nm] = df_comb[:len(train)].astype('int16')
 test[nm] = df_comb[len(train):].astype('int16')
 del df_comb; x=gc.collect()
if verbose: print(nm,', ',end='')
 def encode_AG(main_columns, uids, aggregations=['mean'], train_df=X_train, test_df=X_test,
fillna=True, usena=False):
for main_column in main_columns:
for col in uids:
for agg_type in aggregations:
new_col_name = main_column+'_'+col+'_'+agg_type
temp_df = pd.concat([train_df[[col, main_column]], test_df[[col,main_column]]])
 if usena: temp_df.loc[temp_df[main_column]==-1,main_column] = np.nan
temp_df = temp_df.groupby([col])[main_column].agg([agg_type] ).reset_index().rename(
 columns={agg_type: new_col_name})
 temp_df.index = list(temp_df[col])
 temp_df = temp_df[new_col_name].to_dict()
train_df[new_col_name] = train_df[col].map(temp_df ).astype('float32')
 test_df[new_col_name] = test_df[col].map(temp_df ).astype('float32')
 if fillna:
train_df[new_col_name].fillna(-1,inplace=True)
 test_df[new_col_name].fillna(-1,inplace=True)
 print("'"+new_col_name+"'",', ',end='')
 def encode_CB(col1,col2,df1=X_train,df2=X_test):
nm = col1+'_'+col2
df1[nm] = df1[col1].astype(str)+'_'+df1[col2].astype(str)
 df2[nm] = df2[col1].astype(str)+'_'+df2[col2].astype(str)
 encode_LE(nm,verbose=False)
 print(nm,', ',end='')
 def encode_AG2(main_columns, uids, train_df=X_train, test_df=X_test):
for main_column in main_columns:
for col in uids:
comb = pd.concat([train_df[[col]+[main_column]],test_df[[col]+[main_column]]],axis=0)
 mp = comb.groupby(col)[main_column].agg(['nunique'])['nunique'].to_dict()
train_df[col+'_'+main_column+'_ct'] = train_df[col].map(mp ).astype('float32')
 test_df[col+'_'+main_column+'_ct'] = test_df[col].map(mp ).astype('float32')
 print(col+'_'+main_column+'_ct, ',end='' )
train_data["Sex"] = train_data["Sex"].map({"female":0, "male":1})
 test_data["Sex"] = test_data["Sex"].map({"female":0, "male":1} )<categorify>
encode_CB('card1','addr1')
 X_train['day'] =X_train.TransactionDT /(24*60*60)
 X_train['uid'] = X_train.card1_addr1.astype(str)+'_'+np.floor(X_train.day-X_train.D1 ).astype(str)
 X_test['day'] = X_test.TransactionDT /(24*60*60)
 X_test['uid'] =X_test.card1_addr1.astype(str)+'_'+np.floor(X_test.day-X_test.D1 ).astype(str )
train_data["sex_class"] = train_data["sex_class"].map({"female_1":0, "female_2":1, "female_3":2, "male_1":4, "male_2":5, "male_3":6})
 test_data["sex_class"] = test_data["sex_class"].map({"female_1":0, "female_2":1, "female_3":2, "male_1":4, "male_2":5, "male_3":6} )<filter>
%%time
X_train['cents'] =(X_train['TransactionAmt'] - np.floor(X_train['TransactionAmt'])).astype('float32')
 X_test['cents'] =(X_test['TransactionAmt'] - np.floor(X_test['TransactionAmt'])).astype('float32')
 encode_FE(X_train,X_test,['uid'])
 encode_AG(['TransactionAmt','D4','D9','D10','D15'],['uid'],['mean','std'],fillna=True,usena=True)
 encode_AG(['C'+str(x)for x in range(1,15)if x!=3],['uid'],['mean'],X_train,X_test,fillna=True,usena=True)
 encode_AG(['M'+str(x)for x in range(1,10)],['uid'],['mean'],fillna=True,usena=True)
 encode_AG2(['P_emaildomain','dist1','DT_M','id_02','cents'], ['uid'], train_df=X_train, test_df=X_test)
 encode_AG(['C14'],['uid'],['std'],X_test,X_test,fillna=True,usena=True)
 encode_AG2(['C13','V314'], ['uid'], train_df=X_train, test_df=X_test)
 encode_AG2(['V127','V136','V309','V307','V320'], ['uid'], train_df=X_train, test_df=X_test)
 X_train['outsider15'] =(np.abs(X_train.D1-X_train.D15)>3 ).astype('int8')
 X_test['outsider15'] =(np.abs(X_test.D1-X_test.D15)>3 ).astype('int8')
 print('outsider15' )
def find_similar_passengers(id, dataset):
subset = dataset[(dataset.title == dataset.title[id])& 
(dataset.Pclass == dataset.Pclass[id])]
if subset["Age"].mean() == "NaN":
subset = dataset[(dataset["sex_class"] == dataset.iloc[id]["sex_class"])]
if subset["Age"].mean() == "NaN":
subset = dataset[(dataset["sex"] == dataset.iloc[id]["sex"])]
age = subset["Age"].mean()
return age<feature_engineering>
for i in range(1,16):
if i in [1,2,3,5,9]: continue
X_train['D'+str(i)] = X_train['D'+str(i)] - X_train.TransactionDT/np.float32(24*60*60)
 X_test['D'+str(i)] = X_test['D'+str(i)] - X_test.TransactionDT/np.float32(24*60*60 )
no_ages = train_data[train_data["Age"].isna() ].index
for pid in no_ages:
train_data.Age[pid] = find_similar_passengers(pid, train_data)
 no_ages_test = test_data[test_data["Age"].isna() ].index
for pid2 in no_ages_test:
test_data.Age[pid2] = find_similar_passengers(pid2, test_data )<data_type_conversions>
cols = list(X_train.columns)
 cols.remove('TransactionDT')
 for c in ['DT_M','day','uid']:
cols.remove(c)
train_data["age_group"] = pd.cut(train_data["Age"], bins=[0,5,65,100], labels=[0,1,2] ).astype("int64")
 test_data["age_group"] = pd.cut(test_data["Age"], bins=[0,5,65,100], labels=[0,1,2] ).astype("int64" )<sort_values>
idxT = X_train.index[:3*len(X_train)//4]
idxV = X_train.index[3*len(X_train)//4:]
train_data[['SibSp', 'Survived']].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
X_train = X_train.drop(columns=['uid'] )
train_data[['Parch', 'Survived']].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<feature_engineering>
idxT = X_train.index[:3*len(X_train)//4]
idxV = X_train.index[3*len(X_train)//4:]
train_data["fsize"] = train_data["SibSp"] + train_data["Parch"] + 1
test_data["fsize"] = test_data["SibSp"] + test_data["Parch"] + 1<sort_values>
oof = np.zeros(len(X_train))
preds = np.zeros(len(X_test))
skf = GroupKFold(n_splits=6)
 for i,(idxT, idxV)in enumerate(skf.split(X_train, y_train, groups=X_train['DT_M'])) :
month = X_train.iloc[idxV]['DT_M'].iloc[0]
print('Fold',i,'withholding month',month)
 print(' rows of train =',len(idxT),'rows of holdout =',len(idxV))
clfz = xgb.XGBClassifier(
 n_estimators=5000,
max_depth=12,
learning_rate=0.02,
subsample=0.8,
colsample_bytree=0.4,
missing=-1,
eval_metric='auc',
tree_method='hist' 
)
 hz = clfz.fit(X_train[cols].iloc[idxT], y_train.iloc[idxT],
eval_set=[(X_train[cols].iloc[idxV],y_train.iloc[idxV])],
verbose=100, early_stopping_rounds=200)
 oof[idxV] += clfz.predict_proba(X_train[cols].iloc[idxV])[:,1]
preds += clfz.predict_proba(X_test[cols])[:,1]/skf.n_splits
print('
print('XGB96 OOF CV=',roc_auc_score(y_train,oof))
train_data[['fsize', 'Survived']].groupby(['fsize'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<count_unique_values>
test_transaction = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_transaction.csv')
 final_col = test_transaction['TransactionID']
<feature_engineering><EOS> 
my_submission = pd.DataFrame({'TransactionID':final_col, 'isFraud': preds})
 my_submission.to_csv('./submission.csv', index=False)
 my_submission.head(5 )