Kaggle competition: Aerial Cactus Identification
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train_ID = train['Id']
test_ID = test['Id']
train.drop("Id", axis = 1, inplace = True)
 test.drop("Id", axis = 1, inplace = True)
 train = train[train.GrLivArea < 4500]
train.reset_index(drop=True, inplace=True)
 outliers = [30, 88, 462, 631, 1322]
train = train.drop(train.index[outliers])
 print(train.columns)
 print(test.columns)
 print(train.shape,test.shape)
<sort_values>
train_df = pd.read_csv(".. /input/train.csv")
 train_df.head()
data_features_na = data_features.isnull().sum()
data_features_na = data_features_na[data_features_na>0]
data_features_na.sort_values(ascending=False )<feature_engineering>
print(f"Train Size: {len(os.listdir('.. /input/train/train')) }")
 print(f"Test Size: {len(os.listdir('.. /input/test/test')) }" )
str_vars = ['MSSubClass','YrSold','MoSold']
for var in str_vars:
data_features[var] = data_features[var].apply(str)
<categorify>
train_path = '.. /input/train/train/'
test_path = '.. /input/test/test/'
common_vars = ['Exterior1st','Exterior2nd','SaleType','Electrical','KitchenQual']
for var in common_vars:
data_features[var] = data_features[var].fillna(data_features[var].mode() [0])
 data_features['MSZoning'] = data_features.groupby('MSSubClass')['MSZoning'].transform(lambda x: x.fillna(x.mode() [0]))<categorify>
class CreateDataset(Dataset):
def __init__(self, df_data, data_dir = './', transform=None):
super().__init__()
self.df = df_data.values
self.data_dir = data_dir
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, index):
img_name,label = self.df[index]
img_path = os.path.join(self.data_dir, img_name)
 image = cv2.imread(img_path)
 if self.transform is not None:
image = self.transform(image)
 return image, label
for col in ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond','BsmtQual',
'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2',"PoolQC"
,'Alley','Fence','MiscFeature','FireplaceQu','MasVnrType','Utilities']:
data_features[col] = data_features[col].fillna('None')
<categorify>
transforms_train = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomHorizontalFlip() ,
transforms.RandomRotation(10),
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
])
 train_data = CreateDataset(df_data=train_df, data_dir=train_path, transform=transforms_train )
for col in('GarageYrBlt', 'GarageArea', 'GarageCars','MasVnrArea','BsmtFinSF1','BsmtFinSF2'
,'BsmtFullBath','BsmtHalfBath','FullBath','HalfBath','BsmtUnfSF','TotalBsmtSF'):
data_features[col] = data_features[col].fillna(0)
 data_features['LotFrontage'] = data_features.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.median()))
print('Features size:', data_features.shape )<data_type_conversions>
batch_size = 64
valid_size = 0.2
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 = DataLoader(train_data, batch_size=batch_size, sampler=train_sampler)
 valid_loader = DataLoader(train_data, batch_size=batch_size, sampler=valid_sampler )
data_features['Functional'] = data_features['Functional'].fillna('Typ')
<sort_values>
transforms_test = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
])
 sample_sub = pd.read_csv(".. /input/sample_submission.csv")
 test_data = CreateDataset(df_data=sample_sub, data_dir=test_path, transform=transforms_test)
 test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False )
total = data_features.isnull().sum().sort_values(ascending=False)
 percent =(data_features.isnull().sum() /data_features.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(10)
<feature_engineering>
classes = [ 'No Cactus','Cactus']
skewness = skewness[abs(skewness)> 0.5]
print("There are {} skewed numerical features to Box Cox transform".format(skewness.shape[0]))
print("Mean skewnees: {}".format(np.mean(skewness)))
 skewed_features = skewness.index
lam = 0.15
for feat in skewed_features:
feat_num[feat] = boxcox1p(feat_num[feat], boxcox_normmax(feat_num[feat] + 1))
data_features[feat] = boxcox1p(data_features[feat], boxcox_normmax(data_features[feat] + 1))
skewness.sort_values(ascending=False )<sort_values>
class CNN(nn.Module):
def __init__(self):
super(CNN, self ).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1)
 self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
 self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
 self.conv4 = nn.Conv2d(64, 128, 3, padding=1)
 self.pool = nn.MaxPool2d(2, 2)
 self.fc1 = nn.Linear(128*2*2, 512)
 self.fc2 = nn.Linear(512, 2)
 self.dropout = nn.Dropout(0.2)
 def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
 x = self.pool(F.relu(self.conv2(x)))
 x = self.pool(F.relu(self.conv3(x)))
 x = self.pool(F.relu(self.conv4(x)))
 x = x.view(-1, 128 * 2 * 2)
 x = self.dropout(x)
 x = F.relu(self.fc1(x))
