Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<define_variables>
import numpy as np
import pandas as pd
import torch
import torch.functional as F
from torch.optim import Adam
from torchvision import models, datasets,transforms
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from PIL import Image
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
submission = {'Id': ids, 'SalePrice': SalePrice_pred}<create_dataframe>
train_data=pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 sample = pd.read_csv(".. /input/aerial-cactus-identification/sample_submission.csv")
 train_data.head(5 )
df_submission = pd.DataFrame(submission)
 df_submission<save_to_csv>
train_data["has_cactus"].value_counts().plot(kind="pie")
 train_dir = "train/train/"
val_dir = "train/train"
train_data.head(5)
 train_data.values[:,1]
df_submission.to_csv('submission.csv', index=False )<set_options>
class dataset_(torch.utils.data.Dataset):
def __init__(self,labels,data_directory,transform):
super().__init__()
self.list_id=labels.values[:,0]
self.labels=labels.values[:,1]
self.data_dir=data_directory
self.transform=transform
def __len__(self):
return len(self.list_id)
 def __getitem__(self,index):
name=self.list_id[index]
img=Image.open('.. /input/aerial-cactus-identification/{}/{}'.format(self.data_dir,name))
img=self.transform(img)
 return img,torch.tensor(self.labels[index],dtype=torch.float32 )
warnings.filterwarnings(action='ignore')
 warnings.filterwarnings(action='ignore', category=DataConversionWarning)
 warnings.filterwarnings(action='ignore', category=ConvergenceWarning)
<set_options>
image_transform={"train":transforms.Compose([transforms.RandomRotation(degrees=0),transforms.RandomResizedCrop(224),transforms.RandomHorizontalFlip(p=0.5),transforms.ToTensor() ,transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])]),
"test": transforms.Compose([transforms.Resize(224),transforms.ToTensor() ,transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])])}
%matplotlib inline
sns.set()<load_from_csv>
test_dir = "test/test/"
test_df = dataset_(sample, test_dir, image_transform["test"])
 test_data_loader = torch.utils.data.DataLoader(test_df, batch_size=64,shuffle=False )
TRAIN_PATH = '.. /input/house-prices-advanced-regression-techniques'
TEST_PATH = '.. /input/house-prices-advanced-regression-techniques'
def load_houses_data(TRAIN_PATH=TRAIN_PATH, TEST_PATH=TEST_PATH):
train_csv = os.path.join(TRAIN_PATH, 'train.csv')
 test_csv = os.path.join(TEST_PATH, 'test.csv')
 return pd.read_csv(train_csv), pd.read_csv(test_csv )<prepare_x_and_y>
train_set, validation_set = train_test_split(train_data,stratify=train_data.has_cactus.values, test_size=0.2 )
X_train, X_test = load_houses_data()
y_train = X_train['SalePrice']
y_train_first = y_train<groupby>
train_data_set = dataset_(train_data, train_dir,image_transform["train"])
 validation_data_set = dataset_(train_data,val_dir, image_transform["test"] )
X_train.groupby('BedroomAbvGr' ).count() ['Id']<groupby>
train_data_frame = torch.utils.data.DataLoader(dataset=train_data_set, batch_size=32, shuffle=True)
 valid_data_frame = torch.utils.data.DataLoader(dataset=validation_data_set, batch_size=32 )
X_train.groupby('BsmtFullBath' ).count() ['Id']<groupby>
vgg16 = models.vgg16(pretrained=True)
 for param in vgg16.parameters() :
param.requires_grad = False
X_train.groupby('GarageFinish' ).count() ['Id']<sort_values>
classifier = torch.nn.Sequential(OrderedDict([ 
('fc1',torch.nn.Linear(512*7*7,1024)) , 
('relu',torch.nn.ReLU()), 
('fc2',torch.nn.Linear(1024,512)) , 
('relu', torch.nn.ReLU()), 
('fc3',torch.nn.Linear(512,2)) , 
('output',torch.nn.LogSoftmax(dim=1))
]))
vgg16.classifier = classifier
X_train.corr() ['SalePrice'].sort_values()<data_type_conversions>
optimizer = Adam(vgg16.classifier.parameters() , lr =0.001)
 criterion = torch.nn.NLLLoss()
X_train['YrSold'] = X_train['YrSold'].astype(str)
 X_train['MoSold'] = X_train['MoSold'].astype(str )<train_model>
device =("cuda:0" if torch.cuda.is_available() else "cpu" )
class Normalize(BaseEstimator, TransformerMixin):
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
dataset = y_train.copy()
dataset = np.log1p(dataset)
 return dataset
y_train = Normalize().transform(y_train)
 sns.distplot(y_train)
<split>
total_epochs = 10
vgg16.to(device)
 train_error_array = []
test_error_array = []
for epoch in range(total_epochs):
running_train_loss = 0.0
running_accuracy = 0.0
vgg16.train()
for step,(data, train_label)in enumerate(train_data_frame):
data, train_label = data.to(device), train_label.to(device)
 output_pred = vgg16.forward(data)
 train_ps = torch.exp(output_pred)
 train_prob, train_cls = train_ps.topk(1, dim=1)
 train_equals =(train_cls == train_label.long().view(*train_cls.shape))
running_accuracy += torch.mean(train_equals.type(torch.FloatTensor))
optimizer.zero_grad()
train_loss = criterion(output_pred, train_label.long())
 running_train_loss += train_loss.item()
train_loss.backward()
optimizer.step()
else:
test_accuracy= 0.0
testloss = 0.0
vgg16.eval()
with torch.no_grad() :
for valid_data, valid_label in valid_data_frame:
valid_data, valid_label = valid_data.to(device), valid_label.to(device)
 test_pred = vgg16.forward(valid_data)
 test_loss = criterion(test_pred, valid_label.long())
 testloss += test_loss.item()
out = torch.exp(test_pred)
 prob, cls = out.topk(1, dim=1)
 equals =(cls==valid_label.long().view(*cls.shape))
test_accuracy += torch.mean(equals.type(torch.FloatTensor))
train_error_array.append(running_train_loss/len(train_data_frame))
test_error_array.append(testloss/len(valid_data_frame))
print(f"EPoch: {epoch+1} >> ")
 print(f"training loss >> {running_train_loss/len(train_data_frame)}")
 print(f"test loss >> {testloss/len(valid_data_frame)}")
 print(f"test accuracy >> {test_accuracy/len(valid_data_frame)}")
 print("
")
X_train_label = X_train[label_attrs]
X_train_num = X_train[num_attrs]<create_dataframe>
output = []
with torch.no_grad() :
vgg16.eval()
for test_data, _ in test_data_loader:
test_data = test_data.to(device)
 test_output = vgg16.forward(test_data)
 test_prob = torch.exp(test_output)
 output += list(test_prob[:,1].cpu().data.numpy())
X_train_num_std = pd.DataFrame(StandardScaler().fit_transform(X_train_num), columns=X_train_num.columns )<sort_values>
sample_output = {"id": sample["id"], "has_cactus":output}
sample_output_dataframe = pd.DataFrame(sample_output)
 sample_output_dataframe.head(10 )
<define_variables><EOS> 
sample_output_dataframe.to_csv("submission.csv",index=False )