Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<data_type_conversions>
%matplotlib inline
_NUMERIC_COLS_WITH_MISSING_VALUES = ['Age', 'Fare', 'Parch', 'Pclass', 'SibSp']
for _col in _NUMERIC_COLS_WITH_MISSING_VALUES:
__simple_imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
 __train_dataset[_col] = __simple_imputer.fit_transform(__train_dataset[_col].values.reshape(-1,1)) [:,0]
if _col in __test_dataset:
__test_dataset[_col] = __simple_imputer.transform(__test_dataset[_col].astype(__train_dataset[_col].dtypes ).values.reshape(-1,1)) [:,0]<data_type_conversions>
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
_STRING_COLS_WITH_MISSING_VALUES = ['Cabin', 'Ticket', 'Sex', 'Name', 'Embarked']
for _col in _STRING_COLS_WITH_MISSING_VALUES:
__simple_imputer = SimpleImputer(missing_values=np.nan, strategy='most_frequent')
 __train_dataset[_col] = __simple_imputer.fit_transform(__train_dataset[_col].values.reshape(-1,1)) [:,0]
if _col in __test_dataset:
__test_dataset[_col] = __simple_imputer.transform(__test_dataset[_col].astype(__train_dataset[_col].dtypes ).values.reshape(-1,1)) [:,0]<categorify>
train_file = ".. /input/train.csv"
test_file = ".. /input/test.csv"
output_file = "submission.csv"
_CATEGORICAL_COLS = ['Sex', 'Embarked']
_ohe = OrdinalEncoder(handle_unknown="use_encoded_value", unknown_value=-1)
 __train_dataset[_CATEGORICAL_COLS] = pd.DataFrame(_ohe.fit_transform(__train_dataset[_CATEGORICAL_COLS]), columns=_CATEGORICAL_COLS)
 __test_dataset[_CATEGORICAL_COLS] = pd.DataFrame(_ohe.transform(__test_dataset[_CATEGORICAL_COLS]), columns=_CATEGORICAL_COLS )<feature_engineering>
raw_data = np.loadtxt(train_file, skiprows=1, dtype='int', delimiter=',')
 x_train, x_val, y_train, y_val = train_test_split(
 raw_data[:,1:], raw_data[:,0], test_size=0.1 )
_TEXT_COLUMNS = ['Name', 'Ticket', 'Cabin']
def process_text(__dataset):
for _col in _TEXT_COLUMNS:
process_text = [t.lower() for t in __dataset[_col]]
table = str.maketrans('', '', string.punctuation)
 process_text = [t.translate(table)for t in process_text]
process_text = [re.sub(r'\d+', 'num', t)for t in process_text]
__dataset[_col] = process_text
return __dataset
__train_dataset = process_text(__train_dataset)
 __test_dataset = process_text(__test_dataset )<prepare_x_and_y>
x_train = x_train.astype("float32")/255.
 x_val = x_val.astype("float32")/255 .
__feature_train = __train_dataset.drop(['Survived'], axis=1)
 __target_train =__train_dataset['Survived']
__feature_test = __test_dataset<feature_engineering>
y_train = to_categorical(y_train)
 y_val = to_categorical(y_val)
 print(y_train[0] )
_TEXT_COLUMNS = ['Cabin', 'Ticket', 'Name']
__temp_train_data = __feature_train[_TEXT_COLUMNS]
__feature_train.drop(_TEXT_COLUMNS, axis=1, inplace=True)
 __feature_train_object_array = []
__temp_test_data = __feature_test[_TEXT_COLUMNS]
__feature_test.drop(_TEXT_COLUMNS, axis=1, inplace=True)
 __feature_test_object_array = []
for _col in _TEXT_COLUMNS:
__tfidfvectorizer = TfidfVectorizer(max_features=3000)
 vector_train = __tfidfvectorizer.fit_transform(__temp_train_data[_col])
 __feature_train_object_array.append(vector_train)
 vector_test = __tfidfvectorizer.transform(__temp_test_data[_col])
 __feature_test_object_array.append(vector_test)
 __feature_train = sparse.hstack([__feature_train] + __feature_train_object_array ).tocsr()
