Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<import_modules>
import numpy as np
import pandas as pd
import zipfile
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import log_loss, roc_auc_score
from sklearn.linear_model import LogisticRegression
import os
init_notebook_mode()<define_variables>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
features1 = ["Age","SibSp","Parch", "Pclass", "is_male", "is_female", "f_size", "Single", "SmallF", "MedF", "LargeF", "Title", "Embarked"]
target = ["Survived"]
print("Total number of features is {}".format(len(features1)) )<load_from_csv>
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224
test_csv = prepare_data('.. /input/test.csv', True)
 test_data = test_csv[features1]
test_csv.head(5 )<split>
train_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_image_list.npy')
 test_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_image_list.npy' )
train, valid = train_test_split(data, test_size=0.2)
 print("The Train Set Size is {}
The Validation Set Size is {}".format(len(train), len(valid)))
 print("Test Set Size is {}".format(len(test_data)) )<save_to_csv>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
train_x , train_y = data[features1].as_matrix() , data[target].as_matrix()
clf = DecisionTreeClassifier()
clf.fit(train_x, train_y)
 print(( np.array(clf.predict(valid[features1].as_matrix())== valid[target].as_matrix().flatten() , dtype=np.int ).sum() * 100.) / len(valid))
result = pd.DataFrame(data={'PassengerId': test_csv['PassengerId'], 'Survived': clf.predict(test_data.as_matrix())})
 result.to_csv(path_or_buf='decision_tree_submittion.csv', index = False, header = True )<prepare_x_and_y>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
class TitanicLoader(Dataset):
def __init__(self,train,transforms=None):
self.X = train.as_matrix(columns=features1)
 self.Y = train.as_matrix(columns=target ).flatten()
self.count = len(self.X)
 self.transforms = transforms
def __getitem__(self, index):
nextItem = Variable(torch.tensor(self.X[index] ).type(torch.FloatTensor))
if self.transforms is not None:
nextItem = self.transforms(nextItem[0])
 return(nextItem, self.Y[index])
 def __len__(self):
return self.count<choose_model_class>
train_EB7 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/train_EB7.npy')
 test_EB7 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_3/cats_and_dogs_3/test_EB7.npy' )
class DNN(nn.Module):
def __init__(self, input_size, first_hidden_size, second_hidden_size, num_classes):
super(DNN, self ).__init__()
self.z1 = nn.Linear(input_size, first_hidden_size)
 self.relu = nn.ReLU()
self.z2 = nn.Linear(first_hidden_size, second_hidden_size)
 self.z3 = nn.Linear(second_hidden_size, num_classes)
 self.bn1 = nn.BatchNorm1d(first_hidden_size)
 self.bn2 = nn.BatchNorm1d(second_hidden_size)
 self.dropout = nn.Dropout(p=0.5)
 self.dropout2 = nn.Dropout(p=0.1)
 self.log_softmax = nn.LogSoftmax(dim=1)
 def forward(self, x):
out = self.z1(x)
 out = self.relu(out)
 out = self.dropout(out)
 out = self.bn1(out)
 out = self.z2(out)
 out = self.relu(out)
 out = self.dropout2(out)
 out = self.bn2(out)
 out = self.z3(out)
 out = self.log_softmax(out)
 return out
def name(self):
return "DNN"<train_on_grid>
X_train, X_val, y_train, y_val = train_test_split(train_EB7, target, test_size=0.1, random_state=42 )
def train_dnn(net, trainL, validL):
count = 0
accuList = []
lossList = []
optimizer = torch.optim.Adam(net.parameters() ,lr=0.001)
 for epc in range(1,epochs + 1):
print("Epoch
vcount = 0
total_loss = 0
net.train()
for data,target in trainL:
optimizer.zero_grad()
out = net(data)
 loss = F.nll_loss(out, target, size_average=False)
 pred = out.data.max(1, keepdim=True)[1]
count += pred.eq(target.data.view_as(pred)).sum()
loss.backward()
optimizer.step()
net.eval()
for data, target in validL:
out = net(data)
 loss = F.nll_loss(out, target, size_average=False)
 total_loss += loss.item()
pred = out.data.max(1, keepdim=True)[1]
vcount += pred.eq(target.data.view_as(pred)).sum().item()
accuList.append(100.*(vcount / len(validL)))
 lossList.append(total_loss / len(validL))
return accuList, lossList
def test(net, loader):
net.eval()
vcount = 0
count = 0
total_loss = 0.0
for data, target in loader:
out = net(data)
 loss = F.nll_loss(out, target, size_average=False)
 total_loss += loss.item()
pred = out.data.max(1, keepdim=True)[1]
vcount += pred.eq(target.data.view_as(pred)).sum().item()
return 100.*(vcount / len(loader)) , total_loss / len(loader )<define_variables>
%%time
lr = LogisticRegression(C=0.01, max_iter=10000)
 lr.fit(X_train, y_train )
epochs = 12<load_pretrained>
val_preds = lr.predict_proba(X_val)[:,1]
titanic_train_DS = TitanicLoader(train)
 titanic_valid_DS = TitanicLoader(valid)
 train_loader = torch.utils.data.DataLoader(titanic_train_DS,
batch_size=6, shuffle=False)
 valid_loader = torch.utils.data.DataLoader(titanic_valid_DS,
batch_size=1, shuffle=False )<train_model>
roc_auc_score(y_val, val_preds )
myNet = DNN(len(features1), 23, 4, 2)
 accuList, lossList = train_dnn(myNet, train_loader, valid_loader )<predict_on_test>
log_loss(y_val, val_preds )
def get_preds(test, net):
net.eval()
preds = []
for data, target in test:
out = net(data)
 pred = out.data.max(1, keepdim=True)[1]
preds.append(pred.item())
 return preds<save_to_csv>
test_preds = lr.predict_proba(test_EB7)[:,1]
<save_to_csv><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()