Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<prepare_x_and_y>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
print('
print('KFold training...')
 folds = KFold(n_splits=N_SPLITS, shuffle=True, random_state=SEED)
 X,y = train_df[features_columns], train_df[TARGET]
P = test_df[features_columns]
RESULTS['kfold'] = 0
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X, y)) :
print('Fold:',fold_+1)
 tr_x, tr_y = X.iloc[trn_idx,:], y[trn_idx]
vl_x, v_y = X.iloc[val_idx,:], y[val_idx]
train_data = lgb.Dataset(tr_x, label=tr_y)
 valid_data = lgb.Dataset(vl_x, label=v_y)
 estimator = lgb.train(
 lgb_params,
train_data,
valid_sets = [train_data, valid_data],
verbose_eval = 1000, 
)
 RESULTS['kfold'] = estimator.predict(P)
 print('AUC score', metrics.roc_auc_score(RESULTS[TARGET], RESULTS['kfold']))
print(' 
<split>
ds = pd.read_csv("/kaggle/input/titanic/train.csv" )
print('
print('StratifiedKFold training...')
 folds = StratifiedKFold(n_splits=N_SPLITS, shuffle=True, random_state=SEED)
 X,y = train_df[features_columns], train_df[TARGET]
P = test_df[features_columns]
RESULTS['stratifiedkfold'] = 0
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X, y, groups=y)) :
print('Fold:',fold_+1)
 tr_x, tr_y = X.iloc[trn_idx,:], y[trn_idx]
vl_x, v_y = X.iloc[val_idx,:], y[val_idx]
train_data = lgb.Dataset(tr_x, label=tr_y)
 valid_data = lgb.Dataset(vl_x, label=v_y)
 estimator = lgb.train(
 lgb_params,
train_data,
valid_sets = [train_data, valid_data],
verbose_eval = 1000, 
)
 RESULTS['stratifiedkfold'] += estimator.predict(P)/N_SPLITS
print('AUC score', metrics.roc_auc_score(RESULTS[TARGET], RESULTS['stratifiedkfold']))
print(' 
<feature_engineering>
Y = ds.Survived
X = ds.drop(['PassengerId','Name','Survived'],axis = 1 )
print('
print('LBO training...')
 train_df['DT_M'] = train_df['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 train_df['DT_M'] =(train_df['DT_M'].dt.year-2017)*12 + train_df['DT_M'].dt.month
main_train_set = train_df[train_df['DT_M']<(train_df['DT_M'].max())].reset_index(drop=True)
 validation_set = train_df[train_df['DT_M']==train_df['DT_M'].max() ].reset_index(drop=True)
 folds = KFold(n_splits=N_SPLITS, shuffle=True, random_state=SEED)
 X,y = main_train_set[features_columns], main_train_set[TARGET]
v_X, v_y = validation_set[features_columns], validation_set[TARGET]
estimators_bestround = []
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X, y)) :
print('Fold:',fold_+1)
 tr_x, tr_y = X.iloc[trn_idx,:], y[trn_idx]
train_data = lgb.Dataset(tr_x, label=tr_y)
 valid_data = lgb.Dataset(v_X, label=v_y)
 estimator = lgb.train(
 lgb_params,
train_data,
valid_sets = [train_data, valid_data],
verbose_eval = 1000, 
)
 estimators_bestround.append(estimator.current_iteration())
 corrected_lgb_params = lgb_params.copy()
corrected_lgb_params['n_estimators'] = int(np.mean(estimators_bestround))
corrected_lgb_params['early_stopping_rounds'] = None
print('
print('Mean Best round:', corrected_lgb_params['n_estimators'])
 X,y = train_df[features_columns], train_df[TARGET]
P = test_df[features_columns]
RESULTS['lbo'] = 0
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X, y)) :
print('Fold:',fold_+1)
 tr_x, tr_y = X.iloc[trn_idx,:], y[trn_idx]
train_data = lgb.Dataset(tr_x, label=tr_y)
 estimator = lgb.train(
 corrected_lgb_params,
train_data 
)
 RESULTS['lbo'] += estimator.predict(P)/N_SPLITS
