Kaggle competition: IEEE-CIS Fraud Detection
A = np.array([[1, 3, 5], [5, 4, 1], [3, 8, 6]])
 print(A)
 cov_matrix = np.cov(A, rowvar=False, bias=True)
 cov_matrix 
<set_options>
from sklearn import preprocessing
import xgboost as xgb
from sklearn.model_selection import KFold , GridSearchCV
from sklearn.metrics import roc_auc_score
import gc
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("dark")
<load_from_csv>
%%time
train_transaction = pd.read_csv('.. /input/train_transaction.csv', index_col='TransactionID' ,nrows = 200000)
 test_transaction = pd.read_csv('.. /input/test_transaction.csv', index_col='TransactionID')
 train_identity = pd.read_csv('.. /input/train_identity.csv', index_col='TransactionID')
 test_identity = pd.read_csv('.. /input/test_identity.csv', index_col='TransactionID')
train_df_org = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_df_org = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 print("train_df_org shape ", train_df_org.shape)
 print("test_df_org shape ", test_df_org.shape)
 train_df_org.head()<normalization>
%%time
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)
standardized_data = StandardScaler().fit_transform(train_df_org)
 print(standardized_data.shape )<concatenate>
%%time
Y_train = train['isFraud'].copy()
X_train = train.drop('isFraud' , axis = 1)
 X_test = test.copy()
del train_transaction, train_identity, test_transaction, test_identity
del train , test
gc.collect()
projected_vec = np.matmul(eigenvectors, sample_data.T)
 projected_vec.shape 
<prepare_output>
for i in X_train.columns:
if X_train[i].dtype == 'object' or X_test[i].dtype == 'object':
lbl = preprocessing.LabelEncoder()
lbl.fit(list(X_train[i].values)+ list(X_test[i].values))
X_train[i] = lbl.transform(list(X_train[i].values))
X_test[i] = lbl.transform(list(X_test[i].values))
pca_dataframe = pd.DataFrame(data=projected_vec, columns=('1st_principal_comp', "2nd_principal_comp", 'labels'))
pca_dataframe.head() 
<normalization>
for i in X_train.columns:
if X_train[i].dtype == 'object' or X_test[i].dtype == 'object':
print(i )
pca = decomposition.PCA()
pca.n_components = 2
pca_data_with_scikit = pca.fit_transform(standardized_data)
 pca_data_with_scikit.shape 
<prepare_output>
%%time
X_train = reduce_mem_usage(X_train)
 X_test = reduce_mem_usage(X_test )
df_PCA_scikit = pd.DataFrame(data=pca_data_with_scikit, columns=('f1_PC', 'f2_PC', 'labels'))
df_PCA_scikit.head()<import_modules>
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.models import Model
from tensorflow.keras import metrics
from tensorflow.keras import backend as K
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Lambda, Flatten, BatchNormalization
from tensorflow.keras.layers import Conv2D, MaxPool2D, AvgPool2D
from tensorflow.keras.optimizers import Adadelta
from keras.utils.np_utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.callbacks import LearningRateScheduler<load_from_csv>
train_corr = X_train.corr().abs() *100
train_corr = train_corr.where(np.triu(np.ones(train_corr.shape)).astype(np.bool))
train_corr.values[[np.arange(train_corr.shape[0])]*2] = np.nan
print(train_corr.shape)
 counter =0
columns_drop =[]
train_corr_matrix = train_corr.values
for i in range(1 , train_corr.shape[0] ,1):
for j in range(i , train_corr.shape[0] , 1):
if train_corr_matrix[i][j] >= 98:
counter+=1
columns_drop.append(train_corr.columns[j])
 if counter%20 ==0:
print('Comman Columns pair reached...' , counter)
 print(' Total Common Pair Found.... ',counter )
train_df_org = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 mnist_train_label = train_df_org.loc[:, "label"]
mnist_train_df = train_df_org.loc[:, "pixel0":] 
<categorify>
sample_submission = pd.read_csv('.. /input/sample_submission.csv', index_col='TransactionID' )
num_of_digit_classes = mnist_train_label_array.max() - mnist_train_label_array.min() + 1
mnist_train_label_array = to_categorical(mnist_train_label_array, num_classes=num_of_digit_classes)
 print('Shape of ytrain after encoding and converting to categorical values ', mnist_train_label_array.shape)
 print(mnist_train_label_array)
<choose_model_class>
Y_train.value_counts()
def run_model(input_shape=(28, 28, 1)) :
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape = input_shape))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
return model
def run_model_compilation(model, optimizer='adam', loss='categorical_crossentropy'):
model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
 def run_model_training(model, train, test, epochs, split):
history=model.fit(train, test, shuffle=True, epochs=epochs, validation_split=split)
 return history<train_model>
EPOCHS = 5
y_pred = np.zeros(sample_submission.shape[0])
 y_oof = np.zeros(X_train.shape[0])
 kf = KFold(n_splits = EPOCHS , shuffle = True)
 for x_train_index , x_val_index in kf.split(X_train , Y_train):
clf = xgb.XGBClassifier(
 n_estimators=500,
max_depth=4,
learning_rate=0.005,
subsample=0.2,
colsample_bytree = 0.2 
)
 x_tr , x_val = X_train.iloc[x_train_index , :] , X_train.iloc[x_val_index,:]
y_tr , y_val = Y_train.iloc[x_train_index] , Y_train.iloc[x_val_index]
clf.fit(x_tr,y_tr)
 y_pred_train = clf.predict_proba(x_val)[:,1]
y_oof[x_val_index] = y_pred_train
print('ROC AUC {}'.format(roc_auc_score(y_val, y_pred_train)))
 y_pred+= clf.predict_proba(X_test)[:,1] / EPOCHS
cnn_model = run_model(( 28,28, 1))
run_model_compilation(cnn_model, 'adam', 'categorical_crossentropy')
 model_history = run_model_training(cnn_model, mnist_train_array, mnist_train_label_array, 100, 0.2 )<predict_on_test>
sample_submission['isFraud'] = y_pred
sample_submission.to_csv('second_simple_xgboost_Fraud_Baseline.csv' )