Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<compute_train_metric>
import os
from datetime import datetime
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
import torchvision
import torchvision.transforms as transforms
from sklearn.model_selection import train_test_split
DT = DecisionTreeClassifier(random_state = 5)
 estimator.append(( 'DT',DecisionTreeClassifier(random_state = 5)))
 cv = cross_val_score(DT,x_train,x_test,cv=10)
 accuracy4 = cv.mean()
accuracy.append(accuracy4)
 print(cv)
 print(cv.mean() )<train_model>
print(os.listdir(".. /input"))
test_path = '.. /input/test/test/'
train_path = '.. /input/train/train/'
train_df = pd.read_csv('.. /input/train.csv' )
DT.fit(x_train,x_test)
 DT.score(y_train,y_test )<save_to_csv>
train_df['has_cactus'].value_counts()
model4pred = DT.predict(y_train)
 submission4 = pd.DataFrame(columns = ['PassengerId','Survived'])
 submission4['PassengerId'] = result['PassengerId']
submission4['Survived'] = model4pred
submission4.to_csv('Decision Tree(No HT ).csv',index = False )<compute_train_metric>
class CactusDataset(Dataset):
def __init__(self, labels, data_dir, transform=None):
super().__init__()
self.labels = labels.values
self.data_dir = data_dir
self.transform = transform
def __len__(self):
return len(self.labels)
 def __getitem__(self, index):
name, label = self.labels[index]
img_path = os.path.join(self.data_dir, name)
 img = mpimg.imread(img_path)
 if self.transform is not None:
img = self.transform(img)
 return img, label
GNB = GaussianNB()
estimator.append(( 'GNB',GaussianNB()))
cv = cross_val_score(GNB,x_train,x_test,cv=10)
 accuracy5 = cv.mean()
accuracy.append(accuracy5)
 print(cv)
 print(cv.mean() )<train_model>
train, valid = train_test_split(train_df, stratify=train_df['has_cactus'], test_size=0.2)
 train_tf = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
])
 valid_tf = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
])
 train_data = CactusDataset(
 labels=train,
data_dir=train_path,
transform=train_tf, 
)
 valid_data = CactusDataset(
 labels=valid,
data_dir=train_path,
transform=valid_tf,
)
GNB.fit(x_train,x_test)
 GNB.score(y_train,y_test )<save_to_csv>
class Net(nn.Module):
def __init__(self):
super(Net, self ).__init__()
self.pool = nn.MaxPool2d(2, 2)
 self.conv1 = nn.Conv2d(
 in_channels=3,
out_channels=16,
kernel_size=3,
stride=1,
padding=1 
)
 self.conv2 = nn.Conv2d(
 in_channels=16,
out_channels=32,
kernel_size=3,
stride=1,
padding=1 
)
 self.conv3 = nn.Conv2d(
 in_channels=32,
out_channels=64,
kernel_size=3,
stride=1,
padding=1 
)
 self.fc1 = nn.Linear(64 * 8 * 8, 512)
 self.output = nn.Linear(512, 2)
 def forward(self, x):
x = F.relu(self.conv1(x))
x = self.pool(F.relu(self.conv2(x)))
 x = self.pool(F.relu(self.conv3(x)))
 x = x.view(-1, 64 * 8 * 8)
 x = F.relu(self.fc1(x))
x = self.output(x)
 return x
model5pred = GNB.predict(y_train)
 submission5 = pd.DataFrame(columns = ['PassengerId','Survived'])
 submission5['PassengerId'] = result['PassengerId']
submission5['Survived'] = model5pred
submission5.to_csv('Gaussian NB(No HT ).csv',index = False )<compute_train_metric>
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
 n_epochs = 20
n_classes = 2
batch_size = 50
learning_rate = 0.001
MNB = MultinomialNB()
estimator.append(( 'MNB',MultinomialNB()))
cv = cross_val_score(MNB,x_train,x_test,cv=10)
 accuracy6 = cv.mean()
accuracy.append(accuracy6)
 print(cv)
 print(cv.mean() )<train_model>
model = Net().to(device)
 criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters() , lr=learning_rate )
MNB.fit(x_train,x_test)
 MNB.score(y_train,y_test )<save_to_csv>
train_loader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=True)
 valid_loader = DataLoader(dataset=valid_data, batch_size=batch_size, shuffle=False )
MNB.fit(x_train,x_test)
 model6pred = MNB.predict(y_train)
 submission6 = pd.DataFrame(columns = ['PassengerId','Survived'])
 submission6['PassengerId'] = result['PassengerId']
submission6['Survived'] = model6pred
submission6.to_csv('MultinomialNB(No HT ).csv',index = False )<compute_train_metric>
steps = len(train_loader)
 results = []
for epoch in range(n_epochs):
t_start = datetime.now()
model.train()
epoch_loss = 0
for i,(images, labels)in enumerate(train_loader):
images = images.to(device)
 labels = labels.to(device)
 outputs = model(images)
 loss = criterion(outputs, labels)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_loss = epoch_loss / len(train_loader)
 t_elapsed = datetime.now() - t_start
model.eval()
correct = 0
total = 0
with torch.no_grad() :
for images, labels in valid_loader:
images = images.to(device)
 labels = labels.to(device)
 outputs = model(images)
 _, predicted = torch.max(outputs.data, 1)
 total += labels.size(0)
 correct +=(predicted == labels ).sum().item()
epoch_acc = correct / total
results.append({'Epoch': epoch+1, 'Loss': epoch_loss, 'Accuracy': epoch_acc})
 print('Epoch [{:0>2d}/{}], Loss: {:.4f}, Accuracy: {:.4f}, Epoch Time: {}'.format(
 epoch+1, n_epochs, epoch_loss, epoch_acc, t_elapsed, 
))
RF = RandomForestClassifier(random_state = 5)
 estimator.append(( 'RF',RandomForestClassifier(random_state = 5)))
 cv = cross_val_score(RF,x_train,x_test,cv=10)
 accuracy7 = cv.mean()
accuracy.append(accuracy7)
 print(cv)
 print(cv.mean() )<train_model>
result_df = pd.read_csv('.. /input/sample_submission.csv')
 test_data = CactusDataset(
 labels=result_df,
data_dir=test_path,
transform=valid_tf 
)
 test_loader = DataLoader(
 dataset=test_data,
batch_size=batch_size,
shuffle=False
)
RF.fit(x_train,x_test)
 RF.score(y_train,y_test )<save_to_csv>
model.eval()
predictions = []
for batch_i,(data, target)in enumerate(test_loader):
data = data.to(device)
 target = target.to(device)
 output = model(data)
 pr = output[:,1].detach().cpu().numpy()
for i in pr:
predictions.append(i)
 result_df['has_cactus'] = predictions
result_df.to_csv('submission.csv', index=False )