Kaggle competition: Kannada MNIST
dataset['text'] = dataset['text'].map(lambda x: x.lower())
 train_data['text'] = train_data['text'].map(lambda x: x.lower())
 dataset['text']<split>
train_df = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 val_df = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv' )
dis_text = train_data['text'][train_data['target']==1]
dis_text 
<filter>
def df_to_image(df):
imgs = []
labels = []
for idx in tqdm_notebook(range(len(df))):
row = df.iloc[idx].values
label, img = row[0], row[1:]
labels.append(label)
 imgs.append(img.reshape(1, 28, 28 ).astype('uint8'))
imgs = np.concatenate(imgs, axis=0)
 labels = np.array(labels)
 return imgs, labels
ndis_text = train_data['text'][train_data['target']==0]
ndis_text<string_transform>
train_img, train_label = df_to_image(train_df)
 val_img, val_label = df_to_image(val_df )
dis_count = dis_text.map(lambda x: nltk.word_tokenize(x))
ndis_count = ndis_text.map(lambda x : nltk.word_tokenize(x))
dataset['text'] = dataset['text'].map(lambda x : nltk.word_tokenize(x))
ndis_count<categorify>
model = torchvision.models.resnet34()
model.fc
collection_words_dist = []
for i in list(dis_count):
collection_words_dist.extend(i)
 map_1 = FreqDist(collection_words_dist)
 map_1<choose_model_class>
model.fc = nn.Linear(512, 10 )
lemmatizer = WordNetLemmatizer()
def nltk_tags_2_word_tags(nltk_tag):
if nltk_tag.startswith('J'):
return wordnet.ADJ
elif nltk_tag.startswith('V'):
return wordnet.VERB
elif nltk_tag.startswith('N'):
return wordnet.NOUN
elif nltk_tag.startswith('R'):
return wordnet.ADV
else:
return None
def lemmatized_sentences(tokenized_sentence):
pos = nltk.pos_tag(tokenized_sentence)
 wordnet_tagged = map(lambda x:(x[0], nltk_tags_2_word_tags(x[1])) , pos)
 lemmatized_sentence = []
for word, tag in wordnet_tagged:
if tag is None:
lemmatized_sentence.append(word)
 else:
lemmatized_sentence.append(lemmatizer.lemmatize(word, tag))
return " ".join(lemmatized_sentence)
<categorify>
model.conv1 = nn.Conv2d(1, 64, kernel_size=(3), stride=1, padding=1 )
dis_lem = dis_count.apply(lemmatized_sentences)
 ndis_lem = ndis_count.apply(lemmatized_sentences)
 dataset['text'] = dataset['text'].apply(lemmatized_sentences)
 dataset 
<feature_engineering>
class DigDataset(data_util.Dataset):
def __init__(self, imgs, labels):
super(DigDataset, self ).__init__()
self.imgs = imgs.reshape(-1, 28, 28, 1)
 self.labels = labels
self.trans = torchvision.transforms.ToTensor()
def __len__(self):
return len(self.labels)
 def __getitem__(self, idx):
img = self.imgs[idx]
label = self.labels[idx]
return self.trans(img), torch.tensor(label )
def remove_pattern(input_txt, pattern):
reg_obj = re.compile(pattern)
 input_txt = reg_obj.sub(r'', input_txt)
 return input_txt 
<feature_engineering>
batch_size = 40
train_set = DigDataset(train_img, train_label)
 val_set = DigDataset(val_img, val_label)
 train_loader = data_util.DataLoader(train_set, batch_size=batch_size, num_workers=2, pin_memory=True, shuffle=True)
 val_loader = data_util.DataLoader(val_set, batch_size=batch_size, num_workers=2, pin_memory=True, shuffle=False )
dataset['text'] = dataset['text'].apply(lambda x: remove_pattern(x,"@[\w]*"))<feature_engineering>
cuda = torch.cuda.is_available()
dataset['text'] = dataset['text'].apply(lambda x: remove_pattern(x,'https?://\S+|www\.\S+'))
dataset['text'] = dataset['text'].apply(lambda x: remove_pattern(x,'<.*?>')) 
<feature_engineering>
def generate_matrix(gt, pre, num_class=10):
mask =(gt >= 0)&(gt < num_class)
 label = num_class * gt[mask].astype('int')+ pre[mask]
count = np.bincount(label, minlength=num_class**2)
 confusion_matrix = count.reshape(num_class, num_class)
 return confusion_matrix