x = self.dropout(x)
 x = self.fc2(x)
 return x
skewness = feat_num.apply(lambda x: skew(x))
skewness = skewness[abs(skewness)> 0.5]
print("There are {} skewed numerical features after Box Cox transform".format(skewness.shape[0]))
print("Mean skewnees: {}".format(np.mean(skewness)))
 skewness.sort_values(ascending=False)
<feature_engineering>
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...' )
data_features['TotalSF']=data_features['TotalBsmtSF'] + data_features['1stFlrSF'] + data_features['2ndFlrSF']
data_features['Total_Bathrooms'] =(data_features['FullBath'] +(0.5 * data_features['HalfBath'])+
data_features['BsmtFullBath'] +(0.5 * data_features['BsmtHalfBath']))
data_features['Total_porch_sf'] =(data_features['OpenPorchSF'] + data_features['3SsnPorch'] +
data_features['EnclosedPorch'] + data_features['ScreenPorch'] +
data_features['WoodDeckSF'])
<feature_engineering>
model = CNN()
print(model)
 if train_on_gpu: model.cuda()
data_features['haspool'] = data_features['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 data_features['hasgarage'] = data_features['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 data_features['hasbsmt'] = data_features['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 data_features['hasfireplace'] = data_features['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
 data_features = data_features.drop(['MasVnrArea', 'OpenPorchSF', 'WoodDeckSF', 'BsmtFinSF1','2ndFlrSF'], axis=1)
 print('data_features size:', data_features.shape)
<prepare_x_and_y>
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adamax(model.parameters() , lr=0.001 )
y_train = train['SalePrice']<categorify>
n_epochs = 30
valid_loss_min = np.Inf
train_losses = []
valid_losses = []
for epoch in range(1, n_epochs+1):
train_loss = 0.0
valid_loss = 0.0
model.train()
for data, target in 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() *data.size(0)
 model.eval()
for data, target in valid_loader:
if train_on_gpu:
data, target = data.cuda() , target.cuda()
output = model(data)
 loss = criterion(output, target)
 valid_loss += loss.item() *data.size(0)
 train_loss = train_loss/len(train_loader.sampler)
 valid_loss = valid_loss/len(valid_loader.sampler)
 train_losses.append(train_loss)
 valid_losses.append(valid_loss)
 print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
 epoch, train_loss, valid_loss))
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() , 'best_model.pt')
 valid_loss_min = valid_loss
data_features = data_features.drop("SalePrice", axis = 1)
 final_features = pd.get_dummies(data_features)
 print(final_features.shape)
 X = final_features.iloc[:len(y), :]
X_test = final_features.iloc[len(y):, :]
X.shape, y_train.shape, X_test.shape
print(X.shape,y_train.shape,X_test.shape)
<train_model>
model.load_state_dict(torch.load('best_model.pt'))
overfit = []
for i in X.columns:
counts = X[i].value_counts()
zeros = counts.iloc[0]
if zeros / len(X)* 100 > 99.95:
overfit.append(i)
 overfit = list(overfit)
 overfit.append('MSZoning_C(all)')
 X = X.drop(overfit, axis=1 ).copy()
X_test = X_test.drop(overfit, axis=1 ).copy()
print(X.shape,y_train.shape,X_test.shape)
<import_modules>
model.eval()
preds = []
for batch_i,(data, target)in enumerate(test_loader):
data, target = data.cuda() , target.cuda()
output = model(data)
 pr = output[:,1].detach().cpu().numpy()
for i in pr:
preds.append(i)
 sample_sub['has_cactus'] = preds
sample_sub.to_csv('submission.csv', index=False )