Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<count_missing_values>
np.random.seed(2)
 %matplotlib inline
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
sum(test_data.Pclass.isna() )<categorify>
train=pd.read_csv('.. /input/digit-recognizer/train.csv')
 test=pd.read_csv('.. /input/digit-recognizer/test.csv')
 print(train.shape)
 print(test.shape)
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>
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 del train
print(X_train.shape)
 print(Y_train.shape)
 X_train = X_train / 255.0
test = test / 255.0
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1)
 print(X_train.shape)
 print(test.shape )
sum(test_data.SibSp.isna() )<feature_engineering>
Y_train = to_categorical(Y_train, num_classes = 10 )
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>
class LeNet:
	@staticmethod
	def build(input_shape, classes):
		model = Sequential()
		
		model.add(Conv2D(20, kernel_size=5, padding="same",
			input_shape=input_shape))
		model.add(Activation("relu"))
		model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 		
		model.add(Conv2D(50, kernel_size=5, padding="same"))
		model.add(Activation("relu"))
		model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 		
		model.add(Flatten())
 		model.add(Dense(500))
		model.add(Dropout(0.3))
		model.add(Activation("relu"))
		
		model.add(Dense(classes))
		model.add(Activation("softmax"))
		return model
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>
NB_EPOCH = 20
BATCH_SIZE = 128
VERBOSE = 1
OPTIMIZER = Adam()
VALIDATION_SPLIT=0.2
IMG_ROWS, IMG_COLS = 28, 28
NB_CLASSES = 10
INPUT_SHAPE =(IMG_ROWS, IMG_COLS, 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 = LeNet.build(input_shape=INPUT_SHAPE, classes=NB_CLASSES)
 model.compile(loss="categorical_crossentropy", optimizer=OPTIMIZER,
	metrics=["accuracy"])
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>
history = model.fit(X_train, Y_train,
		batch_size=BATCH_SIZE, epochs=NB_EPOCH,
		verbose=VERBOSE, validation_split=VALIDATION_SPLIT)
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix<split>
lr_reduction = ReduceLROnPlateau(monitor='val_accuracy', patience=3, verbose=1, factor=0.5, min_lr=0.00001)
 X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2)
 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 )
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>
NB_EPOCH = 30
BATCH_SIZE = 128
VERBOSE = 1
model.compile(loss="categorical_crossentropy", optimizer=OPTIMIZER,
	metrics=["accuracy"])
 history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=BATCH_SIZE),
epochs = NB_EPOCH, validation_data =(X_val,Y_val),
verbose = VERBOSE, steps_per_epoch=X_train.shape[0] // BATCH_SIZE
, callbacks=[lr_reduction] )
log_reg = LogisticRegression()
log_reg.fit(X_train, y_train)
 y_pred = log_reg.predict(X_test)
 y_pred<compute_test_metric>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
<compute_test_metric><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("LeNet_mnist_datagen.csv",index=False)