Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<drop_column>
import numpy as np
import pandas as pd
features_drop = [ 'SibSp', 'Parch', 'FamilySize', 'FareBand']
X = X.drop(features_drop, axis=1)
 X_test = X_test.drop(features_drop, axis=1)
 print(X)
 print(X_test )<normalization>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
sc = StandardScaler()
X = sc.fit_transform(X)
 print('X')
 print(X)
 X_test = sc.fit_transform(X_test)
 print('X_test')
 print(X_test )<compute_train_metric>
import matplotlib.pyplot as plt
classifier = LogisticRegression(random_state = 0)
 classifier.fit(X, y)
 y_pred_l_reg = classifier.predict(X_test)
 acc_l_reg = round(classifier.score(X, y)* 100, 2)
 print(str(acc_l_reg)+ ' percent')
<compute_train_metric>
tr_sample = train.drop('label', axis=1 ).values.reshape(-1,28,28)[0]
ts_sample = test.values.reshape(-1,28,28)[0]
clf = SVC()
clf.fit(X, y)
 y_pred_svc = clf.predict(X_test)
 acc_svc = round(clf.score(X, y)* 100, 2)
 print(acc_svc )<predict_on_test>
test['pixel345'].value_counts()
clf = KNeighborsClassifier(n_neighbors = 3)
 clf.fit(X, y)
 y_pred_knn = clf.predict(X_test)
 acc_knn = round(clf.score(X, y)* 100, 2)
 print(acc_knn)
<choose_model_class>
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
clf = DecisionTreeClassifier()
clf.fit(X, y)
 y_pred_decision_tree = clf.predict(X_test)
 acc_decision_tree = round(clf.score(X, y)* 100, 2)
 print(acc_decision_tree)
<predict_on_test>
x_test = test.values.reshape(-1,28,28,1)
 x_test = x_test/255
x_train_full = train.drop('label', axis=1 ).values.reshape(-1,28,28,1)
 y_train_full = train['label'].values
x_train, x_val, y_train, y_val = train_test_split(x_train_full, y_train_full, test_size=0.2, random_state=42 )
clf = RandomForestClassifier(n_estimators=100)
 clf.fit(X, y)
 y_pred_random_forest = clf.predict(X_test)
 acc_random_forest = round(clf.score(X, y)* 100, 2)
 print(acc_random_forest )<compute_train_metric>
batch_size=64
img_gen = ImageDataGenerator(rescale=1/255,
rotation_range=30,
zoom_range=.1,
shear_range=.1,
width_shift_range=.25,
height_shift_range=.25)
 train_gen = img_gen.flow(x_train, y_train,
batch_size=batch_size)
 valid_gen = img_gen.flow(x_val, y_val,
batch_size=batch_size,
shuffle=False )
sgd = SGDClassifier()
sgd.fit(X, y)
 Y_pred = sgd.predict(X_test)
 acc_sgd = round(sgd.score(X, y)* 100, 2)
 print(acc_sgd )<save_to_csv>
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Conv2D, MaxPool2D, Dropout, BatchNormalization, AveragePooling2D, GlobalAveragePooling2D
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau, TensorBoard
output = pd.DataFrame({'PassengerId': test_dataset.PassengerId, 'Survived': y_pred_l_reg})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved" )<import_modules>
early_stop = EarlyStopping(monitor='val_loss', patience=5, mode='min',restore_best_weights=True)
 check_point = ModelCheckpoint('digit_reg_mnist_z.h5', monitor='val_accuracy', save_best_only=True)
 lr_plateau = ReduceLROnPlateau(monitor='val_accuracy',
patience=2,
factor=.2,
min_lr=1e-6 )
import matplotlib.pyplot as plt
import sklearn.model_selection
from sklearn.preprocessing import MinMaxScaler
from sklearn.impute import KNNImputer
from sklearn.metrics import accuracy_score
import seaborn as sns
import numpy as np
import pandas as pd<load_from_csv>
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), input_shape=(28,28,1), padding='same'))
model.add(BatchNormalization(momentum=.9, epsilon=1e-5))
model.add(Activation('relu'))
model.add(Conv2D(64, kernel_size=(3,3), padding='same'))
model.add(BatchNormalization(momentum=.9, epsilon=1e-5))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Conv2D(64, kernel_size=(3,3), padding='same'))
model.add(BatchNormalization(momentum=.9, epsilon=1e-5))
model.add(Activation('relu'))
model.add(Conv2D(128, kernel_size=(3,3), padding='same'))
model.add(BatchNormalization(momentum=.9, epsilon=1e-5))
model.add(Activation('relu'))
model.add(AveragePooling2D(pool_size=(2,2)))
 model.add(Conv2D(128, kernel_size=(3,3), padding='same'))
