Kaggle competition: Don't Get Kicked!
from torchvision import datasets,transforms
from torch.utils.data import TensorDataset, DataLoader,Dataset
from torch import nn,optim
import torch.nn.functional as F
import torchvision.transforms.functional as FF
import torch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<choose_model_class>
train_data=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()
class NeuralNetwork(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<data_type_conversions>
test_data=pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()
class MNIST_data(Dataset):

 def __init__(self, file_path,
transform = transforms.Compose([transforms.ToPILImage() , transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])
):
df = pd.read_csv(file_path)
 if len(df.columns)== 784:
self.X = df.values.reshape(( -1,28,28)).astype(np.uint8)[:,:,:,None]
self.y = None
else:
self.X = df.iloc[:,1:].values.reshape(( -1,28,28)).astype(np.uint8)[:,:,:,None]
self.y = torch.from_numpy(df.iloc[:,0].values)
 self.transform = transform
def __len__(self):
return len(self.X)
 def __getitem__(self, idx):
if self.y is not None:
return self.transform(self.X[idx]), self.y[idx]
else:
return self.transform(self.X[idx] )<load_from_csv>
train_data.isnull().sum()
batch_size = 64
train_dataset = MNIST_data('.. /input/digit-recognizer/train.csv', transform= transforms.Compose(
 [transforms.ToPILImage() ,transforms.ToTensor() , transforms.Normalize(mean=(0.5,), std=(0.5,)) ]))
test_dataset = MNIST_data('.. /input/digit-recognizer/test.csv')
 train_size = int(0.85 * len(train_dataset))
valid_size=int(0.15*len(train_dataset))
train_dataset,valid_dataset = torch.utils.data.random_split(train_dataset, [train_size,valid_size])
 train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size, shuffle=True)
 valid_loader = torch.utils.data.DataLoader(dataset=valid_dataset,
batch_size=batch_size, shuffle=True)
 test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size, shuffle=False )<load_from_csv>
train_data['IsBadBuy'].value_counts()
dftrain=pd.read_csv(".. /input/digit-recognizer/train.csv")
 dftest=pd.read_csv(".. /input/digit-recognizer/test.csv")
 submission=pd.read_csv(".. /input/digit-recognizer/sample_submission.csv" )<prepare_x_and_y>
train_data['Model'].value_counts()
train=np.array(dftrain.iloc[:,1:])
 labels=dftrain['label']<find_best_params>
train_data.drop('Model',axis=1,inplace=True)
 test_data.drop("Model",axis=1,inplace=True)
 train_data['Trim'].value_counts()
model=NeuralNetwork().cuda()
print(model )<choose_model_class>
train_data.drop('Trim',inplace=True,axis=1)
 test_data.drop('Trim',inplace=True,axis=1)
optimizer=optim.Adam(model.parameters() ,lr=0.0003)
 crit=nn.CrossEntropyLoss()<find_best_params>
train_data['SubModel'].value_counts()
epochs=30
step=0
train_losses,test_losses,accu=[],[],[]
for i in range(epochs):
running_loss=0
for images,labels in train_loader:
images,labels=images.cuda() ,labels.cuda()
optimizer.zero_grad()
logps=model(images)
 loss=crit(logps,labels)
 loss.backward()
optimizer.step()
running_loss+=loss.item()
else:
test_loss=0
accuracy=0
with torch.no_grad() :
model.eval()
for images,labels in valid_loader:
images,labels=images.cuda() ,labels.cuda()
logps=model(images)
 test_loss+=crit(logps,labels)
 ps=torch.exp(logps)
 topp,topc=ps.topk(1,dim=1)
 equal=topc==labels.view(*topc.shape)
 accuracy+=torch.mean(equal.type(torch.FloatTensor))
model.train()
train_losses.append(running_loss/len(train_loader))
test_losses.append(test_loss/len(valid_loader))
accu.append(accuracy)
 print("Epoch: {}/{}".format(i+1,epochs),
"Training Loss {}".format(running_loss/len(train_loader)) ,
"Testing Loss {}".format(test_loss/len(valid_loader)) ,
"Accuracy {}".format(accuracy/len(valid_loader)) )<define_variables>
train_data.drop('SubModel',inplace=True,axis=1)
 test_data.drop('SubModel',inplace=True,axis=1)
 train_data['Color'].value_counts()
pred=[]<find_best_params>
train_data['Color'].fillna(value='Color_Unknown',inplace=True)
 test_data['Color'].fillna(value='Color_Unknown',inplace=True)
 print("Number of null values in Color column "+str(train_data['Color'].isnull().sum()))
print("Number of null values in Color column "+str(test_data['Color'].isnull().sum()))
train_data['Transmission'].value_counts()
with torch.no_grad() :
model.eval()
for images in test_loader:
logps=model(images.cuda())
 ps=torch.exp(logps)
 topp,topc=ps.topk(1,dim=1)
 for i in topc:
pred.append(i[0].item())
<prepare_output>
train_data[train_data['Transmission']=='Manual']
submission['Label']=pred<save_to_csv>
train_data['Transmission'].replace("Manual","MANUAL",inplace=True)
 train_data['Transmission'].value_counts()
