Kaggle competition: Titanic - Machine Learning from Disaster
train_on_gpu = torch.cuda.is_available()
if not train_on_gpu:
print('CUDA is not available.Training on CPU...')
 else:
print('CUDA is available! Training on GPU...' )<load_from_csv>
train_df = pd.read_csv(".. /input/titanic/train.csv")
 test_df = pd.read_csv(".. /input/titanic/test.csv" )
names_df = pd.read_csv('.. /input/names.csv',header=None)
 classes = names_df[0]<choose_model_class>
train_df.isna().sum()
model = models.vgg16(pretrained=True)
 print(model )<load_from_csv>
test_df.isna().sum()
valid_size = 0.2
train_transform = transforms.Compose([transforms.Resize(255),
transforms.CenterCrop(224),
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
])
 test_transform = transforms.Compose([transforms.Resize(255),
transforms.CenterCrop(224),
transforms.ToTensor()
])
 train_data = CarDataset(root=".. /input/car_data/car_data/train",transform=train_transform,csv_file=".. /input/names.csv")
 test_data = CarDataset(root=".. /input/car_data/car_data/test",transform=test_transform,csv_file=".. /input/names.csv")
 num_train = len(train_data)
 indices = list(range(num_train))
np.random.shuffle(indices)
 split = int(np.floor(valid_size * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetRandomSampler(valid_idx)
 train_loader = torch.utils.data.DataLoader(train_data, batch_size=20,
sampler=train_sampler)
 valid_loader = torch.utils.data.DataLoader(train_data, batch_size=20,
sampler=valid_sampler)
 test_loader = torch.utils.data.DataLoader(test_data, batch_size=20 )<choose_model_class>
train_set = train_df.drop(columns=["Survived"])
 test_set = test_df.copy(deep = True)
 frame = [train_set,test_set]
final_set = pd.concat(frame,axis=0 )
n_inputs = model.classifier[6].in_features
last_layer = nn.Linear(n_inputs, len(classes))
model.classifier[6] = last_layer
if train_on_gpu:
model.cuda()<choose_model_class>
def fill_nan_categorical(categorical_features,data):
for i in categorical_features:
data[i] = data[i].fillna(data[i].mode() [0])
 return data
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.classifier.parameters() , lr=0.01 )<train_model>
categorical_features = final_set.select_dtypes(exclude=["number","bool_"] ).columns.tolist()
final_set = fill_nan_categorical(categorical_features,final_set )
n_epochs = 10
valid_loss_min = np.Inf
for epoch in range(1, n_epochs+1):
train_loss = 0.0
valid_loss = 0.0
model.train()
for batch_i,(data, index, path,target)in enumerate(train_loader):
if train_on_gpu:
data, target = data.cuda() , target.cuda()
optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
train_loss += loss.item()
if batch_i % 20 == 19:
print('Epoch %d, Batch %d Train Loss: %.16f' % 
(epoch, batch_i + 1, train_loss / 20))
train_loss = 0.0
model.eval()
for batch_i,(data, index, path,target)in enumerate(valid_loader):
if train_on_gpu:
data, target = data.cuda() , target.cuda()
output = model(data)
 loss = criterion(output, target)
 valid_loss += loss.item()
if batch_i % 20 == 19:
print('Epoch %d, Batch %d Validation Loss: %.16f' % 
(epoch, batch_i + 1, valid_loss / 20))
valid_loss = 0.0
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.pt')
 valid_loss_min = valid_loss<load_from_disk>
def fill_nan_continuous(continuous_features,data):
for i in categorical_features:
data[i] = data[i].fillna(data[i].mean() [0])
 return data
model.load_state_dict(torch.load('model.pt'))<find_best_params>
continuous_features = final_set.select_dtypes(include=["number","object_"] ).columns.tolist()
final_set = fill_nan_categorical(continuous_features,final_set )
results = {}
for data,index,path,target in test_loader:
if train_on_gpu:
data, target = data.cuda() , target.cuda()
output = model(data)
 prob = torch.nn.functional.softmax(output, dim=1)
 _, pred = torch.max(prob, 1)
 for k in range(len(pred)) :
name = path[k]
results[name] = pred[k].cpu().tolist()<prepare_output>
def one_hot_encoder(final_set):
df = final_set.copy(deep= True)
 dummies = pd.get_dummies(df,prefix="column_",drop_first=True)
 return dummies
output = pd.DataFrame(results, index=[0] ).transpose()
output = output.reset_index()
output.rename(columns={'index':'Id',0:'Predicted'}, inplace=True)
 output['Predicted'] = output['Predicted']+1
output.head()<count_values>
final_set = final_set.drop(columns = ["Name"])
 ohe_set = one_hot_encoder(final_set )
output['Predicted'].value_counts()<save_to_csv>
train_data = pd.DataFrame(ohe_set[:891])
 test_data = pd.DataFrame(ohe_set[891:])
 X = train_data
y = train_df["Survived"]
def create_download_link(df, title = "Download CSV file", filename = "data.csv"):
csv = df.to_csv(index=False)
 b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html )<load_pretrained>
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.1, random_state = 42 )
create_download_link(output )<save_to_csv>
xgb_classifier = xgboost.XGBRFClassifier(n_estimators=30,
learning_rate=1,
reg_lambda=0.15,
gamma=0.06,
max_depth=20)
 xgb_classifier.fit(X_train, y_train)
 y_test_pred = xgb_classifier.predict(X_test ).tolist()
accuracy_score(y_test_pred,y_test )
output.to_csv('output.csv',index=False )<set_options>
actual = y_test
predicted = y_test_pred
matrix = confusion_matrix(actual,predicted, labels=[1,0])
 print('Confusion matrix :
',matrix)
 tp, fn, fp, tn = confusion_matrix(actual,predicted,labels=[1,0] ).reshape(-1)
 print('Outcome values :
', tp, fn, fp, tn)
 matrix = classification_report(actual,predicted,labels=[1,0])
 print('Classification report :
',matrix )
plt.style.use('seaborn')
 sns.set(font_scale=2.5)
 warnings.filterwarnings('ignore')
 %matplotlib inline<load_from_csv>
y_pred = xgb_classifier.predict(test_data ).tolist()
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )<count_missing_values>
predictions = pd.DataFrame(y_pred)
 datasets = pd.concat([test_set["PassengerId"],predictions],axis=1)
 datasets.columns = ["PassengerId","Survived"]
datasets.to_csv("gender_submission.csv",index=False )