Kaggle competition: QSTP - Deep Learning 2019
<SOS> metric: categorizationaccuracy Kaggle data source: qstp-deep-learning-2019<import_modules>
%matplotlib inline
warnings.filterwarnings("ignore")
<load_from_csv>
df = pd.read_csv('.. /input/train.csv')
 df.head()
train=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 sample_sub=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv')
<sort_values>
class ImageDataset(Dataset):
def __init__(self, csv_file, root_dir, transform=None):

 self.data_frame = pd.read_csv(csv_file)
 self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.data_frame)
 def __getitem__(self, idx):
img_name = os.path.join(self.root_dir, self.data_frame['Id'][idx])
 image = Image.open(img_name ).convert('RGB')
 label = np.array(self.data_frame['Category'][idx])
 if self.transform:
image = self.transform(image)
 sample =(image, label)
 return sample
corr_dict=correlation_train['SalePrice'].sort_values(ascending=False ).to_dict()
important_columns=[]
for key,value in corr_dict.items() :
if(( value>0.1)&(value<0.8)) |(value<=-0.1):
important_columns.append(key)
 important_columns<concatenate>
for i in range(len(trainset)) :
sample = trainset[i]
print(i, sample[0].size() , " | Label: ", sample[1])
 if i == 9:
break
train_test=pd.concat([train,test],axis=0,sort=False)
 train_test.head()<create_dataframe>
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...' )
pd.set_option('display.max_rows', 5000)
 train_test_null_info=pd.DataFrame(train_test.isnull().sum() ,columns=['Count of NaN'])
 train_test_dtype_info=pd.DataFrame(train_test.dtypes,columns=['DataTypes'])
 train_tes_info=pd.concat([train_test_null_info,train_test_dtype_info],axis=1)
 train_tes_info 
<data_type_conversions>

 dir(models)
 model=models.resnext101_32x8d(pretrained=True)
 model
train_test.loc[train_test['Fireplaces']==0,'FireplaceQu']='Nothing'
train_test['LotFrontage'] = train_test['LotFrontage'].fillna(train_test.groupby('1stFlrSF')['LotFrontage'].transform('mean'))
train_test['LotFrontage'].interpolate(method='linear',inplace=True)
 train_test['LotFrontage']=train_test['LotFrontage'].astype(int)
 train_test['MasVnrArea'] = train_test['MasVnrArea'].fillna(train_test.groupby('MasVnrType')['MasVnrArea'].transform('mean'))
train_test['MasVnrArea'].interpolate(method='linear',inplace=True)
 train_test['MasVnrArea']=train_test['MasVnrArea'].astype(int)
 train_test["Fence"] = train_test["Fence"].fillna("None")
 train_test["FireplaceQu"] = train_test["FireplaceQu"].fillna("None")
 train_test["Alley"] = train_test["Alley"].fillna("None")
 train_test["PoolQC"] = train_test["PoolQC"].fillna("None")
 train_test["MiscFeature"] = train_test["MiscFeature"].fillna("None")
 train_test.loc[train_test['BsmtFinSF1']==0,'BsmtFinType1']='Unf'
train_test.loc[train_test['BsmtFinSF2']==0,'BsmtQual']='TA'
train_test['YrBltRmd']=train_test['YearBuilt']+train_test['YearRemodAdd']
train_test['Total_Square_Feet'] =(train_test['BsmtFinSF1'] + train_test['BsmtFinSF2'] + train_test['1stFlrSF'] + train_test['2ndFlrSF'] + train_test['TotalBsmtSF'])
 train_test['Total_Bath'] =(train_test['FullBath'] +(0.5 * train_test['HalfBath'])+ train_test['BsmtFullBath'] +(0.5 * train_test['BsmtHalfBath']))
