Kaggle competition: IEEE-CIS Fraud Detection
y_sub = np.argmax(y_pred, 1 )<save_to_csv>
sub1 = pd.read_csv('.. /input/ieee-blend/lgb_sub.csv')
 sub2 = pd.read_csv('.. /input/ieee-blend/submission_IEEE(1 ).csv')
 sub3 = pd.read_csv('.. /input/ieee-blend/submission_IEEE.csv')
 sub4 = pd.read_csv('.. /input/ieee-blend/submission.csv')
 temp=pd.read_csv('.. /input/ieee-blend/lgb_sub.csv' )
submission = pd.DataFrame({'ImageId': np.arange(1, 28001), 'Label': y_sub})
 submission.to_csv("submission.csv", index = False)
 print("Your submission was successfully saved!" )<save_model>
temp['isFraud'] = 0.60*sub4['isFraud'] + 0.40*sub3['isFraud']
temp.to_csv('submission_p2_1.csv', index=False )
model.save("my_model" )<load_from_csv>
sub_path = ".. /input/ieee-blend"
all_files = os.listdir(sub_path)
 all_files
transform = transforms.Normalize(0, 255, inplace=False)
 train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 submit_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 features_train = transform(torch.Tensor(np.reshape(train_df.iloc[:,1:].to_numpy() ,(-1,1,28,28))))
labels_train = torch.LongTensor(train_df['label'].to_numpy())
 features_submit = transform(torch.Tensor(np.reshape(submit_df.to_numpy() ,(-1,28,28))))
train_len = int(features_train.shape[0]*0.6)
 valid_len = int(features_train.shape[0]*0.2)
 train_set = torch.utils.data.TensorDataset(features_train[:train_len],labels_train[:train_len])
 valid_set = torch.utils.data.TensorDataset(features_train[train_len:(train_len+valid_len)],labels_train[train_len:(train_len+valid_len)])
 test_set = torch.utils.data.TensorDataset(features_train[(train_len+valid_len):],labels_train[(train_len+valid_len):])
 train_loader = DataLoader(train_set, batch_size = 50)
 valid_loader = DataLoader(valid_set, batch_size = 50)
 test_loader = DataLoader(test_set, batch_size = 50 )<choose_model_class>
outs = [pd.read_csv(os.path.join(sub_path, f), index_col=0)for f in all_files]
concat_sub = pd.concat(outs, axis=1)
 cols = list(map(lambda x: "ieee" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols
concat_sub.reset_index(inplace=True)
 concat_sub.head()
ncol = concat_sub.shape[1]
class CNN_classifier(nn.Module):
def __init__(self):
super(CNN_classifier, self ).__init__()
self.conv1 = nn.Conv2d(1, 32, 5, padding=2)
 self.conv2 = nn.Conv2d(32, 32, 5, padding=2)
 self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
 self.pool = nn.MaxPool2d(2, 2)
 self.fc1 = nn.Linear(64*3*3, 256)
 self.fc2 = nn.Linear(256, 10)
 self.dropout = nn.Dropout(0.25)
 def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
 x = self.dropout(x)
 x = self.pool(F.relu(self.conv2(x)))
 x = self.dropout(x)
 x = self.pool(F.relu(self.conv3(x)))
 x = self.dropout(x)
 x = x.view(-1, 64*3*3)
 x = F.relu(self.fc1(x))
x = self.dropout(x)
 x = self.fc2(x)
 return x
model = CNN_classifier()
print(model )<choose_model_class>
concat_sub['ieee_max'] = concat_sub.iloc[:, 1:ncol].max(axis=1)
 concat_sub['ieee_min'] = concat_sub.iloc[:, 1:ncol].min(axis=1)
 concat_sub['ieee_mean'] = concat_sub.iloc[:, 1:ncol].mean(axis=1)
 concat_sub['ieee_median'] = concat_sub.iloc[:, 1:ncol].median(axis=1 )
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters() , lr=0.01)
 if torch.cuda.is_available() :
model = model.cuda()
criterion = criterion.cuda()<train_model>
cutoff_lo = 0.7
cutoff_hi = 0.3
n_epochs = 10
valid_plot = []
train_plot = []
valid_loss_min = np.Inf
for epoch in range(1, n_epochs+1):
train_loss = 0.0
valid_loss = 0.0
model.train()
for data, target in train_loader:
if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
train_loss += loss.item() *data.size(0)
 model.eval()
for data, target in valid_loader:
if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 loss = criterion(output, target)
 valid_loss += loss.item() *data.size(0)
 train_loss = train_loss/len(train_loader.sampler)
 valid_loss = valid_loss/len(valid_loader.sampler)
 train_plot.append(train_loss)
 valid_plot.append(valid_loss)
 print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
 epoch, train_loss, valid_loss))
if valid_loss <= valid_loss_min:
print('Validation loss decreased({:.6f} --> {:.6f} ).Saving model...'.format(
 valid_loss_min,
valid_loss))
torch.save(model.state_dict() , 'model_mnist_cnn.pt')
 valid_loss_min = valid_loss<find_best_params>
concat_sub['isFraud'] = concat_sub['ieee_mean']
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_mean.csv',
index=False, float_format='%.6f' )
test_loss = 0.0
correct_pred = 0
false_images = torch.empty(( 1, 28, 28))
false_preds = []
false_targets = []
model.eval()
for data, target in test_loader:
if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 loss = criterion(output, target)
 test_loss += loss.item() *data.size(0)
 _, pred = torch.max(output, 1)
 correct_pred += torch.sum(pred == target ).item()
true_pred = pred==target
for i, value in enumerate(true_pred):
if value.item() == False:
false_images = torch.cat(( false_images, data[i]), dim=0)
 false_preds.append(pred[i])
 false_targets.append(target[i])
 false_images = false_images[1:]
test_loss = test_loss/len(test_loader.dataset)
 accuracy = correct_pred /len(test_loader.dataset)
 print('Test Loss: {:.6f}
'.format(test_loss))
print('Accuracy: {:.2f}%'.format(accuracy*100)) 
<find_best_params>
concat_sub['isFraud'] = concat_sub['ieee_median']
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_median.csv',
index=False, float_format='%.6f')
data = features_submit.view(-1,1,28,28)
 if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 score, predictions = torch.topk(output, 1 )<save_to_csv>
concat_sub['isFraud'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1), 1,
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
0, concat_sub['ieee_median']))
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_pushout_median.csv',
index=False, float_format='%.6f' )
ImageId = np.arange(1,predictions.size() [0]+1)
 Label = predictions.view(predictions.size() [0] ).numpy()
new_submission = pd.DataFrame({'ImageId': ImageId, 'Label': Label})
 new_submission.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<import_modules>
concat_sub['isFraud'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['ieee_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['ieee_min'],
concat_sub['ieee_mean']))
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_minmax_mean.csv',
index=False, float_format='%.6f' )
import numpy as np
import pandas as pd<load_from_csv>
concat_sub['isFraud'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['ieee_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['ieee_min'],
concat_sub['ieee_median']))
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_minmax_median.csv',
index=False, float_format='%.6f' )
training_data = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test_images = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train_images = training_data.drop("label", axis=1)
 train_labels = training_data["label"]<data_type_conversions>
sub_base = pd.read_csv('.. /input/ieee-blend/lgb_sub.csv' )
<feature_engineering><EOS> 
concat_sub['ieee_base'] = sub_base['isFraud']
concat_sub['isFraud'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['ieee_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['ieee_min'],
concat_sub['ieee_base']))
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_minmax_bestbase.csv',
index=False, float_format='%.6f' )