submission.to_csv("submissionD1.csv",index=False )<set_options>
train_data['Transmission'].fillna(value="Transmission_unk",inplace=True)
 test_data['Transmission'].fillna(value="Transmission_unk",inplace=True)
 train_data['WheelTypeID'].value_counts()
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
train_data.drop('WheelTypeID',inplace=True,axis=1)
 test_data.drop('WheelTypeID',inplace=True,axis=1)
 train_data['WheelType'].value_counts()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from fastai.vision import *
from PIL import Image<load_from_csv>
test_data['WheelType'].value_counts()
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )<set_options>
train_data['WheelType'].fillna(value='WheelType_unk',inplace=True)
 test_data['WheelType'].fillna(value='WheelType_unk',inplace=True)
 train_data['WheelType'].value_counts()
PATH.ls() 
<split>
train_data['Nationality'].value_counts()
train_img = train.iloc[:,1:785]
test_img = test.iloc[:,:]<categorify>
test_data['Nationality'].value_counts()
def save_img(data,fpath,isTest=False):
if isTest == False:
for index, row in data.iterrows() :
label,digit = row[0], row[1:]
filepath = fpath
filename = "train_{}.jpg".format(index)
 digit = digit.values
digit = digit.reshape(28,28)
 digit = digit.astype(np.uint8)
 img = Image.fromarray(digit)
 img.save(filepath/filename)
 else:
for index, row in data.iterrows() :
digit = row[:]
filepath = fpath
filename = "test_{}.jpg".format(index)
 digit = digit.values
digit = digit.reshape(28,28)
 digit = digit.astype(np.uint8)
 img = Image.fromarray(digit)
 img.save(filepath/filename )<save_model>
train_data['Nationality'].fillna(value='Nationality_unk',inplace=True)
 test_data['Nationality'].fillna(value='Nationality_unk',inplace=True)
 train_data['Size'].value_counts()
save_img(train,TRAIN,False )<sort_values>
test_data['Size'].value_counts()
sorted(os.listdir(TRAIN)) [0]<train_model>
train_data['Size'].fillna(value='Size_unk',inplace=True)
 test_data['Size'].fillna(value="Size_unk",inplace=True)
 train_data['TopThreeAmericanName'].value_counts()
save_img(test,TEST,True )<sort_values>
test_data['TopThreeAmericanName'].value_counts()
sorted(os.listdir(TEST)) [0]<feature_engineering>
train_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True)
 test_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True)
 train_data['PRIMEUNIT'].value_counts()
train['filename'] = ['train/train_{}.jpg'.format(x)for x,_ in train.iterrows() ]<feature_engineering>
test_data['PRIMEUNIT'].value_counts()
test['filename'] = ['test/test_{}.jpg'.format(x)for x,_ in test.iterrows() ]<load_pretrained>
train_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True)
 test_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True)
 train_data['AUCGUART'].replace("AGREEN","GREEN",inplace=True)
 test_data['AUCGUART'].replace("ARED","RED",inplace=True)
 train_data['AUCGUART'].fillna(value="AUC_unk",inplace=True)
 test_data['AUCGUART'].fillna(value="AUC_unk",inplace=True)
 train_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1)
 test_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1)
 train_data.drop('PurchDate',axis=1,inplace=True)
 test_data.drop("PurchDate",axis=1,inplace=True)
 train_data.shape
img = open_image(TRAIN/'train_0.jpg')
 img<set_options>
train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object'
not_categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']
for i in not_categorical:
maximum=np.max(train_data[i])
 train_data[i]=train_data[i]/maximum
maximum_test=np.max(test_data[i])
 test_data[i]=test_data[i]/maximum_test
train_data[not_categorical].head()
tfms =([*rand_pad(padding=3,size=28,mode='reflection'),zoom(scale=1.005),],[] )<load_pretrained>
categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']
categorical
src = ImageList.from_df(train,PATH,cols='filename' ).split_by_rand_pct(0.2 ).label_from_df(cols='label' ).add_test_folder(PATH/'test' )<normalization>
train_data[categorical[0]]
data = src.transform(tfms, size=28 ).databunch().normalize(imagenet_stats )<define_variables>
pd.get_dummies(train_data[categorical[0]] )
data.show_batch(2,2 )<find_best_params>
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)
 train_data.head()
data.classes,data.c<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))
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))
train_data.shape
learn = cnn_learner(data,models.resnet50,metrics=accuracy,model_dir='/kaggle/working/' )<find_best_params>
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42)
 print(X_train.shape,y_train.shape)
 print(X_test.shape,y_test.shape )
learn.lr_find(num_it=600 )<set_options>
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )
learn.recorder.plot(skip_end=25 )<define_search_space>
KNN.score(X_test,y_test )
lr=4e-3<train_model>
predict=KNN.predict(test_data.drop('RefId',axis=1))
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head()
learn.fit_one_cycle(10,slice(lr))<save_model>
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True)
 Submission.head()
Submission.to_csv('Submission.csv')