__feature_test = sparse.hstack([__feature_test] + __feature_test_object_array ).tocsr()<predict_on_test>
model = Sequential()
model.add(Conv2D(filters = 16, kernel_size =(3, 3), activation='relu',
input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 16, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
__model = RandomForestClassifier()
__model.fit(__feature_train, __target_train)
 __y_pred = __model.predict(__feature_test )<prepare_output>
datagen = ImageDataGenerator(zoom_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1,
rotation_range = 10 )
submission = pd.DataFrame(columns=['PassengerId'], data=__test_dataset_submission_columns)
 submission = pd.concat([submission, pd.DataFrame(__y_pred, columns=['Survived'])], axis=1)
 submission.head()<save_to_csv>
model.compile(loss='categorical_crossentropy', optimizer = Adam(lr=1e-4), metrics=["accuracy"] )
submission.to_csv("kaggle_submission.csv", index=False )<import_modules>
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<load_from_csv>
hist = model.fit_generator(datagen.flow(x_train, y_train, batch_size=16),
steps_per_epoch=500,
epochs=20,
verbose=2,
validation_data=(x_val[:400,:], y_val[:400,:]),
callbacks=[annealer] )
train_dataset = pd.read_csv('.. /input/titanic/train.csv')
 train_dataset.head() 
<load_from_csv>
final_loss, final_acc = model.evaluate(x_val, y_val, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss, final_acc))
test_dataset = pd.read_csv('.. /input/titanic/test.csv')
 test_dataset.head()<prepare_x_and_y>
y_hat = model.predict(x_val)
 y_pred = np.argmax(y_hat, axis=1)
 y_true = np.argmax(y_val, axis=1)
 cm = confusion_matrix(y_true, y_pred)
 print(cm )
X = train_dataset[['Pclass','Sex','Age','Fare','SibSp','Parch', 'Embarked', 'Name', 'Cabin']]
print(X)
 y = train_dataset.iloc[:, 1].values
print(y[0:10])
 X_test = test_dataset[['Pclass','Sex','Age','Fare','SibSp','Parch', 'Embarked','Name','Cabin']]
print(X_test)
<feature_engineering>
mnist_testset = np.loadtxt(test_file, skiprows=1, dtype='int', delimiter=',')
 x_test = mnist_testset.astype("float32")
 x_test = x_test.reshape(-1, 28, 28, 1)/255 .
X['Age'] = X['Age'].fillna(( X['Age'].mean()))
X["Embarked"] = X["Embarked"].fillna("S")
 X.loc[X["Embarked"] == "S", "Embarked"] = 0
X.loc[X["Embarked"] == "C", "Embarked"] = 1
X.loc[X["Embarked"] == "Q", "Embarked"] = 2
X_test['Age'] = X_test['Age'].fillna(( X_test['Age'].mean()))
X_test["Embarked"] = X_test["Embarked"].fillna("S")
 X_test.loc[X_test["Embarked"] == "S", "Embarked"] = 0
X_test.loc[X_test["Embarked"] == "C", "Embarked"] = 1
X_test.loc[X_test["Embarked"] == "Q", "Embarked"] = 2
data = [X, X_test]
titles = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in data:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr','Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 dataset['Title'] = dataset['Title'].map(titles)
 dataset['Title'] = dataset['Title'].fillna(0)
 dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int)
 dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4
dataset['Age'] = dataset['Age'].astype(int)
 dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
dataset['Fare'] = dataset['Fare'].fillna(dataset['Fare'].median())
 dataset['Cabin'] = dataset['Cabin'].str[:1]
X = X.drop(['Name'], axis=1)
 X_test = X_test.drop(['Name'], axis=1)
 print(X)
 print(X_test)
<categorify>
y_hat = model.predict(x_test, batch_size=64 )
cabin_mapping = {"A": 0, "B": 0.4, "C": 0.8, "D": 1.2, "E": 1.6, "F": 2, "G": 2.4, "T": 2.8}
data = [X, X_test]
for dataset in data:
dataset['Cabin'] = dataset['Cabin'].map(cabin_mapping)
 X["Cabin"].fillna(X.groupby("Pclass")["Cabin"].transform("median"), inplace=True)
 X_test["Cabin"].fillna(X_test.groupby("Pclass")["Cabin"].transform("median"), inplace=True )<feature_engineering>
y_pred = np.argmax(y_hat,axis=1 )
<feature_engineering><EOS> 
with open(output_file, 'w')as f :
f.write('ImageId,Label
')
 for i in range(len(y_pred)) :
f.write("".join([str(i+1),',',str(y_pred[i]),'
']))