Kaggle competition: Titanic - Machine Learning from Disaster
def evaluation(dataloader,model):
total,correct=0,0
for data in dataloader:
inputs,labels=data
inputs,labels=inputs.to(device),labels.to(device)
 out1,out2=my_model(inputs)
 _,pred1=torch.max(out1.data,1)
 _,pred2=torch.max(out2.data,1)
 _,labels1=torch.max(labels[:,0,:].data,1)
 _,labels2=torch.max(labels[:,1,:].data,1)
 total+=labels.size(0)
 fin1=(pred1==labels1)
 fin2=(pred2==labels2)
 correct+=(fin1==fin2 ).sum().item()
return 100*correct/total 
<choose_model_class>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
loss_fn = nn.CrossEntropyLoss()
plist = [
{'params': my_model.fc1.parameters() , 'lr': 5e-3},
{'params': my_model.fc2.parameters() , 'lr': 5e-3}
]
lr=0.01
opt = optim.SGD(my_model.parameters() ,lr=0.01,momentum=0.9,nesterov=True)
<train_model>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
loss_epoch_arr = []
loss_arr = []
max_epochs = 2
min_loss = 1000
batch_size = 50
n_iters = np.ceil(9000/batch_size)
 for epoch in range(max_epochs):
for i, data in enumerate(train_loader, 0):
my_model.train()
images, labels = data
images = images.to(device)
 targetnp=labels[:,0,:].cpu().numpy()
targetnp1 = labels[:,1,:].cpu().numpy()
new_targets1 = np.argmax(targetnp,axis=1)
 new_targets2 = np.argmax(targetnp1,axis=1)
 new_targets1=torch.LongTensor(new_targets1)
 new_targets2=torch.LongTensor(new_targets2)
 new_targets1 = new_targets1.to(device)
 new_targets2 = new_targets2.to(device)
 opt.zero_grad()
out = my_model.forward(images)
 loss_fc1 = loss_fn(out[0], new_targets1)
 loss_fc2 = loss_fn(out[1],new_targets2)
 loss = torch.add(loss_fc1,loss_fc2)
 loss.backward()
opt.step()
if min_loss > loss.item() :
min_loss = loss.item()
best_model = copy.deepcopy(my_model.state_dict())
 print('Min loss %0.2f' % min_loss)
 if i % 100 == 0:
print('Iteration: %d/%d, Loss: %0.2f' %(i, n_iters, loss.item()))
del images, labels, out
torch.cuda.empty_cache()
loss_arr.append(loss.item())
 print("Epoch number :",epoch)
 print("Train Accuracy :",evaluation(train_loader,my_model))
print("Test Accuracy :" ,evaluation(validation_loader,my_model))
print('Learning Rate :',lr)
 loss_epoch_arr.append(loss.item())
 plt.plot(loss_arr)
 plt.show()<load_from_disk>
train_data['FamilySize'] = train_data['SibSp'] + train_data['Parch'] + 1
test_data['FamilySize'] = test_data['SibSp'] + test_data['Parch'] + 1
my_model.load_state_dict(best_model )<compute_test_metric>
train_data['Fare'].fillna(train_data['Fare'].median() , inplace=True)
 test_data['Fare'].fillna(test_data['Fare'].median() , inplace=True)
evaluation(validation_loader,my_model )<find_best_params>
women = train_data.loc[train_data['Sex']=='female']['Survived']
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women )
my_model.eval()
plist=[]
fn_list=[]
for inputs_test, fn in test_loader:
inputs_test=inputs_test.to(device)
 out1,out2=my_model.forward(inputs_test)
 _,pred1=torch.max(out1,1)
 pred1=pred1.tolist()
_,pred2=torch.max(out2,1)
 pred2=pred2.tolist()
for x,y,z in zip(pred1,pred2,fn):
p="V"+str(x)+"_"+"C"+str(y)
 plist.append(p)
 fn_list.append(z )<save_to_csv>
men = train_data.loc[train_data['Sex']=='male']['Survived']
rate_men = sum(men)/len(men)
 print("% of men who survived:", rate_men )
submission = pd.DataFrame({"ImageId":fn_list, "Class":plist})
 submission.to_csv('submission.csv', index=False )<load_from_csv>
