Kaggle competition: Titanic - Machine Learning from Disaster
def one_epoch(fold):
log_string('fold:%d, batch_size:%d,lr:%f,fold_epech:%d,device:%s' %(fold , BATCH_SIZE,lr,2,device))
folds= StratifiedKFold(n_splits=fold ).split(df['image_id'],df['label'])
 acc_train_all= []
acc_valid_all= []
best_acc = 0
for i,(train_index,valid_index)in enumerate(folds):
train_df = df.loc[train_index,:].reset_index(drop=True)
 valid_df = df.loc[valid_index,:].reset_index(drop=True)
 train_dataset = CassavaDataset(train_df,BASE_DIR,transforms=transforms.Compose([
transforms.RandomResizedCrop(IMG_SIZE),
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomVerticalFlip(p=0.5),
transforms.RandomRotation(45),
transforms.ColorJitter(brightness=0.1,contrast=0.1),
transforms.ToTensor() ,
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
]))
valid_dataset = CassavaDataset(valid_df,BASE_DIR,transforms=transforms.Compose([
transforms.CenterCrop(IMG_SIZE),
transforms.ToTensor() ,
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
]))
trainLoader = torch.utils.data.DataLoader(dataset=train_dataset,batch_size=BATCH_SIZE,shuffle=True)
 validLoader = torch.utils.data.DataLoader(dataset=valid_dataset,batch_size=BATCH_SIZE)
 acc= train(net, i, trainLoader, optimizer, criterion)
 acc_train_all.append(acc)
 acc= valid(net ,i, validLoader)
 acc_valid_all.append(acc)
 if acc > best_acc:
best_acc = acc
torch.save(net, './models/efficientnetb4_fold5_2.pth')
 scheduler.step()
plt.plot(acc_train_all)
 plt.plot(acc_valid_all)
 plt.ylabel('Accuracy')
 plt.xlabel('Epoch')
 plt.legend(['Train', 'Valid'], loc='upper left')
 plt.show()<choose_model_class>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
<load_from_csv>
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived:", rate_men)
 print("% of women who survived:", rate_women )
def test_one(model_name , file_name):
net = CassvaImgClassifier(model_name,5, False)
 net.to(device)
 net.load_state_dict(torch.load(filename ).state_dict())
 net.eval()
preds = []
submit = pd.read_csv(os.path.join(BASE_DIR, "sample_submission.csv"))
for image_id in submit.image_id:
img = Image.open(BASE_DIR+"/test_images/"+image_id ).convert("RGB")
 img = transform1(img ).unsqueeze(0 ).to(device)
 pred = net(img)
 pred = pred.cpu().detach().numpy().astype('int')
 preds.extend(pred)
 return preds<feature_engineering>
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output_flat = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output_flat.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )
file_name = ['a','b','c']
model_name = ['a','b','c']
transform1 = transforms.Compose([
transforms.CenterCrop(IMG_SIZE),
transforms.ToTensor() ,
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
 all_preds = []
for i in range(3):
all_preds.append(test_one(model_name[i],file_name[i]))
submit['label'] = np.argmax(np.mean(all_preds, axis=0), axis=1)
 submit.to_csv('submission.csv', index = False )<define_variables>
y = train_data["Survived"]
bins= [0,3,13,18,45,120]
labels = ['Infant','Child','Teen','Adolt','Senior']
train_data['AgeGroup'] = pd.cut(train_data['Age'], bins=bins, labels=labels, right=False)
 test_data['AgeGroup'] = pd.cut(train_data['Age'], bins=bins, labels=labels, right=False)
 features = ["Pclass", "Sex", "SibSp", "Parch", "AgeGroup"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output_age = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output_age.to_csv('my_submission_age.csv', index=False)
 print("Your submission was successfully saved!")
 print("Based on age groups as well:", sum(output_age["Survived"]))
print("Based on four features only:", sum(output_flat["Survived"]))
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master' 
<import_modules>
processed_train_data = train_data.dropna(subset=['Cabin'] )