train_test['Total_Porch_Area'] =(train_test['OpenPorchSF'] + train_test['3SsnPorch'] + train_test['EnclosedPorch'] + train_test['ScreenPorch'] + train_test['WoodDeckSF'])
 train_test['exists_pool'] = train_test['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 train_test['exists_garage'] = train_test['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 train_test['exists_fireplace'] = train_test['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
 train_test['exists_bsmt'] = train_test['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 train_test['old_house'] = train_test['YearBuilt'].apply(lambda x: 1 if x <1990 else 0)
 for i in train_test.columns:
if 'SalePrice' not in i:
if 'object' in str(train_test[str(i)].dtype):
train_test[str(i)]=train_test[str(i)].fillna(method='ffill' )<categorify>
for param in model.parameters() :
param.requires_grad = False
columns =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual',
'GarageCond', 'ExterQual', 'ExterCond','HeatingQC', 'PoolQC',
'KitchenQual', 'BsmtFinType1', 'YrSold', 'MoSold', 'BsmtFinType2',
'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond')
 for col in columns:
lbl_enc = LabelEncoder()
lbl_enc.fit(list(train_test[col].values))
train_test[col] = lbl_enc.transform(list(train_test[col].values))<sort_values>
model.fc = nn.Sequential(
 nn.Linear(2048, 512),
nn.ReLU() ,
nn.Dropout(0.05),
nn.Linear(512,67), 
)
 model.cuda()
model
numeric_features = train_test.dtypes[train_test.dtypes != "object"].index
skewed_features = train_test[numeric_features].apply(lambda x: skew(x)).sort_values(ascending=False)
 print(skewed_features)
<filter>
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.fc.parameters() , lr=3e-4 )
high_skewness = skewed_features[abs(skewed_features)> 0.9]
skewed_features = high_skewness.index 
<feature_engineering>
total_params = sum(p.numel() for p in model.parameters())
 print(f'{total_params:,} total parameters.')
 total_trainable_params = sum(
 p.numel() for p in model.parameters() if p.requires_grad)
 print(f'{total_trainable_params:,} training parameters.' )
for feature in skewed_features:
train_test[feature] = boxcox1p(train_test[feature], boxcox_normmax(train_test[feature] + 1))<categorify>
epochs=4
iter=0
for epoch in range(epochs):
for i,(images,labels)in enumerate(trainloader):
images=Variable(images.cuda())
 labels=Variable(labels.cuda())
 optimizer.zero_grad()
outputs=model(images)
 loss=criterion(outputs, labels)
 loss.backward()
optimizer.step()
iter+=1
if iter%500==0:
correct=0
total=0
for images, labels in trainloader:
images=Variable(images.cuda())
 outputs=model(images)
 _, predicted=torch.max(outputs.data,1)
 total+=labels.size(0)
 correct+=(predicted.cpu() ==labels.cpu() ).sum()
accuracy=(100*correct/total)
 print("Iteration {}, Loss {}, Accuracy {}".format(iter, loss.data.item() , accuracy))
train_test=pd.get_dummies(train_test,dtype='int8' )<create_dataframe>
for param in model.layer4.parameters() :
param.requires_grad = True
train_test_null_info=pd.DataFrame(train_test.isnull().sum() ,columns=['Count of NaN'])
 train_test_dtype_info=pd.DataFrame(train_test.dtypes,columns=['DataTypes'])
 train_test_info=pd.concat([train_test_null_info,train_test_dtype_info],axis=1)
 train_test_info<split>
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters() , lr=2e-4 )
train=train_test[0:1460]
test=train_test[1460:2919]<sort_values>
total_params = sum(p.numel() for p in model.parameters())
 print(f'{total_params:,} total parameters.')
 total_trainable_params = sum(
 p.numel() for p in model.parameters() if p.requires_grad)
 print(f'{total_trainable_params:,} training parameters.' )
corr_dict2=corr_new_train['SalePrice'].sort_values(ascending=False ).to_dict()
corr_dict2 
<define_variables>
for param in model.layer3.parameters() :
param.requires_grad = True
best_columns=[]
for key,value in corr_dict2.items() :
if(( value>=0.3175)&(value<0.9)) |(value<=-0.315):
best_columns.append(key)
 best_columns<feature_engineering>
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters() , lr=1e-4 )
train['SalePrice_Log1p'] = np.log1p(train.SalePrice)
<train_on_grid>
total_params = sum(p.numel() for p in model.parameters())
 print(f'{total_params:,} total parameters.')
 total_trainable_params = sum(
 p.numel() for p in model.parameters() if p.requires_grad)
 print(f'{total_trainable_params:,} training parameters.' )
dbscan = DBSCAN(eps=1400, min_samples=20 ).fit(train_pca)
 core_samples_mask = np.zeros_like(dbscan.labels_, dtype=bool)
 core_samples_mask[dbscan.core_sample_indices_] = True
labels=dbscan.labels_
n_clusters_ = len(set(labels)) -(1 if -1 in labels else 0 )<create_dataframe>
test_transforms = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])
 testset = ImageDataset(csv_file = '.. /input/sample_sub.csv', root_dir = '.. /input/data/data/', transform=test_transforms)
 testloader = torch.utils.data.DataLoader(testset, batch_size=16, shuffle=False, num_workers=4 )
labels=pd.DataFrame(labels,columns=['Classes'])
 print(labels[labels['Classes']==-1] )<concatenate>
submission = pd.read_csv('.. /input/sample_sub.csv')
 submission.head()
<filter><EOS> 

 submission.to_csv('submission.csv', index=False, encoding='utf-8')