Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<drop_column>
np.random.seed(0)
 sns.set(style='white', context='notebook', palette='deep')
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
merge.drop('PdDistrict', axis = 1, inplace = True )<categorify>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )
merge['DayWeek'] = lb.fit_transform(merge['DayOfWeek'])
 merge[['DayOfWeek', 'DayWeek']].head(10 )<drop_column>
tmp = train["label"]
X_train, X_test, y_train, y_test = train_test_split(train.drop(labels=["label"],axis=1),
tmp,
test_size = 0.01,
random_state = 0 )
merge.drop('DayOfWeek', axis = 1, inplace = True )<feature_engineering>
y_train = to_categorical(y_train, num_classes = 10)
 y_test = to_categorical(y_test, num_classes = 10)
 plt.imshow(X_train[0][:,:,0],cmap='gray' )
merge['Block'] = merge['Address'].str.contains('block', case = False)
 merge['ST'] = merge['Address'].str.contains('ST', case = False)
 merge.drop('Address', axis = 1, inplace = True )<feature_engineering>
epochs = 30
batchsize = 16
steps_per_epoch = X_train.shape[0] / batchsize
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
factor=0.5,
min_lr=0.00001)
 dataset = ImageDataGenerator()
dataset.fit(X_train )
medX= merge[merge['X'] < -120.5]['X'].median()
medY = merge[merge['Y'] < 90]['Y'].median()
merge.loc[merge['X'] >= -120.5, 'X'] = medX
merge.loc[merge['Y'] >= 90, 'Y'] = medY<feature_engineering>
merge['X+Y'] = merge['X'] + merge['Y']
merge['X-Y'] = merge['X'] - merge['Y']<drop_column>
merge.drop('Date', axis = 1, inplace = True )<split>
data_train = merge[:train.shape[0]]
data_test = merge[train.shape[0]:]<train_model>
trains = lg.Dataset(data_train, label = tar, categorical_feature=['PdDis', 'DayWeek'])
 params = {
'boosting':'gbdt',
'objective':'multiclass',
'num_class':39,
'max_delta_step':0.9,
'min_data_in_leaf': 20,
'learning_rate': 0.4,
'max_bin': 480,
'num_leaves': 45,
'verbose' : 1
}
bst = lg.train(params, trains, 120 )<predict_on_test>
prediction = bst.predict(data_test )<save_to_csv>
result1 = pd.DataFrame(prediction, columns = LB.inverse_transform(np.linspace(0, 38, 39, dtype='int16')) ,index=data_test.index)
 result1.head()
result1.to_csv('final_submission', index_label = 'Id' )<set_options>
pd.set_option('max_colwidth', 40 )<feature_engineering>
model = Sequential()
model.add(Conv2D(32,kernel_size=3,padding='same',activation='relu',
input_shape=(28,28,1)))
 model.add(Conv2D(32,kernel_size=3,padding='same',activation='relu',
input_shape=(28,28,1)))
 model.add(MaxPool2D())
 model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Conv2D(64,kernel_size=3,padding='same',activation='relu',
input_shape=(28,28,1)))
 model.add(Conv2D(64,kernel_size=3,padding='same',activation='relu',
input_shape=(28,28,1)))
 model.add(MaxPool2D())
 model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
MAX_LEN = 96
PATH = '.. /input/tf-roberta/'
tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file=PATH+'vocab-roberta-base.json',
merges_file=PATH+'merges-roberta-base.txt',
lowercase=True,
add_prefix_space=True 
)
 EPOCHS = 3
BATCH_SIZE = 32
PAD_ID = 1
SEED = 88888
LABEL_SMOOTHING = 0.1
tf.random.set_seed(SEED)
 np.random.seed(SEED)
 sentiment_id = {'positive': 1313, 'negative': 2430, 'neutral': 7974}
train = pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv' ).fillna('')
 for k in range(train.shape[0]):
train.loc[k,'text'] = re.sub(r"(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?", " ", train.loc[k,'text'])
 train.loc[k,'text'] = re.sub(r'https?://\S+', ' ', train.loc[k,'text'])
 train.loc[k,'text'] = re.sub(r"(www.[a-z.\/0-9]*)", " ", train.loc[k,'text'])
 train.loc[k,'text'] = re.sub(' +', ' ', train.loc[k,'text'])
 train.loc[k,'selected_text'] = re.sub(r"(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?", " ", train.loc[k,'selected_text'])
 train.loc[k,'selected_text'] = re.sub(r'https?://\S+', ' ', train.loc[k,'selected_text'])
 train.loc[k,'selected_text'] = re.sub(r"(www.[a-z.\/0-9]*)", " ", train.loc[k,'selected_text'])
 train.loc[k,'selected_text'] = re.sub(' +', ' ', train.loc[k,'selected_text'])
 train.head()<feature_engineering>
print("-------------------------------------------------------------------")
 print('my model')
 history = model.fit_generator(augmented_dataset.flow(X_train,y_train,batch_size=batchsize),
epochs=epochs, validation_data=(X_test,y_test),
steps_per_epoch=X_train.shape[0]//batchsize,
callbacks=[learning_rate_reduction])
 print("OuO")
 fig, ax = plt.subplots(2,1)
 ax[0].plot(history.history['loss'], color='b', label="Training loss")
 legend = ax[0].legend(loc='best', shadow=True)
 ax[1].plot(history.history['accuracy'], color='b', label="Training accuracy")
 legend = ax[1].legend(loc='best', shadow=True )
test = pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv' ).fillna('')
 for k in range(test.shape[0]):
test.loc[k,'text'] = re.sub(r"(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?", " ", test.loc[k,'text'])
 test.loc[k,'text'] = re.sub(r'https?://\S+', ' ', test.loc[k,'text'])
 test.loc[k,'text'] = re.sub(r"(www.[a-z.\/0-9]*)", " ", test.loc[k,'text'])
 test.loc[k,'text'] = re.sub(' +', ' ', test.loc[k,'text'])
 ct = test.shape[0]
input_ids_t = np.ones(( ct,MAX_LEN),dtype='int32')
 attention_mask_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 token_type_ids_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 for k in range(test.shape[0]):
text1 = " "+" ".join(test.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 s_tok = sentiment_id[test.loc[k,'sentiment']]
input_ids_t[k,:len(enc.ids)+3] = [0, s_tok] + enc.ids + [2]
attention_mask_t[k,:len(enc.ids)+3] = 1<init_hyperparams>
predictions = model.predict(test)
 pre_res = np.argmax(predictions,axis = 1)
 res = pd.Series(pre_res,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),res],axis = 1)
 submission.to_csv("mnist_submission.csv",index=False )