Kaggle competition: Digit Recognizer
def submission_encoder(df:pd.DataFrame)-> np.ndarray:
dct = {}
for i in tqdm(df['image_id'].unique()):
if not i in dct.keys() :
dct[i] = []
tmp = df[df['image_id'] == i].values
for j in tmp:
dct[i].append(int(j[1]))
dct[i].append(float(j[2]))
dct[i].append(int(j[3]))
dct[i].append(int(j[4]))
dct[i].append(int(j[5]))
dct[i].append(int(j[6]))
dct[i] = map(str,dct[i])
 dct[i] = ' '.join(dct[i])
 dct = [[k, v] for k, v in dct.items() ]
return pd.DataFrame(dct,columns = ['image_id','PredictionString'] ).reset_index(drop = True)
 df = submission_encoder(final_df)
 df.to_csv('Fold5Yolo.csv', index=False )<merge>
%matplotlib inline
NORMAL = "14 1 0 0 1 1"
low_threshold = 0.00
high_threshold = 0.99
pred_det_df = df
n_normal_before = len(pred_det_df.query("PredictionString == @NORMAL"))
merged_df = pd.merge(pred_det_df, pred_2cls, on="image_id", how="left")
 if "target" in merged_df.columns:
merged_df["class0"] = 1 - merged_df["target"]
c0, c1, c2 = 0, 0, 0
for i in range(len(merged_df)) :
p0 = merged_df.loc[i, "class0"]
if p0 < low_threshold:
c0 += 1
elif low_threshold <= p0 and p0 < high_threshold:
if ' 14 ' not in merged_df.loc[i, "PredictionString"]:
merged_df.loc[i, "PredictionString"] += f" 14 {p0} 0 0 1 1"
c1 += 1
else:
merged_df.loc[i, "PredictionString"] = NORMAL
c2 += 1
n_normal_after = len(merged_df.query("PredictionString == @NORMAL"))
print(
 f"n_normal: {n_normal_before} -> {n_normal_after} with threshold {low_threshold} & {high_threshold}" 
)
 print(f"Keep {c0} Add {c1} Replace {c2}")
 submission_filepath = str("submission.csv")
 submission_df = merged_df[["image_id", "PredictionString"]]
submission_df.to_csv(submission_filepath, index=False)
 print(f"Saved to {submission_filepath}" )<import_modules>
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 submit_df = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv' )
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import RidgeCV<load_from_csv>
num_pixel = len(train_df.columns)- 1
num_pixel
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )<count_missing_values>
transform_0 = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_1 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomRotation(30),
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_2 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=15, translate=(0.1,0.1), scale=(0.8,0.8)) ,
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_3 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=30, scale=(1.1,1.1)) ,
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_4 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=30, translate=(0.1,0.1)) ,
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_5 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=10, shear=45),
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ] )
sum(train.isnull().sum() )<count_missing_values>
class DataFrame_to_Dataset(Dataset):
def __init__(self, df, transform=transform_0):
if len(df.columns)== num_pixel:
self.features = df.values.reshape(( -1,28,28)).astype(np.uint8)
 self.labels = None
else:
self.features = df.iloc[:,1:].values.reshape(( -1,28,28)).astype(np.uint8)
 self.labels = torch.from_numpy(df.label.values)
 self.transform = transform
def __len__(self):
return len(self.features)
 def __getitem__(self, index):
if self.labels is not None:
return self.transform(self.features[index]), self.labels[index]
else:
return self.transform(self.features[index] )
sum(test.isnull().sum() )<concatenate>
def create_dataloaders(seed, test_size=0.1, df=train_df, batch_size=32):
train_data, valid_data = train_test_split(df,
test_size=test_size,
random_state=seed)
 train_dataset_0 = DataFrame_to_Dataset(train_data)
 train_dataset_1 = DataFrame_to_Dataset(train_data, transform_1)
 train_dataset_2 = DataFrame_to_Dataset(train_data, transform_2)
 train_dataset_3 = DataFrame_to_Dataset(train_data, transform_3)
 train_dataset_4 = DataFrame_to_Dataset(train_data, transform_4)
 train_dataset_5 = DataFrame_to_Dataset(train_data, transform_5)
 train_dataset = ConcatDataset([train_dataset_0, train_dataset_1, train_dataset_2, train_dataset_3, train_dataset_4, train_dataset_5])
 valid_dataset = DataFrame_to_Dataset(valid_data)
 train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
 valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=batch_size, shuffle=False)
 return train_loader, valid_loader
