Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<train_model>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from keras import datasets
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout, BatchNormalization
from keras.utils.vis_utils import plot_model
def train_gbm_cls(X_tr, y_tr, X_val, y_val, X_test, y_test):
gbm_cls = LGBMClassifier(
 objective = 'binary',
class_weight = 'balanced' 
)
 gbm_cls.fit(
 X_tr, y_tr,
eval_set =(X_val, y_val),
early_stopping_rounds = 20,
verbose = 0, 
)
 print('Train')
 print(classification_report(y_train, gbm_cls.predict(X_tr)))
 print('Validation')
 print(classification_report(y_val, gbm_cls.predict(X_val)))
 print('Test')
 gbm_y_hat = gbm_cls.predict(X_test)
 print(classification_report(y_test, gbm_y_hat))
return gbm_cls, gbm_y_hat
train_feat = tr_mean_max
val_feat = val_mean_max
test_feat = test_mean_max
gbm_cls, gbm_y_hat = train_gbm_cls(train_feat, y_train, val_feat, y_val , test_feat, y_test )<prepare_output>
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
sub = pd.DataFrame(columns = ['id', 'target'])
 sub['id'] = test_df.id
sub['target'] = gbm_y_hat 
<save_to_csv>
print(f"Missing values in the train set: {train_df.isnull().any(axis=None)}",
f"Missing values in the test set: {test_df.isnull().any(axis=None)}", sep='
' )
nsub = pd.DataFrame(columns = ['id', 'target'])
 nsub['id'] = test_df.id
nsub['target'] = y_model_hat
nsub.to_csv('submission.csv', index = False )<install_modules>
y_train = pd.get_dummies(train_df['label'] ).values
X_train = train_df.values[:,1:] / 255.0
X_train = X_train.reshape(( -1, 28, 28, 1))
X_test = test_df.values / 255.0
X_test = X_test.reshape(( -1, 28, 28, 1))
!pip install tweet_preprocessor
!pip install datasets<load_from_csv>
show_some_img(X_train, y_train, 10 )
train = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 test = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv' )<load_from_csv>
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.05, random_state=1)
 X_train, X_local_test, y_train, y_local_test = train_test_split(X_train, y_train, test_size = 0.02, random_state=2 )
sample_submission = pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv')
 sample_submission.head(20 )<correct_missing_values>
print(f"Train set size: {len(X_train)}", f"Validation set size: {len(X_val)}", f"Test set size: {len(X_local_test)}", sep='
' )
train.dropna()<filter>
img_rows, img_cols = 28, 28
model = Sequential()
model.add(Conv2D(32, kernel_size=5, activation='relu', padding='same', input_shape=(img_rows, img_cols, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size=5, activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2), padding="same"))
model.add(Dropout(0.2))
model.add(Conv2D(64, kernel_size=3, activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=3, activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2), padding="same"))
model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )
print('train positive samples: %d' % train[train['target'] == 1].shape[0])
 print('train negative samples: %d' % train[train['target'] == 0].shape[0] )<categorify>
datagen = ImageDataGenerator(
 rotation_range=15,
zoom_range = 0.15,
width_shift_range=0.1,
height_shift_range=0.1)
 datagen.fit(X_train )
def preprocess(text):
text = text.replace("
text = p.clean(text)
 return text
train['text'] = train['text'].apply(lambda x: preprocess(x))
test['text'] = test['text'].apply(lambda x: preprocess(x))
print(train['text'].values.tolist() [:5])
 print(test['text'].values.tolist() [:5] )<split>
augmented_images, augmented_labels = datagen.flow(X_train, y_train, batch_size=10 ).next()
show_some_img(augmented_images, augmented_labels, 10 )
X_train, X_val, y_train, y_val = train_test_split(train['text'], train['target'], test_size=.1, random_state=42)
 X_test = test['text'].values.tolist()
X_train = X_train.tolist()
X_val = X_val.tolist()
y_train = y_train.tolist()
y_val = y_val.tolist()<load_pretrained>
batch_size = 400
training = model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size),
validation_data=(X_val, y_val), epochs=70, steps_per_epoch=X_train.shape[0] // batch_size )
model = AutoModelForSequenceClassification.from_pretrained('vinai/bertweet-large')
 tokenizer = AutoTokenizer.from_pretrained("vinai/bertweet-large", use_fast=False )<categorify>
model.evaluate(X_local_test, y_local_test, batch_size=batch_size )
train_encodings = tokenizer(X_train, truncation=True, padding=True)
 val_encodings = tokenizer(X_val, truncation=True, padding=True )<categorify>
y_local_pred = model.predict(X_local_test )
class DisasterDataset(torch.utils.data.Dataset):
def __init__(self, encodings, labels):
self.encodings = encodings
self.labels = labels
def __getitem__(self, idx):
item = {key: torch.tensor(val[idx])for key, val in self.encodings.items() }
item['labels'] = torch.tensor(self.labels[idx])
 return item
def __len__(self):
return len(self.labels)
 train_dataset = DisasterDataset(train_encodings, y_train)
 val_dataset = DisasterDataset(val_encodings, y_val )<compute_train_metric>
show_some_wrong_img(X_local_test, y_local_test, y_local_pred, 10 )
acc = load_metric('accuracy')
 precision = load_metric('precision')
 recall = load_metric('recall')
 f1 = load_metric('f1')
 def compute_metrics(eval_pred):
predictions, labels = eval_pred
predictions = np.argmax(predictions, axis=1)
 acc_result = acc.compute(predictions=predictions, references=labels)
 precision_result = precision.compute(predictions=predictions, references=labels)
 recall_result = recall.compute(predictions=predictions, references=labels)
 f1_result = f1.compute(predictions=predictions, references=labels)
 result = {**acc_result, **precision_result, **recall_result, **f1_result}
return result
training_args = TrainingArguments(
 output_dir='./results',
num_train_epochs=2,
per_device_train_batch_size=64,
per_device_eval_batch_size=128,
warmup_steps=500,
weight_decay=0.01,
logging_dir='./logs',
logging_steps=20,
evaluation_strategy='steps' 
)
 trainer = Trainer(
 model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
compute_metrics=compute_metrics 
)
 trainer.train()<feature_engineering>
y_pred = np.argmax(model.predict(X_test), axis=1)
 result = pd.DataFrame({'ImageId':range(1, len(y_pred)+1), 'Label':y_pred})
 result.head()
<save_to_csv><EOS> 
result.to_csv('my_submission.csv', index=False )