Kaggle competition: Don't Get Kicked!
y_train = keras.utils.to_categorical(y_train, num_classes)
 X_train, X_val, Y_train, Y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state=42 )<choose_model_class>
train_data = pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()
model = Sequential()
model.add(Conv2D(32, kernel_size= 3,activation='relu',kernel_initializer='he_normal',input_shape=input_shape))
model.add(Conv2D(32, kernel_size= 3,activation='relu',kernel_initializer='he_normal'))
model.add(MaxPool2D(( 2, 2)))
 model.add(Dropout(0.20))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size =(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(Conv2D(64, kernel_size =(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(BatchNormalization())
 model.add(Conv2D(128, kernel_size =(3, 3), activation='relu',padding='same',kernel_initializer='he_normal'))
model.add(Dropout(0.25))
model.add(Flatten())
 model.add(BatchNormalization())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(num_classes, activation='softmax'))<choose_model_class>
test_data = pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer= keras.optimizers.RMSprop() ,
metrics=['accuracy'] )<choose_model_class>
train_data.isnull().sum()
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.0001 )<define_variables>
train_data['IsBadBuy'].value_counts()
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False )<train_model>
train_data['Model'].value_counts()
datagen.fit(X_train)
<train_model>
train_data.drop('Model',axis = 1,inplace = True)
 test_data.drop('Model',axis = 1,inplace = True)
h = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 1, steps_per_epoch=(X_train.shape[0] // batch_size)
 , callbacks=[learning_rate_reduction], )<compute_test_metric>
train_data['Trim'].value_counts()
final_loss, final_acc = model.evaluate(X_val, Y_val, verbose=0)
 print(final_loss, final_acc )<predict_on_test>
train_data.drop('Trim',axis = 1,inplace = True)
 test_data.drop('Trim',axis = 1,inplace = True )
predicted_classes = model.predict_classes(X_test )<save_to_csv>
train_data['SubModel'].value_counts()
submissions=pd.DataFrame({"ImageId": list(range(1,len(predicted_classes)+1)) ,
"Label": predicted_classes})
 submissions.to_csv("final_submission_V5.csv", index=False, header=True )<prepare_x_and_y>
train_data.drop('SubModel',axis = 1,inplace = True)
 test_data.drop('SubModel',axis = 1,inplace = True )
<choose_model_class>
train_data['Color'].value_counts()
<choose_model_class>
train_data['Color'].fillna(value = 'Color_Unkown',inplace=True)
 test_data['Color'].fillna(value= 'Color_Unknown',inplace =True )
<train_model>
train_data['Transmission'].value_counts()
<import_modules>
train_data['Transmission'].replace('Manual','MANUAL',inplace=True )
<train_model>
train_data["Transmission"].value_counts()
<load_from_csv>
train_data['WheelTypeID'].value_counts()
<save_to_csv>
train_data.drop('WheelTypeID',axis = 1,inplace = True)
 test_data.drop('WheelTypeID',axis = 1,inplace = True )
<set_options>
train_data['WheelType'].value_counts()
%matplotlib inline 
<load_from_csv>
train_data['WheelType'].fillna(value = 'WheelType_unk',inplace=True)
 test_data['WheelType'].fillna(value= 'WheelType_unk',inplace =True )
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' )<define_variables>
train_data['Nationality'].value_counts()
num_pixel = len(train_df.columns)- 1
num_pixel<categorify>
train_data['Nationality'].fillna(value = 'Nationality_unk',inplace=True)
 test_data['Nationality'].fillna(value= 'Nationality_unk',inplace =True )
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])
 ] )<data_type_conversions>
train_data["Size"].value_counts()
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] )<split>
train_data['Size'].fillna(value = 'Size_unk',inplace=True)
 test_data['Size'].fillna(value= 'Size_unk',inplace =True )
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<choose_model_class>
train_data['TopThreeAmericanName'].value_counts()
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.25)
)
 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.25)
)
 self.conv3 = nn.Sequential(
 nn.Conv2d(64, 128, kernel_size=4),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Dropout2d(0.25)
)
 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<set_options>
train_data['TopThreeAmericanName'].fillna(value = 'TopThreeAmericanName_unk',inplace=True)
 test_data['TopThreeAmericanName'].fillna(value= 'TopThreeAmericanName_unk',inplace =True )
