Kaggle competition: Digit Recognizer
data['text'] = data['text'].apply(lambda x: re.sub(re.compile(r'https?\S+'), '', x))
data['text'] = data['text'].apply(lambda x: re.sub(re.compile(r'[\//:,.!?@&\-'\`"\_
\
data['text'] = data['text'].apply(lambda x: re.sub(re.compile(r'<.*?>'), '', x))
data['text'] = data['text'].apply(lambda x: re.sub(re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE), '', x))
data['text'] = data['text'].apply(lambda x: re.sub(re.compile(r'\d'), '', x))
data['text'] = data['text'].apply(lambda x: re.sub(re.compile(r'[^\w]'), ' ', x))
data['text'] = data['text'].str.lower()<split>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
clean_train = data[0:train_data.shape[0]]
clean_submit = data[train_data.shape[0]:-1]
X_train, X_test, y_train, y_test = train_test_split(clean_train['text'], clean_train['target'],
test_size = 0.2, random_state = 4 )<feature_engineering>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
def tfidf(words):
tfidf_vectorizer = TfidfVectorizer()
data_feature = tfidf_vectorizer.fit_transform(words)
 return data_feature, tfidf_vectorizer
X_train_tfidf, tfidf_vectorizer = tfidf(X_train.tolist())
 X_test_tfidf = tfidf_vectorizer.transform(X_test.tolist() )<train_model>
X_train = train.iloc[:, 1:].values.reshape(-1,28,28,1)
 Y_train = train.iloc[:, 0]
x_test = test.values.reshape(-1,28,28,1 )
lr_tfidf = LogisticRegression(class_weight = 'balanced', solver = 'lbfgs', n_jobs = -1)
 lr_tfidf.fit(X_train_tfidf, y_train)
 y_predicted_lr = lr_tfidf.predict(X_test_tfidf )<compute_test_metric>
seed_value= 1998
os.environ['PYTHONHASHSEED']=str(seed_value)
 random.seed(seed_value)
 np.random.seed(seed_value)
 tf.set_random_seed(seed_value)
 config = tf.ConfigProto(
 device_count = {'GPU': 1, 'CPU': 100}, 
)
 set_session(tf.Session(config=config))
print(tensorflow_backend._get_available_gpus() )
def score_metrics(y_test, y_predicted):
accuracy = accuracy_score(y_test, y_predicted)
 precision = precision_score(y_test, y_predicted)
 recall = recall_score(y_test, y_predicted)
 print("accuracy = %0.3f, precision = %0.3f, recall = %0.3f" %(accuracy, precision, recall))<compute_test_metric>
X_train = X_train/255
x_test = x_test/255
score_metrics(y_test, y_predicted_lr )<train_on_grid>
KFold = 5
Folds = StratifiedKFold(n_splits=KFold, shuffle=True ).split(X_train, Y_train )
pipeline = Pipeline([ 
('clf', DecisionTreeClassifier(splitter='random', class_weight='balanced'))
])
 parameters = {
'clf__max_depth':(150,160,165),
'clf__min_samples_split':(18,20,23),
'clf__min_samples_leaf':(5,6,7)
 }
df_tfidf = GridSearchCV(pipeline, parameters, n_jobs=-1, verbose=-1, scoring='f1')
 df_tfidf.fit(X_train_tfidf, y_train)
 print(df_tfidf.best_estimator_.get_params() )<predict_on_test>
classifiers = []
for train_idx, valid_idx in Folds:
FOLD = len(classifiers)+1
print("STARTED TRAINING ON FOLD-%d"%(FOLD))
x_train, x_valid = X_train[train_idx], X_train[valid_idx]
y_train, y_valid = to_categorical(Y_train[train_idx], 10), to_categorical(Y_train[valid_idx], 10)
 datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False 
)
 datagen.fit(x_train)
 callbacks = [
EarlyStopping(monitor='val_loss', patience=15, verbose=1, mode='auto', restore_best_weights=True),
ModelCheckpoint(filepath='MNIST_DigitRecognizer_CNN_Fold%d.hdf5'%(FOLD), monitor='val_loss', save_best_only=True, verbose=1),
ReduceLROnPlateau(monitor='val_loss', patience=3, verbose=1, factor=0.5, min_lr=0.000001)
 ]
clf = Sequential([
Conv2D(32, kernel_size=(3,3), input_shape=(28,28,1), activation='relu', padding='same', kernel_initializer='glorot_uniform'),
Conv2D(32, kernel_size=(3,3), activation='relu', padding='same', kernel_initializer='glorot_uniform'),
MaxPooling2D() ,
Conv2D(64, kernel_size=(3,3), activation='relu', padding='same', kernel_initializer='glorot_uniform'),
Conv2D(64, kernel_size=(3,3), activation='relu', padding='same', kernel_initializer='glorot_uniform'),
MaxPooling2D() ,
Dropout(rate=0.15),
Flatten() ,
Dense(512, activation='relu', kernel_initializer='glorot_uniform'),
Dropout(rate=0.5),
Dense(10, activation='softmax', kernel_initializer='glorot_uniform')
 ])
 clf.compile(loss='categorical_crossentropy', optimizer=adam(lr=0.001))
train_history = clf.fit_generator(
 datagen.flow(x_train, y_train, batch_size=32),
validation_data=(x_valid, y_valid),
steps_per_epoch=len(x_train)/32,
epochs=50,
workers=6,
use_multiprocessing=True,
callbacks = callbacks,
verbose=0 
)
 plot_eval_result(train_hist = train_history)
 classifiers.append(clf)
y_predicted_dt = df_tfidf.predict(X_test_tfidf )<compute_test_metric>
final_result = sum(clf.predict(x_test)for clf in classifiers)/KFold
final_result = np.argmax(final_result, axis=1 )
score_metrics(y_test, y_predicted_dt )<install_modules>
y_pred = pd.DataFrame(data = final_result, index=list(range(1, test.shape[0]+1)) , columns = ['Label'])
 y_pred.to_csv("output.csv", index=True, index_label='ImageId' )