train_data.Sex = pd.get_dummies(train_data.Sex)
 test_data.Sex = pd.get_dummies(test_data.Sex )
df = pd.read_csv('.. /input/train.csv' )<prepare_x_and_y>
train_data['Age'].fillna(train_data['Age'].median() , inplace=True)
 test_data['Age'].fillna(test_data['Age'].median() , inplace=True)
y = df['Volume']
X = df.drop(['Volume','Date'],axis=1 )<import_modules>
train_data['IsAlone'] = 0
train_data.loc[train_data['FamilySize'] == 1, 'IsAlone'] = 1
test_data['IsAlone'] = 0
test_data.loc[test_data['FamilySize'] == 1, 'IsAlone'] = 1
train_data['Age*Class'] = train_data.Age * train_data.Pclass
test_data['Age*Class'] = test_data.Age * test_data.Pclass
from sklearn.linear_model import LinearRegression<choose_model_class>
old = train_data.loc[train_data['Age'] > 45]['Survived']
rate_old = sum(old)/len(old)
 print("% of old people who survived:", rate_old )
reg = LinearRegression()<train_model>
mid_age = train_data.loc[(train_data['Age'] <= 45)&(train_data['Age'] >= 15)]['Survived']
rate_mid = sum(mid_age)/len(mid_age)
 print("% of middle aged people who survived:", rate_mid )
reg.fit(X,y )<load_from_csv>
young = train_data.loc[train_data['Age'] < 15]['Survived']
rate_young = sum(young)/len(young)
 print("% of young people who survived:", rate_young )
test = pd.read_csv('.. /input/test.csv' )<drop_column>
train_data['Age'] = pd.cut(train_data['Age'], bins=[0., 10., 25., 50, 80, np.inf], labels=[0,1,2,3,4] ).astype(int)
 test_data['Age'] = pd.cut(test_data['Age'], bins=[0., 10., 25., 50, 80, np.inf], labels=[0,1,2,3,4] ).astype(int)
testdf = test.drop(['Date'],axis=1 )<predict_on_test>
print(train_data.isnull().sum())
 train_data[train_data.Embarked.isnull() ]
prediction = reg.predict(testdf )<save_to_csv>
pd.options.mode.chained_assignment = None
train_data.Embarked = train_data.Embarked.fillna('None')
 test_data.Embarked = test_data.Embarked.fillna('None')
 label_encoder = LabelEncoder()
train_data.Embarked = label_encoder.fit_transform(train_data.Embarked)
 test_data.Embarked = label_encoder.transform(test_data.Embarked)
serial = test['Date']
data = {'Date': serial, 'Volume': prediction}
submission = pd.DataFrame(data)
 submission.to_csv('Submission.csv', index=False )<load_from_csv>
y = train_data["Survived"]
features = ["Pclass", "Sex", "Fare", "SibSp", "Parch","FamilySize", "Embarked", "IsAlone"]
X = train_data[features]
X_test = test_data[features]
print(X)
 scoring_method = "f1"
sample = pd.read_csv('/kaggle/input/mlbio1/sample_submission.csv')
 test = pd.read_csv('/kaggle/input/mlbio1/test.csv')
 train = pd.read_csv('/kaggle/input/mlbio1/train.csv')
<import_modules>
rf_model = RandomForestClassifier()
rf_params ={
'bootstrap': [True, False],
'max_depth': [10, None],
'max_features': ['auto', 'sqrt'],
'min_samples_leaf': [1, 2, 4],
'min_samples_split': [2, 5, 10],
'n_estimators': [100]}
rf_gs = GridSearchCV(rf_model, rf_params, scoring=scoring_method, cv=8, n_jobs=4)
 rf_gs.fit(X, y)
 print(rf_gs.best_params_)
 print(rf_gs.best_score_ )
%matplotlib inline
mpl.style.use('ggplot')
 sns.set_style('white')
 flatui = ["
pl = sns.color_palette(flatui )<sort_values>
cross_val_score(rf_gs, X, y, cv=5 )
<drop_column><EOS> 
predictions = random_forest.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your pipeline submission was successfully saved!" )