model.add(BatchNormalization(momentum=.9, epsilon=1e-5))
model.add(Activation('relu'))
model.add(Conv2D(128, kernel_size=(3,3), padding='same'))
model.add(BatchNormalization(momentum=.9, epsilon=1e-5))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Conv2D(256, kernel_size=(3,3), padding='same'))
model.add(BatchNormalization(momentum=.9, epsilon=1e-5))
model.add(Activation('relu'))
model.add(Conv2D(256, kernel_size=(3,3), padding='same'))
model.add(BatchNormalization(momentum=.9, epsilon=1e-5))
model.add(Activation('relu'))
model.add(GlobalAveragePooling2D())
 model.add(Dense(10, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adamax',
metrics=['accuracy'])
 model.summary()
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()<load_from_csv>
model.fit(train_gen,
epochs=100,
steps_per_epoch=250,
validation_data=valid_gen,
callbacks=[lr_plateau, early_stop] )
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<concatenate>
import seaborn as sns
from sklearn.metrics import classification_report, confusion_matrix
all_data = pd.concat([train_data, test_data])
 all_data.head()<compute_test_metric>
eval_df = pd.DataFrame(model.history.history)
 length = len(eval_df )
survival_rate = train_data["Survived"].mean()
print(f"Survival rate: {survival_rate}")
 print(f"Death rate: {1-survival_rate}" )<define_variables>
pred = np.argmax(model.predict(valid_gen), axis=1)
 pred
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women )<filter>
print(classification_report(y_val, pred))
train_data.loc[train_data["Sex"] == "male"]["Embarked"]<normalization>
lr_plateau = ReduceLROnPlateau(monitor='accuracy',
patience=2,
factor=.2,
min_lr=1e-6)
 full_train_gen = img_gen.flow(x_train_full, y_train_full,
batch_size=batch_size )
features = ["Pclass", "Sex", "SibSp", "Parch", "Age", "Embarked"]
X_train = pd.get_dummies(train_data[features])
 imputer = KNNImputer(n_neighbors=5, weights='uniform', metric='nan_euclidean')
 X_train = imputer.fit_transform(X_train)
 scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
 y_train = np.array(train_data["Survived"])
 print(f"Shape of the training set: {X_train.shape}" )<train_on_grid>
model.fit(full_train_gen,
epochs=22,
steps_per_epoch=250,
callbacks=[lr_plateau] )
def trainClassifier(X_train, y_train, model_name, classifier, params, score, verbose=False, num_folds=10):
kf = sklearn.model_selection.StratifiedKFold(num_folds)
 train_scores = []
best_score = 0
for config in sklearn.model_selection.ParameterGrid(params):
train_scores_run = []
counts = []
for train_indices, valid_indices in kf.split(X_train, y_train):
counts.append(len(train_indices))
X_train_kf = X_train[train_indices]
y_train_kf = y_train[train_indices]
X_valid_kf = X_train[valid_indices]
y_valid_kf = y_train[valid_indices]
model = classifier(**config)
 model.fit(X_train_kf, y_train_kf)
 y_hat = model.predict(X_valid_kf)
 train_score = score(y_valid_kf, y_hat)
 train_scores_run.append(train_score)
 if np.average(train_scores_run, weights=counts)> best_score:
best_score = np.average(train_scores_run, weights=counts)
 best_config = config
if(verbose):
print("New best score obtained")
 print(f"Training with: {config}")
 print(f"Total Score obtained with cross validation: {best_score}
")
 train_scores.append(np.average(train_scores_run, weights=counts))
output_df = pd.DataFrame(data = [[model_name, best_config ,best_score]], \
columns=["model_name", "parameters", "training_score"])
 return output_df<create_dataframe>
real_pred = np.argmax(model.predict(x_test), axis=1 )
<choose_model_class><EOS> 
submit_df = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 submit_df['Label'] = real_pred
submit_df.to_csv('submission.csv', index=False )