Kaggle competition: Digit Recognizer
name_list_test = test_data.Name.to_list()
print(name_list_test )<count_values>
import matplotlib.pyplot as plt
import seaborn as sns
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D , MaxPool2D , Flatten , Dropout
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report,confusion_matrix
list_of_titles_test = title_retriever(name_list_test)
 print(len(list_of_titles_test))
test_data['Title'] = list_of_titles_test
test_data.Title.value_counts()<categorify>
train_df = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test_df = pd.read_csv(".. /input/digit-recognizer/test.csv")
 submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv" )
Title_Age_median_test = test_data.groupby(['Title'] ).Age.transform('median')
 test_data.Age.fillna(Title_Age_median_test, inplace = True)
<categorify>
y_train = train_df['label']
y = train_df['label']
del train_df['label']
X = train_data[features]
y = train_data.Survived
Pclass_Fare_median = X.groupby('Pclass' ).Fare.transform('median')
 X.Fare.fillna(Pclass_Fare_median, inplace = True)
 X.Embarked.fillna('S', inplace = True)
 X['Embarked'].replace({'S': 0, 'Q': 1, 'C': 2}, inplace = True)
 X['Sex'].replace({'female': 0, 'male': 1}, inplace = True)
 X.info()
print(X.Sex)
<save_to_csv>
label_binarizer = LabelBinarizer()
y_train = label_binarizer.fit_transform(y_train)
 print(train_df.shape,y_train.shape)
X_test = test_data[features]
clf.fit(X, y)
 predictions = clf.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )<load_from_csv>
x_train = x_train / 255
x_test = x_test / 255
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )<filter>
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 )
women = train_data.loc[train_data.Sex == 'female']['Survived']
print('Women survived',sum(women)/len(women))
men = train_data.loc[train_data.Sex == 'male']['Survived']
print('Men survived',sum(men)/len(men))<categorify>
x_train,x_val,y_train,y_val = train_test_split(x_train,y_train,test_size = 0.1)
 print(x_train.shape,x_val.shape,y_train.shape,y_val.shape )
train_data['female'] = pd.get_dummies(train_data['Sex'])['female']
test_data['female'] = pd.get_dummies(test_data['Sex'])['female']<feature_engineering>
model = Sequential()
model.add(Conv2D(150 ,(3,3), strides = 1 , padding = 'same' , activation = 'relu' , input_shape =(28,28,1)))
 model.add(MaxPool2D(( 2,2), strides = 2 , padding = 'same'))
model.add(Conv2D(100 ,(3,3), strides = 1 , padding = 'same' , activation = 'relu'))
model.add(Dropout(0.1))
model.add(MaxPool2D(( 2,2), strides = 2 , padding = 'same'))
model.add(Conv2D(75 ,(5,5), strides = 1 , padding = 'same' , activation = 'relu'))
model.add(Dropout(0.2))
model.add(MaxPool2D(( 2,2), strides = 2 , padding = 'same'))
model.add(Conv2D(50 ,(4,4), strides = 1 , padding = 'same' , activation = 'relu'))
model.add(Dropout(0.15))
model.add(MaxPool2D(( 2,2), strides = 2 , padding = 'same'))
model.add(Conv2D(25 ,(4,4), strides = 1 , padding = 'same' , activation = 'relu'))
model.add(MaxPool2D(( 2,2), strides = 2 , padding = 'same'))
model.add(Flatten())
 model.add(Dense(units = 512 , activation = 'relu'))
model.add(Dropout(0.25))
model.add(Dense(units = 10 , activation = 'softmax'))
model.compile(optimizer = 'adam' , loss = 'categorical_crossentropy' , metrics = ['accuracy'])
 model.summary()
sum(train_data['Age'].isnull())
 train_data['Age'] = train_data['Age'].fillna(train_data['Age'].mean())
 test_data['Age'] = test_data['Age'].fillna(test_data['Age'].mean() )<filter>
history = model.fit(datagen.flow(x_train,y_train, batch_size = 256),epochs = 30 , validation_data =(x_val, y_val))
<filter><EOS> 
y_pred=model.predict_classes(x_test)
 submission.head()
submission=pd.DataFrame({'ImageId': submission.ImageId,'Label':y_pred})
 submission.to_csv('/kaggle/working/submission.csv',index=False)
 check=pd.read_csv('/kaggle/working/submission.csv')
 check