print('AUC score', metrics.roc_auc_score(RESULTS[TARGET], RESULTS['lbo']))
print('<feature_engineering>
print(X.isnull().sum() )
print('
print('GroupKFold timeblocks split training...')
 folds = GroupKFold(n_splits=N_SPLITS)
 train_df['groups'] = train_df['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 train_df['groups'] =(train_df['groups'].dt.year-2017)*12 + train_df['groups'].dt.month
X,y = train_df[features_columns], train_df[TARGET]
split_groups = train_df['groups']
P = test_df[features_columns]
RESULTS['groupkfold_timeblocks'] = 0
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X, y, groups=split_groups)) :
print('Fold:',fold_+1)
 tr_x, tr_y = X.iloc[trn_idx,:], y[trn_idx]
vl_x, v_y = X.iloc[val_idx,:], y[val_idx]
train_data = lgb.Dataset(tr_x, label=tr_y)
 valid_data = lgb.Dataset(vl_x, label=v_y)
 estimator = lgb.train(
 lgb_params,
train_data,
valid_sets = [train_data, valid_data],
verbose_eval = 1000, 
)
 RESULTS['groupkfold_timeblocks'] += estimator.predict(P)/N_SPLITS
print('AUC score', metrics.roc_auc_score(RESULTS[TARGET], RESULTS['groupkfold_timeblocks']))
print('<prepare_x_and_y>
X_temp = X
Sex_Embarked = {"Sex":{"male": 1.,"female": 0.},
"Embarked":{"S": 0.,"C": 1.,"Q": 2.}}
X_temp = X_temp.replace(Sex_Embarked, inplace=False)
 CT = ColumnTransformer(transformers = [('encoder',OrdinalEncoder() ,['Ticket'])],remainder = 'passthrough')
 X_temp = pd.DataFrame(CT.fit_transform(X_temp.drop(['Cabin'],axis = 1,inplace = False ).dropna(subset = ['Age','Embarked'],axis =0 ,inplace = False)) ,
columns=[ 'Ticket', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare','Embarked'],
dtype = 'float' )
print('
print('GroupKFold uID split training...')
 folds = GroupKFold(n_splits=N_SPLITS)
 train_df['groups'] = ''
for col in ['card1','card2','card3','card5','addr1','addr2',]:
train_df['groups'] = '_' + train_df[col].astype(str)
 X,y = train_df[features_columns], train_df[TARGET]
split_groups = train_df['groups']
P = test_df[features_columns]
RESULTS['groupkfold_uid'] = 0
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X, y, groups=split_groups)) :
print('Fold:',fold_+1)
 tr_x, tr_y = X.iloc[trn_idx,:], y[trn_idx]
vl_x, v_y = X.iloc[val_idx,:], y[val_idx]
train_data = lgb.Dataset(tr_x, label=tr_y)
 valid_data = lgb.Dataset(vl_x, label=v_y)
 estimator = lgb.train(
 lgb_params,
train_data,
valid_sets = [train_data, valid_data],
verbose_eval = 1000, 
)
 RESULTS['groupkfold_uid'] += estimator.predict(P)/N_SPLITS
print('AUC score', metrics.roc_auc_score(RESULTS[TARGET], RESULTS['groupkfold_uid']))
print('<create_dataframe>
imputer = SimpleImputer(missing_values=np.nan, strategy = 'mean')
 ds.Age = imputer.fit_transform(ds.Age.values.reshape(-1,1))
X.Age = imputer.fit_transform(X.Age.values.reshape(-1,1))
print('
print('Intermediate results...')
 final_df = []
for current_strategy in list(RESULTS.iloc[:,2:]):
auc_score = metrics.roc_auc_score(RESULTS[TARGET], RESULTS[current_strategy])
 final_df.append([current_strategy, auc_score])
 final_df = pd.DataFrame(final_df, columns=['Stategy', 'Result'])
 final_df.sort_values(by=['Result'], ascending=False, inplace=True)
 print(final_df )<feature_engineering>
ds.Embarked = SimpleImputer(missing_values=np.nan, strategy = 'most_frequent' ).fit_transform(ds.Embarked.values.reshape(-1,1))
X.Embarked = SimpleImputer(missing_values=np.nan, strategy = 'most_frequent' ).fit_transform(X.Embarked.values.reshape(-1,1))
print('
print('LBO full set training...')
 train_df['DT_M'] = train_df['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 train_df['DT_M'] =(train_df['DT_M'].dt.year-2017)*12 + train_df['DT_M'].dt.month
main_train_set = train_df[train_df['DT_M']<(train_df['DT_M'].max())].reset_index(drop=True)
 validation_set = train_df[train_df['DT_M']==train_df['DT_M'].max() ].reset_index(drop=True)
 X,y = main_train_set[features_columns], main_train_set[TARGET]
v_X, v_y = validation_set[features_columns], validation_set[TARGET]
estimators_bestround = []
for current_model in range(3):
print('Model:',current_model+1)
 SEED += 1
seed_everything(SEED)
 corrected_lgb_params = lgb_params.copy()
corrected_lgb_params['seed'] = SEED
train_data = lgb.Dataset(X, label=y)
 valid_data = lgb.Dataset(v_X, label=v_y)
 estimator = lgb.train(
 corrected_lgb_params,
train_data,
valid_sets = [train_data, valid_data],
verbose_eval = 1000, 
)
 estimators_bestround.append(estimator.current_iteration())
 corrected_lgb_params = lgb_params.copy()
corrected_lgb_params['n_estimators'] = int(np.mean(estimators_bestround))
corrected_lgb_params['early_stopping_rounds'] = None
print('
print('Mean Best round:', corrected_lgb_params['n_estimators'])
 X,y = train_df[features_columns], train_df[TARGET]
P = test_df[features_columns]
RESULTS['lbo_full'] = 0
NUMBER_OF_MODELS = 3
for current_model in range(NUMBER_OF_MODELS):
print('Model:',current_model+1)
 SEED += 1
seed_everything(SEED)
 train_data = lgb.Dataset(X, label=y)
 estimator = lgb.train(
 corrected_lgb_params,
train_data 
)
 RESULTS['lbo_full'] += estimator.predict(P)/NUMBER_OF_MODELS
print('AUC score', metrics.roc_auc_score(RESULTS[TARGET], RESULTS['lbo_full']))
print('<create_dataframe>
X_Cabins = X.Cabin.dropna().str.contains('C',regex = False)
 X_Cabins = X.Cabin[X_Cabins.loc[X_Cabins == True].index]
print(X_Cabins )
print('
print('Intermediate results...')
 final_df = []
for current_strategy in list(RESULTS.iloc[:,2:]):
auc_score = metrics.roc_auc_score(RESULTS[TARGET], RESULTS[current_strategy])
 final_df.append([current_strategy, auc_score])
 final_df = pd.DataFrame(final_df, columns=['Stategy', 'Result'])
 final_df.sort_values(by=['Result'], ascending=False, inplace=True)
 print(final_df )<feature_engineering>
X = X.drop(['Cabin'],axis = 1)
 X
test_df['DT_W'] = test_df['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 RESULTS['DT_W'] =(test_df['DT_W'].dt.year-2017)*52 + test_df['DT_W'].dt.weekofyear
for curent_time_block in range(RESULTS['DT_W'].min() , RESULTS['DT_W'].max() +1):
print('
print('Time Block:', curent_time_block)
 final_df = []
temp_df = RESULTS[RESULTS['DT_W']==curent_time_block]
for current_strategy in list(temp_df.iloc[:,2:]):
auc_score = metrics.roc_auc_score(temp_df[TARGET], temp_df[current_strategy])
 final_df.append([current_strategy, auc_score])
 final_df = pd.DataFrame(final_df, columns=['Stategy', 'Result'])
 final_df.sort_values(by=['Result'], ascending=False, inplace=True)
 print(final_df)
 print(' 
<save_to_csv>
X_temp = X
Sex_Embarked = {"Sex":{"male": 1.,"female": 0.},
"Embarked":{"S": 0.,"C": 1.,"Q": 2.},
"Deck":{"A": 1.,"B": 2.,"C": 3.,
"D": 4.,"E": 5.,"F": 6.,
"G": 7.,"T": 8.}}
X_temp = X_temp.replace(Sex_Embarked, inplace=False)
 X_temp = pd.DataFrame(CT.fit_transform(X_temp),
columns=[ 'Ticket', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare','Embarked','Deck'],
dtype = 'float' )
print('
print('Small bonus')
 test_df = pd.read_pickle('.. /input/ieee-data-minification/test_transaction.pkl')
 kernel_with_identity = pd.read_csv('.. /input/ieee-gb-2-make-amount-useful-again/submission.csv')
 kernel_no_identity = pd.read_csv('.. /input/ieee-experimental/submission.csv')
 test_df = test_df[['TransactionID']]
test_df['isFraud'] = kernel_with_identity['isFraud'] + kernel_no_identity['isFraud']
test_df[['TransactionID','isFraud']].to_csv('submission.csv', index=False )<import_modules>
X_temp = X_temp.drop(['Deck'],axis = 1)
import numpy as np
import pandas as pd
from shutil import copyfile
import xgboost as xgb<prepare_x_and_y>
X_temp['family_Size'] = X_temp.Parch + X_temp.SibSp
X_temp = X_temp.drop(['Parch','SibSp'],axis = 1 )
X_train, y_train, X_test, submission = a1w.quick_wrangle()<save_to_csv>
mean = ['{0}, {1:.0f}'.format(pclass,np.nanmean(X.where(X.Pclass == pclass,inplace = False ).Fare)) for pclass in X.Pclass.unique() ]
print(mean )
for n in range(100, 501, 200):
clf = xgb.XGBClassifier(n_estimators=n, n_jobs=4, max_depth=10, learning_rate=0.03, subsample=0.9, colsample_bytree=0.9, missing=-999)
 clf.fit(X_train, y_train)
 submission['isFraud'] = clf.predict_proba(X_test)[:, 1]
submission.to_csv('XGBoost' + str(n)+ '.csv' )<set_options>
X_temp = X_temp.drop(['Ticket'],axis = 1 )
warnings.filterwarnings("ignore")
 %matplotlib inline<feature_engineering>
X_train, X_valid, Y_train, Y_valid = train_test_split(X_temp,Y,test_size = 0.25,random_state = 1)
 logistic = LogisticRegression(C=1, penalty="l1", solver='liblinear', random_state=7 ).fit(X_train,Y_train)
 model = SelectFromModel(logistic, prefit=True)
 X_new = model.transform(X_train)
 X_new
LABELS = ["isFraud"]
all_files = glob.glob(".. /input/lgmodels/*.csv")
 scores = np.zeros(len(all_files))
for i in range(len(all_files)) :
scores[i] = float('.'+all_files[i].split(".")[3])
 print(i,scores[i],all_files[i] )<sort_values>
selected_features = pd.DataFrame(model.inverse_transform(X_new),
index=X_train.index,
columns=X_train.columns)
 selected_columns = selected_features.columns[selected_features.var() != 0]
selected_columns
top = scores.argsort() [::-1]
for i, f in enumerate(top):
print(i,scores[f],all_files[f] )<load_from_csv>
cm2 = confusion_matrix(Y_valid,logistic.predict(X_valid))
log_acc = accuracy_score(Y_valid,logistic.predict(X_valid))
print(log_acc,'
',cm2 )
outs = [pd.read_csv(all_files[f], index_col=0)for f in top]
concat_sub = pd.concat(outs, axis=1)
 cols = list(map(lambda x: "m" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols<feature_engineering>
sc = MinMaxScaler()
scaled_X_temp = pd.DataFrame(sc.fit_transform(X_temp),columns = X_temp.columns)
 scaled_X_temp
rank = np.tril(corr.values,-1)
 rank[rank<0.92] = 1
m =(rank>0 ).sum() -(rank>0.97 ).sum()
m_gmean, s = 0, 0
for n in range(m):
mx = np.unravel_index(rank.argmin() , rank.shape)
 w =(m-n)/m
m_gmean += w*(np.log(concat_sub.iloc[:,mx[0]])+np.log(concat_sub.iloc[:,mx[1]])) /2
s += w
rank[mx] = 1
m_gmean = np.exp(m_gmean/s )<save_to_csv>
X_train, X_valid, Y_train, Y_valid = train_test_split(scaled_X_temp,Y,test_size = 0.25,random_state = 1)
 knn_Classifier = KNeighborsClassifier(n_neighbors = 7, metric = 'minkowski', p=2)
 knn_Classifier.fit(X_train,Y_train)
 Y_pred_knn = knn_Classifier.predict(X_valid)
 cm2 = confusion_matrix(Y_valid,Y_pred_knn)
 knn_acc = accuracy_score(Y_valid,Y_pred_knn)
 print(knn_acc)
 print(cm2 )
concat_sub['isFraud'] = m_gmean
concat_sub[['isFraud']].to_csv('stack_gmean.csv' )<set_options>
cv_knn_score = cross_val_score(knn_Classifier,scaled_X_temp,Y,cv = 10,scoring='accuracy' ).mean()
print(cv_knn_score )
warnings.simplefilter('ignore')
 sns.set()
%matplotlib inline<load_from_csv>
svm_Classifier = SVC(kernel = 'rbf', random_state = 0)
 cv_svm_score = cross_val_score(svm_Classifier,scaled_X_temp,Y,cv = 10,scoring='accuracy' ).mean()
print(cv_svm_score )
%%time
warnings.simplefilter('ignore')
 files = ['.. /input/ieee-fraud-detection/test_identity.csv',
'.. /input/ieee-fraud-detection/test_transaction.csv',
'.. /input/ieee-fraud-detection/train_identity.csv',
'.. /input/ieee-fraud-detection/train_transaction.csv',
'.. /input/ieee-fraud-detection/sample_submission.csv']
def load_data(file):
return reduce_mem_usage(pd.read_csv(file))
with multiprocessing.Pool() as pool:
test_identity, test_transaction, train_identity, train_transaction, sample_submission = pool.map(load_data, files )<count_values>
nb_Classifier = GaussianNB()
cv_nb_score = cross_val_score(nb_Classifier,scaled_X_temp,Y,cv = 10,scoring='accuracy' ).mean()
print(cv_nb_score )
train_transaction.loc[train_transaction.card3.isin(train_transaction.card3.value_counts() [train_transaction.card3.value_counts() < 200].index), 'card3'] = "Others"
train_transaction.loc[train_transaction.card5.isin(train_transaction.card5.value_counts() [train_transaction.card5.value_counts() < 300].index), 'card5'] = "Others"<feature_engineering>
dt_Classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
 cv_dt_score = cross_val_score(dt_Classifier,scaled_X_temp,Y,cv = 10,scoring='accuracy' ).mean()
print(cv_dt_score )
train_transaction.loc[train_transaction.addr1.isin(train_transaction.addr1.value_counts() [train_transaction.addr1.value_counts() <= 5000 ].index), 'addr1'] = "Others"
train_transaction.loc[train_transaction.addr2.isin(train_transaction.addr2.value_counts() [train_transaction.addr2.value_counts() <= 50 ].index), 'addr2'] = "Others"<feature_engineering>
rf_Classifier = RandomForestClassifier(n_estimators=10, max_depth=None, random_state=0)
 cv_rf_score = cross_val_score(rf_Classifier,scaled_X_temp, Y,cv = 8,scoring='accuracy' ).mean()
print(cv_rf_score )
train_transaction.loc[train_transaction['P_emaildomain'].isin(['gmail.com', 'gmail']),'P_emaildomain'] = 'Google'
train_transaction.loc[train_transaction['P_emaildomain'].isin(['yahoo.com', 'yahoo.com.mx', 'yahoo.co.uk',
'yahoo.co.jp', 'yahoo.de', 'yahoo.fr',
'yahoo.es']), 'P_emaildomain'] = 'Yahoo Mail'
train_transaction.loc[train_transaction['P_emaildomain'].isin(['hotmail.com','outlook.com','msn.com', 'live.com.mx',
'hotmail.es','hotmail.co.uk', 'hotmail.de',
'outlook.es', 'live.com', 'live.fr',
'hotmail.fr']), 'P_emaildomain'] = 'Microsoft'
train_transaction.loc[train_transaction.P_emaildomain.isin(train_transaction.P_emaildomain\ 
.value_counts() [train_transaction.P_emaildomain.value_counts() <= 500 ]\ 
.index), 'P_emaildomain'] = "Others"
train_transaction.P_emaildomain.fillna("NoInf", inplace=True )<concatenate>
XGB_Classifier = XGBClassifier(n_estimators = 1000,learning_rate = 0.01)
 cv_XGB_score = cross_val_score(XGB_Classifier,scaled_X_temp, Y,cv = 18,scoring='accuracy' ).mean()
print(cv_XGB_score )
ploting_cnt_amt(train_transaction, 'R_emaildomain' )<count_values>
eclf = VotingClassifier(
 estimators=[('xgb',XGB_Classifier),('lr', logistic),('rf', rf_Classifier),('dt', dt_Classifier),('svm',svm_Classifier)],
voting='hard' 
)
 for clf, label in zip([XGB_Classifier, logistic, rf_Classifier, dt_Classifier,svm_Classifier, eclf],
['XGBooster Classifier', 'Logistic Regression', 'Random Forest', 'Decision Tree','SVM Classifier', 'Ensemble']):
scores = cross_val_score(clf, scaled_X_temp, Y, scoring='accuracy', cv=10)
 print("Accuracy: %0.2f(+/- %0.2f)[%s]" %(scores.mean() , scores.std() , label))
train_transaction.loc[train_transaction.C1.isin(train_transaction.C1\ 
.value_counts() [train_transaction.C1.value_counts() <= 400 ]\ 
.index), 'C1'] = "Others"<count_values>
params = {'lr__C': [1.0, 100.0], 'rf__n_estimators': [20, 200],
'xgb__n_estimators': [500,2000],'xgb__learning_rate': [0.01,0.1]}
rands = RandomizedSearchCV(estimator=eclf, param_distributions=params, cv=5)
 rands = rands.fit(scaled_X_temp, Y)
 Y_pred_rands = rands.predict(X_valid)
 rands_cm = confusion_matrix(Y_valid,Y_pred_rands)
 rands_acc = accuracy_score(Y_valid,Y_pred_rands)
 print(rands_acc)
 print(rands_cm )
train_transaction.loc[train_transaction.C2.isin(train_transaction.C2\ 
.value_counts() [train_transaction.C2.value_counts() <= 350 ]\ 
.index), 'C2'] = "Others"<concatenate>
test_set = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_set
ploting_cnt_amt(train_transaction, 'C2' )<feature_engineering>
test = test_set.copy()
test['family_Size'] = test['SibSp'] + test['Parch']
test = test.drop(['Name','SibSp','Parch','Cabin','Ticket'],axis = 1)
 encoded_features = {'Sex' : {'male': 1,'female': 0 },
'Embarked':{'S': 0.,'C': 1.,'Q': 2.}}
test.replace(encoded_features, inplace = True)
 test
START_DATE = '2017-12-01'
startdate = datetime.datetime.strptime(START_DATE, "%Y-%m-%d")
 train_transaction["Date"] = train_transaction['TransactionDT'].apply(lambda x:(startdate + datetime.timedelta(seconds=x)))
 train_transaction['_Weekdays'] = train_transaction['Date'].dt.dayofweek
train_transaction['_Hours'] = train_transaction['Date'].dt.hour
train_transaction['_Days'] = train_transaction['Date'].dt.day
test_transaction["Date"] = test_transaction['TransactionDT'].apply(lambda x:(startdate + datetime.timedelta(seconds=x)))
 test_transaction['_Weekdays'] = test_transaction['Date'].dt.dayofweek
test_transaction['_Hours'] = test_transaction['Date'].dt.hour
test_transaction['_Days'] = test_transaction['Date'].dt.day<set_options>
test.Age = imputer.fit_transform(test.Age.values.reshape(-1,1))
notnull_samples = test[test.columns].dropna()
X_set = notnull_samples.loc[:,['Pclass', 'Sex', 'Age', 'Embarked','family_Size']]
Y_set = notnull_samples.loc[:,['Fare']]
linreg = LinearRegression()
linreg.fit(X_set, Y_set)
 fare_predict = linreg.predict(test.loc[test.Fare.isnull() ,X_set.columns])
 test.loc[test.Fare.isnull() ,'Fare'] = fare_predict
color_op = ['
'
'
dates_temp = train_transaction.groupby(train_transaction.Date.dt.date)['TransactionAmt'].count().reset_index()
trace = go.Scatter(x=dates_temp['Date'], y=dates_temp.TransactionAmt,
opacity = 0.8, line = dict(color = color_op[7]), name= 'Total Transactions')
 dates_temp_sum = train_transaction.groupby(train_transaction.Date.dt.date)['TransactionAmt'].sum().reset_index()
trace1 = go.Scatter(x=dates_temp_sum.Date, line = dict(color = color_op[1]), name="Total Amount",
y=dates_temp_sum['TransactionAmt'], opacity = 0.8, yaxis='y2')
 layout = dict(
 title= "Total Transactions and Fraud Informations by Date",
xaxis=dict(
 rangeselector=dict(
 buttons=list([
dict(count=1, label='1m', step='month', stepmode='backward'),
dict(count=3, label='3m', step='month', stepmode='backward'),
dict(count=6, label='6m', step='month', stepmode='backward'),
dict(step='all')
 ])
),
rangeslider=dict(visible = True),
type='date'),
yaxis=dict(title='Total Transactions'),
yaxis2=dict(overlaying='y',
anchor='x', side='right',
zeroline=False, showgrid=False,
title='Total Transaction Amount')
)
 fig = dict(data= [trace, trace1,], layout=layout)
 iplot(fig )<groupby>
scaled_test = pd.concat([test.loc[:,['PassengerId']],pd.DataFrame(sc.fit_transform(test.drop(['PassengerId'],axis = 1)) ,
columns = test.drop(['PassengerId'],axis = 1 ).columns)],axis = 1)
color_op = ['
'
'
tmp_amt = train_transaction.groupby([train_transaction.Date.dt.date, 'isFraud'])['TransactionAmt'].sum().reset_index()
tmp_trans = train_transaction.groupby([train_transaction.Date.dt.date, 'isFraud'])['TransactionAmt'].count().reset_index()
tmp_trans_fraud = tmp_trans[tmp_trans['isFraud'] == 1]
tmp_amt_fraud = tmp_amt[tmp_amt['isFraud'] == 1]
dates_temp = train_transaction.groupby(train_transaction.Date.dt.date)['TransactionAmt'].count().reset_index()
trace = go.Scatter(x=tmp_trans_fraud['Date'], y=tmp_trans_fraud.TransactionAmt,
opacity = 0.8, line = dict(color = color_op[1]), name= 'Fraud Transactions')
 trace1 = go.Scatter(x=tmp_amt_fraud.Date, line = dict(color = color_op[7]), name="Fraud Amount",
y=tmp_amt_fraud['TransactionAmt'], opacity = 0.8, yaxis='y2')
 layout = dict(
 title= "FRAUD TRANSACTIONS - Total Transactions and Fraud Informations by Date",
xaxis=dict(
 rangeselector=dict(
 buttons=list([
dict(count=1, label='1m', step='month', stepmode='backward'),
dict(count=3, label='3m', step='month', stepmode='backward'),
dict(count=6, label='6m', step='month', stepmode='backward'),
dict(step='all')
 ])
),
rangeslider=dict(visible = True),
type='date'),
yaxis=dict(title='Total Transactions'),
yaxis2=dict(overlaying='y',
anchor='x', side='right',
zeroline=False, showgrid=False,
title='Total Transaction Amount')
)
 fig = dict(data= [trace, trace1], layout=layout)
 iplot(fig )<define_variables>
eclf.fit(scaled_X_temp,Y)
 Survived = pd.DataFrame(eclf.predict(scaled_test.drop(['PassengerId'],axis = 1)) ,columns = ['Survived'])
 Survived
def seed_everything(seed=0):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed )<find_best_model_class>
submission = pd.concat([scaled_test.loc[:,'PassengerId'],Survived],axis =1)
 submission
def make_predictions(tr_df, tt_df, features_columns, target, lgb_params, NFOLDS=6):
folds = GroupKFold(n_splits=NFOLDS)
 X,y = tr_df[features_columns], tr_df[target]
P,P_y = tt_df[features_columns], tt_df[target]
split_groups = tr_df['DT_M']
tt_df = tt_df[['TransactionID',target]]
predictions = np.zeros(len(tt_df))
oof = np.zeros(len(tr_df))
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X, y, groups=split_groups)) :
print('Fold:',fold_)
 tr_x, tr_y = X.iloc[trn_idx,:], y[trn_idx]
vl_x, vl_y = X.iloc[val_idx,:], y[val_idx]
print(len(tr_x),len(vl_x))
tr_data = lgb.Dataset(tr_x, label=tr_y)
 vl_data = lgb.Dataset(vl_x, label=vl_y)
 estimator = lgb.train(
 lgb_params,
tr_data,
valid_sets = [tr_data, vl_data],
verbose_eval = 200, 
)
 pp_p = estimator.predict(P)
 predictions += pp_p/NFOLDS
oof_preds = estimator.predict(vl_x)
 oof[val_idx] =(oof_preds - oof_preds.min())/(oof_preds.max() - oof_preds.min())
 if LOCAL_TEST:
feature_imp = pd.DataFrame(sorted(zip(estimator.feature_importance() ,X.columns)) , columns=['Value','Feature'])
 print(feature_imp)
 del tr_x, tr_y, vl_x, vl_y, tr_data, vl_data
gc.collect()
tt_df['prediction'] = predictions
print('OOF AUC:', metrics.roc_auc_score(y, oof))
if LOCAL_TEST:
print('Holdout AUC:', metrics.roc_auc_score(tt_df[TARGET], tt_df['prediction']))
return tt_df<define_variables>
submission.to_csv('submission.csv', index=False )
<feature_engineering><EOS> 
submission.to_csv('submission.csv', index=False )