Kaggle competition: Kannada MNIST
print('n per class:
{}'.format(train_df['target'].value_counts()))<count_values>
train = '/kaggle/input/Kannada-MNIST/train.csv'
test = '/kaggle/input/Kannada-MNIST/test.csv'
dataset = pd.read_csv(train)
 dataset_test = pd.read_csv(test)
 dataset_test = dataset_test.drop(['id'], axis = 1)
 features = dataset.drop(['label'], axis = 1)
 labels = dataset['label']
train_df['location'].value_counts()<count_missing_values>
seed = 300
test_size = 0.2
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size = test_size, random_state = seed, stratify=labels)
 X_train = np.repeat(X_train[..., np.newaxis], 1, -1)
 X_test = np.repeat(X_test[..., np.newaxis], 1, -1)
 features_test = np.repeat(features_test[..., np.newaxis], 1, -1)
 y_train = np_utils.to_categorical(np.array(y_train), 10)
 y_test = np_utils.to_categorical(np.array(y_test), 10)
 print("features set has {} samples.".format(features.shape))
print("Training set has {} samples.".format(X_train.shape))
print("Testing set has {} samples.".format(X_test.shape))
train_df['keyword'].isna().value_counts()<prepare_x_and_y>
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=5, padding='same', activation='relu', input_shape=(28, 28, 1)))
 model.add(Conv2D(filters=16, kernel_size=5, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.4))
model.add(Conv2D(filters=32, kernel_size=5, padding='same', activation='relu'))
model.add(Conv2D(filters=32, kernel_size=5, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.4))
model.add(Conv2D(filters=64, kernel_size=5, padding='same', activation='relu'))
model.add(Conv2D(filters=64, kernel_size=5, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )
a = train_df['text']<count_unique_values>
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 WEIGHTS_FILE = 'weights.base.hdf5'
epochs = 100
checkpointer = ModelCheckpoint(filepath='WEIGHTS_FILE',
verbose=1, save_best_only=True)
 history = model.fit(X_train,
y_train,
validation_data=(X_test, y_test),
epochs=epochs,
batch_size=200,
callbacks=[checkpointer],
verbose=1 )
all_words = []
for t in train_df['text'].values:
all_words.extend(wordpunct_tokenize(t))
counter = Counter(all_words)
 counter.most_common(10 )<categorify>
print(features_test.shape)
 predictions = [np.argmax(model.predict(np.expand_dims(tensor, axis=0)))for tensor in features_test]
print(np.array(predictions))
def remove_url(text):
url = re.compile(r'https?://\S+|www\.\S+')
 return url.sub(r'',text)
 def remove_numbers(text):
return re.sub(r'[^a-zA-Z']', ' ', text)
 def remove_unicode(text):
return re.sub(r'[^\x00-\x7F]+', '', text)
 def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 stop_words = set(stopwords.words('english'))
def remove_stopwords(text):
text = text.lower()
text = " ".join([w for w in wordpunct_tokenize(text)if w not in stop_words])
 return text
p = inflect.engine()
def convert_number(text):
temp_str = text.split()
new_string = []
for word in temp_str:
if word.isdigit() :
temp = p.number_to_words(word)
 new_string.append(temp)
 else:
new_string.append(word)
 temp_str = ' '.join(new_string)
 return temp_str 
<choose_model_class>
!rm weights.base.hdf5
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/2"
bert_layer = hub.KerasLayer(module_url, trainable=True )<feature_engineering>
print(np.array(predictions))
predictions_df = pd.DataFrame(np.array(predictions), columns = ['Label'])
 predictions_df.reset_index(level=0, inplace=True)
 predictions_df.columns = ['id', 'label']
predictions_df.index += 1
predictions_df.to_csv('predictions.csv', index = False)
 predictions_df.head()