use_cuda = torch.cuda.is_available()
print(use_cuda )<choose_model_class>
train_data['PRIMEUNIT'].value_counts()
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!
' )<create_dataframe>
train_data['PRIMEUNIT'].fillna(value = 'PRIMEUNIT_unk',inplace=True)
 test_data['PRIMEUNIT'].fillna(value= 'PRIMEUNIT_unk',inplace =True )
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 = 11
num_epochs = 6
for seed in range(num_models):
train(seed, num_epochs )<data_type_conversions>
train_data['AUCGUART'].value_counts()
final_pred = ensemble_df.iloc[:,2:].mode(axis=1 ).iloc[:,0]
submit_df.Label = final_pred.astype(int)
 submit_df.head()<save_to_csv>
train_data['AUCGUART'].fillna(value = 'AUCGUART_unk',inplace=True)
 test_data['AUCGUART'].fillna(value= 'AUCGUART_unk',inplace =True )
submit_df.to_csv('submission.csv', index=False )<set_options>
train_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice','MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice','MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice','MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'
],axis = 1,inplace = True)
 test_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice','MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice','MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice','MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'
],axis = 1,inplace = True )
clear_output()<import_modules>
train_data.drop('PurchDate',axis = 1,inplace = True)
 test_data.drop('PurchDate',axis = 1,inplace = True )
tensorflow_backend._get_available_gpus()<load_from_csv>
non_categorical = train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']
train=pd.read_csv(r'.. /input/train.csv')
 test=pd.read_csv(r'.. /input/test.csv' )<categorify>
for i in non_categorical:
maximum = np.max(train_data[i])
 train_data[i]/=maximum
maximum_test = np.max(test_data[i])
 test_data[i]/=maximum_test
labels=pd.get_dummies(train.iloc[:,0] ).values<split>
categorical = train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']
X_train,X_val,y_train,y_val=train_test_split(data,labels,test_size=0.33,random_state=42 )<create_dataframe>
pd.get_dummies(train_data[categorical[0]] )
aug_data=ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1 )<compute_test_metric>
for i in categorical:
dummies = pd.get_dummies(train_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_data = pd.concat([train_data,dummies],axis=1)
 train_data.drop(i,inplace=True,axis=1)
 dummies = pd.get_dummies(test_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_data = pd.concat([test_data,dummies],axis=1)
 test_data.drop(i,inplace=True,axis=1 )
generator=aug_data.flow(X_train,y_train,batch_size=64 )<choose_model_class>
for i in train_data.drop('IsBadBuy',axis=1 ).columns:
if i not in test_data.columns:
test_data[i]=np.zeros(len(test_data))
def create_model() :
model=Sequential()
model.add(Conv2D(32,(3,3),input_shape=(28,28,1),activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,(3,3),activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,(5,5),strides=(2,2),padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,(3,3),activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,(5,5),strides=(2,2),padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128,(4,4),activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dense(128,activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(10,activation='softmax'))
model.compile(optimizer='adam',loss=categorical_crossentropy,metrics=['accuracy'])
 return model<choose_model_class>
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))
model=create_model()<train_model>
test_data = test_data[train_data.drop('IsBadBuy',axis=1 ).columns]
history=model.fit_generator(generator,epochs=50,validation_data=(X_val,y_val),steps_per_epoch=data.shape[0]//64 )<concatenate>
X = train_data.drop(['RefId','IsBadBuy'],axis=1)
 y = train_data['IsBadBuy']
generator=aug_data.flow(data,labels,batch_size=64 )<choose_model_class>
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=42 )
model=create_model()<train_model>
from sklearn.neighbors import KNeighborsClassifier
history=model.fit_generator(generator,epochs=50,validation_data=(X_val,y_val),steps_per_epoch=data.shape[0]//64 )<prepare_x_and_y>
from sklearn.neighbors import KNeighborsClassifier
x_test=test.values.reshape(-1,28,28,1)/255<predict_on_test>
from sklearn.neighbors import KNeighborsClassifier
pred=np.argmax(model.predict(x=x_test),1 )<concatenate>
KNN = KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )
output=np.concatenate(( np.arange(1,test.shape[0]+1 ).reshape(-1,1),pred.reshape(-1,1)) ,axis=1 )<create_dataframe>
KNN.score(X_test,y_test )
submission=pd.DataFrame(output,columns=['ImageId','Label'] )<save_to_csv>
predict = KNN.predict(test_data.drop('RefId',axis=1))
submission.to_csv('Submission.csv',index=False )<import_modules>
Submissions = pd.DataFrame(predict,columns=['IsBadBuy'])
 Submissions.head()
from IPython.display import FileLink<save_model>
Submissions["RefId"]=test_data['RefId']
Submissions.set_index('RefId',inplace=True )
model.save('model.h5' )<import_modules>
Submissions.to_csv('Submissions.csv' )