house_data = pd.concat([train.iloc[:,:-1], test],axis=0)
 house_data = house_data.drop(columns=['Id'], axis=1)
 sep = len(train )<data_type_conversions>
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Sequential(
 nn.Conv2d(1, 32, kernel_size=3),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, kernel_size=3),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, kernel_size=5, stride=2, padding=14),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Dropout2d(0.4)
)
 self.conv2 = nn.Sequential(
 nn.Conv2d(32, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=5, stride=2, padding=6),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Dropout2d(0.3)
)
 self.conv3 = nn.Sequential(
 nn.Conv2d(64, 128, kernel_size=4),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Dropout2d(0.2)
)
 self.fc = nn.Sequential(
 nn.Linear(128*1*1, 10)
)
 def forward(self, x):
x = self.conv1(x)
 x = self.conv2(x)
 x = self.conv3(x)
 x = x.view(-1, 128*1*1)
 x = self.fc(x)
 return x
n_columns = [name for name in house_data.columns if house_data[name].dtype in ['int64', 'float64']]
w_columns = [name for name in house_data.columns if house_data[name].dtype == "object"]
values = {}
for a in w_columns:
values[a] = 'UNKNOWN'
for a in n_columns:
values[a] = house_data[a].median()
house_data.fillna(value=values,inplace=True)
 house_data.head()<drop_column>
use_cuda = torch.cuda.is_available()
print(use_cuda )
house_data = house_data.drop(['PoolQC'], axis=1 )<categorify>
def train(seed, num_epochs):
print('Creating new dataloaders...')
 train_loader, valid_loader = create_dataloaders(seed=seed)
 print('Creating a new model...')
 net = Net()
criterion = nn.CrossEntropyLoss()
if use_cuda:
net.cuda()
criterion.cuda()
optimizer = optim.Adam(net.parameters() ,
lr=0.003, betas=(0.9, 0.999),
eps=1e-08, weight_decay=0,
amsgrad=False)
 scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1)
 print('Training the model...')
 for epoch in range(num_epochs):
net.train()
t0 = time.time()
training_loss = 0.0
num_samples = 0
for features, labels in train_loader:
if use_cuda:
features = features.cuda()
labels = labels.cuda()
optimizer.zero_grad()
outputs = net(features)
 loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
training_loss += loss.item()
num_samples += len(features)
 net.eval()
correct = 0
total = 0
with torch.no_grad() :
for valid_features, valid_labels in valid_loader:
if use_cuda:
valid_features = valid_features.cuda()
valid_labels = valid_labels.cuda()
outputs = net(valid_features)
 _, predicted = torch.max(outputs, 1)
 total += valid_labels.size(0)
 correct +=(predicted == valid_labels ).sum().item()
scheduler.step()
print('[model %d, epoch %d, time: %.3f seconds] train_loss: %.5f, val_acc: %4f %%' % 
(seed + 1, epoch + 1, time.time() - t0, training_loss/num_samples, 100 * correct / total))
net.eval()
test_pred = torch.LongTensor()
if use_cuda:
test_pred = test_pred.cuda()
with torch.no_grad() :
for features in test_loader:
if use_cuda:
features = features.cuda()
outputs = net(features)
 _, predicted = torch.max(outputs, 1)
 test_pred = torch.cat(( test_pred, predicted), dim=0)
 model_name = 'model_' + str(seed + 1)
 ensemble_df[model_name] = test_pred.cpu().numpy()
print('Prediction Saved!
' )
house_data = pd.get_dummies(house_data )<prepare_x_and_y>
test_dataset = DataFrame_to_Dataset(test_df)
 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=32, shuffle=False)
 ensemble_df = submit_df.copy()
num_models = 23
num_epochs = 6
for seed in range(num_models):
train(seed, num_epochs )
y_train = train.iloc[:,-1]
X_train = house_data.iloc[:sep, :]
X_test = house_data.iloc[sep:, :]<train_model>
final_pred = ensemble_df.iloc[:,2:].mode(axis=1 ).iloc[:,0]
submit_df.Label = final_pred.astype(int)
 submit_df.head()
search_rf = RandomForestRegressor(n_estimators = 100, random_state=0)
 search_rf.fit(X_train, y_train)
 y_test_rf = search_rf.predict(X_test)
 print('Random forest accuracy:', search_rf.score(X_train, y_train))<save_to_csv>
submit_df.to_csv('submission.csv', index=False )