dataset['text'] = dataset['text'].apply(lambda x: remove_pattern(x,"[^a-zA-Z<feature_engineering>
def do_train(model, optim, data_loader, epoch):
model.train()
confusion_matrix = np.zeros(( 10, 10))
tbar = tqdm_notebook(train_loader)
 for img, label in tbar:
if cuda:
img, label = img.cuda() , label.cuda()
optim.zero_grad()
logit = model(img)
 loss = F.cross_entropy(logit, label)
 loss.backward()
optim.step()
pred = torch.argmax(logit, dim=1)
 batch_acc =(pred == label ).sum() / len(pred)
 tbar.set_description(f'Train loss: {loss.item() :.4f} acc: {batch_acc}')
 confusion_matrix += generate_matrix(label.cpu().numpy() , pred.cpu().numpy())
 acc = np.diag(confusion_matrix ).sum() / confusion_matrix.sum()
print(f'Epoch: {epoch_idx} acc: {acc}')
 def do_eval(model, data_loader, epoch):
model.eval()
confusion_matrix = np.zeros(( 10, 10))
tbar = tqdm_notebook(train_loader)
 for img, label in tbar:
if cuda:
img, label = img.cuda() , label.cuda()
with torch.no_grad() :
logit = model(img)
 loss = F.cross_entropy(logit, label)
 pred = torch.argmax(logit, dim=1)
 batch_acc =(pred == label ).sum() / len(pred)
 tbar.set_description(f'Val loss: {loss.item() :.4f} acc: {batch_acc}')
 confusion_matrix += generate_matrix(label.cpu().numpy() , pred.cpu().numpy())
 acc = np.diag(confusion_matrix ).sum() / confusion_matrix.sum()
print(f'Epoch: {epoch_idx} val acc: {acc}')
 return acc
tfidf = TfidfVectorizer(ngram_range =(1,1),max_df=0.90, min_df=2,stop_words = 'english')
 text_set = tfidf.fit_transform(dataset['text'])
 text_set<concatenate>
epoch = 20
if cuda:
model = model.cuda()
optim = torch.optim.Adam(params=model.parameters())
 bst_model = None
bst_acc = 0.
 for epoch_idx in range(epoch):
do_train(model, optim, train_loader, epoch_idx)
 val_acc = do_eval(model, val_loader, epoch_idx)
 if bst_acc < val_acc:
bst_model = deepcopy(model)
 bst_acc = val_acc
dataset_dtm = hstack(( text_set,np.array(dataset['location'])[:,None])) 
<feature_engineering>
test_df = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv' )
dataset_dtm=text_set<prepare_x_and_y>
test_img, ids = df_to_image(test_df )
dataset_dtm = dataset_dtm.tocsr()
x_train = dataset_dtm[0:len(train_data)]
x_test = dataset_dtm[len(train_data):]
x_train.shape<define_variables>
test_set = DigDataset(test_img, ids)
 test_loader = data_util.DataLoader(test_set, batch_size=batch_size, num_workers=2, shuffle=False )
y_train = train_data['target']
len(y_train )<choose_model_class>
submit = {
'id': [],
'label': []
}
if cuda:
bst_model = bst_model.cuda()
for b_imgs, b_ids in tqdm_notebook(test_loader):
if cuda:
b_imgs = b_imgs.cuda()
with torch.no_grad() :
logit = bst_model(b_imgs)
 pred = torch.argmax(logit, dim=1)
 pred = pred.cpu().numpy()
submit['label'].extend(pred.tolist())
 submit['id'].extend(b_ids.cpu().numpy().tolist())
kfold = StratifiedKFold(n_splits = 5)
<choose_model_class>
submit_df = pd.DataFrame(submit)
 submit_df.head()
random_state = 2
classifiers = []
classifiers.append(SVC(kernel = 'rbf',probability = True))
classifiers.append(DecisionTreeClassifier(random_state=random_state))
classifiers.append(RandomForestClassifier(random_state=random_state))
classifiers.append(ExtraTreesClassifier(random_state=random_state))
classifiers.append(GradientBoostingClassifier(random_state=random_state))
classifiers.append(MLPClassifier(random_state=random_state))
classifiers.append(KNeighborsClassifier())
 classifiers.append(LogisticRegression())
 classifiers.append(XGBClassifier(random_state=random_state))

<train_on_grid>
submit_df.to_csv('submission.csv', index=False )