Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<filter>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
pclass1 = train_data.loc[train_data.Pclass == 1]['Survived']
print('Class1',sum(pclass1)/len(pclass1))
pclass2 = train_data.loc[train_data.Pclass == 2]['Survived']
print('Class2',sum(pclass2)/len(pclass2))
pclass3 = train_data.loc[train_data.Pclass == 3]['Survived']
print('Class3',sum(pclass3)/len(pclass3))<count_missing_values>
dataset = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 dataset
sum(test_data.Pclass.isna() )<categorify>
np.any(dataset.isnull().sum() )
train_data['class1'] = pd.get_dummies(train_data.Pclass)[1]
test_data['class1'] = pd.get_dummies(test_data.Pclass)[1]
train_data['class2'] = pd.get_dummies(train_data.Pclass)[2]
test_data['class2'] = pd.get_dummies(test_data.Pclass)[2]<count_missing_values>
scaler = StandardScaler()
X = scaler.fit_transform(X)
 X
sum(test_data.SibSp.isna() )<feature_engineering>
test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 X_test = test_data.values
X_test.shape
sibs = train_data.loc[train_data.SibSp <= 1]['Survived']
print(sum(sibs)/len(sibs))
train_data['many_sibs'] =(train_data.SibSp > 1)*1
test_data['many_sibs'] =(test_data.SibSp > 1)*1<filter>
X_test_scaled = scaler.fit_transform(X_test)
 X_test_scaled
young = train_data.loc[train_data.Age <= 15]['Survived']
print(sum(young)/len(young))
old = train_data.loc[train_data.Age >=40]['Survived']
print(sum(old)/len(old))<feature_engineering>
datagen = ImageDataGenerator(rotation_range=10, zoom_range = 0.1, width_shift_range=0.1, height_shift_range=0.1)
bins = [0.42, 15, 30, 50,80]
train_data['bin_age'] = pd.cut(x=train_data.Age, bins=bins)
 test_data['bin_age'] = pd.cut(x=test_data.Age, bins=bins )<categorify>
model_list = []
train_data['young'] = pd.get_dummies(train_data.bin_age ).iloc[:,0]
test_data['young'] = pd.get_dummies(test_data.bin_age ).iloc[:,0]
train_data['senior'] = pd.get_dummies(train_data.bin_age ).iloc[:,3]
test_data['senior'] = pd.get_dummies(test_data.bin_age ).iloc[:,3]<import_modules>
reduce_lr = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.2,
min_lr=1e-6 )
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix<split>
for i in range(7):
model = keras.models.Sequential([
keras.layers.Conv2D(32, kernel_size = 3, activation = 'relu', padding = 'same', input_shape = [28, 28, 1]),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(64, kernel_size = 3, activation = 'relu', padding = 'same'),
keras.layers.MaxPool2D() ,
keras.layers.Conv2D(32, kernel_size = 3, activation = 'relu', padding = 'same'),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(64, kernel_size = 3, activation = 'relu', padding = 'same'),
keras.layers.MaxPool2D() ,
keras.layers.Flatten() ,
keras.layers.Dropout(0.25),
keras.layers.Dense(256, activation = 'relu'),
keras.layers.Dropout(0.5),
keras.layers.Dense(10, activation = 'softmax')])
 model.compile(loss = 'sparse_categorical_crossentropy', optimizer = 'nadam', metrics = ['accuracy'])
 model_list.append(model)
X = train_data[features]
y = train_data.Survived
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 0 )<choose_model_class>
history = [0] * 7
for i in range(7):
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size = 0.08)
 history[i] = model_list[i].fit_generator(datagen.flow(X, y, batch_size = 64),
epochs = 20, validation_data =(X_valid, y_valid), callbacks = [reduce_lr])
 print("CNN : {} Maximum Train Accuracy : {} Maximum Validation Accuracy : {}".format(i+1, max(history[i].history['accuracy']), max(history[i].history['val_accuracy'])) )
log_reg = LogisticRegression()
log_reg.fit(X_train, y_train)
 y_pred = log_reg.predict(X_test)
 y_pred<compute_test_metric>
X_test_scaled = X_test_scaled.reshape(( 28000, 28, 28, 1))
X_test_scaled.shape
accuracy_score(y_pred, y_test )<compute_test_metric>
for i in range(7):
ensemble_cnn_pred = ensemble_cnn_pred + model_list[i].predict(X_test_scaled )
confusion_matrix(y_pred, y_test )<train_model>
np.sum(ensemble_cnn_pred[0] )
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X_train, y_train)
 y_pred = model.predict(X_test )<compute_test_metric>
ensemble_cnn_pred = np.argmax(ensemble_cnn_pred, axis = 1)
 ensemble_cnn_pred[0]
accuracy_score(y_pred, y_test )<compute_test_metric>
pred_df_ensemble_cnn = pd.DataFrame(columns = ['ImageId', 'Label'])
 pred_df_ensemble_cnn['ImageId'] = np.arange(1, 28001)
 pred_df_ensemble_cnn['Label'] = ensemble_cnn_pred
pred_df_ensemble_cnn
<feature_engineering><EOS> 
pred_df_ensemble_cnn.to_csv('ens_cnn_with_aug_sub.csv', index = False )