Kaggle competition: DSNet: fastai Hackathon
<SOS> metric: categorizationaccuracy Kaggle data source: dsnet:-fastai-hackathon<prepare_output>
from fastai.vision import *
from fastai.metrics import accuracy, fbeta
test_final_sub=test_init_df['PassengerId']<feature_engineering>
path = Path(".. /input" )
test_init_df['Salutation']=test_init_df['Name'].apply(checkNameSalutations)
 test_init_df.head()<feature_engineering>
src =(ImageList.from_folder(path/'train')
.split_by_rand_pct() 
.label_from_folder() 
.add_test(Path('.. /input/test/test' ).ls()))
test_init_df["Salutation"] = test_init_df["Salutation"].replace(["Lady","Countess","Capt","Col","Don","Dr","Major","Rev","Sir","Jonkheer","Dona"],"other")
 test_init_df["Salutation"] = [0 if i == "Master" else 1 if i == "Miss" or i == "Ms" or i == "Mlle" or i == "Mrs" else 2 if i == "Mr" else 3 for i in test_init_df["Salutation"]]
test_init_df["Salutation"].head(20 )<count_missing_values>
data =(src.transform(get_transforms() , size=128)
.databunch(path=Path(".. /")) 
.normalize(imagenet_stats))
test_init_df.isnull().sum()<drop_column>
data.show_batch(3 )
test_init_df.drop(['Name'],axis=1,inplace=True )<feature_engineering>
data.batch_stats()
test_init_df['Sex']= test_init_df['Sex'].apply(binaryGender)
 test_init_df.head()<feature_engineering>
arch = models.resnet34
test_init_df['Alpha_ticket']= test_init_df['Ticket'].apply(checkAplphabetInTicket)
 test_init_df.head()<count_values>
learn = cnn_learner(data, arch, metrics=accuracy ).to_fp16()
test_init_df['Alpha_ticket'].value_counts()<drop_column>
lr = 5e-2
test_init_df.drop(labels= ["Ticket"], axis = 1, inplace = True)
 test_init_df.head()<feature_engineering>
learn.fit_one_cycle(6, max_lr=slice(lr))
index_nan_age = list(test_init_df["Age"][test_init_df["Age"].isnull() ].index)
 for i in index_nan_age:
age_pred = test_init_df["Age"][(( test_init_df["SibSp"] == test_init_df.iloc[i]["SibSp"])&(test_init_df["Parch"] == test_init_df.iloc[i]["Parch"])&(test_init_df["Pclass"] == test_init_df.iloc[i]["Pclass"])) ].median()
age_med = test_init_df["Age"].median()
if not np.isnan(age_pred):
test_init_df["Age"].iloc[i] = age_pred
else:
test_init_df["Age"].iloc[i] = age_med<filter>
learn.save('stage-1' )
test_init_df[test_init_df["Fare"].isnull() ]<feature_engineering>
test_probs, _ = learn.get_preds(ds_type=DatasetType.Test)
 test_preds = [data.classes[pred] for pred in np.argmax(test_probs.numpy() , axis=-1)]
test_init_df["Fare"] = test_init_df["Fare"].fillna(np.mean(test_init_df[test_init_df["Pclass"] == 3]["Fare"]))<filter>
fnames = [f.name[:-4] for f in learn.data.test_ds.items]
df = pd.DataFrame({'id':fnames, 'predicted_class':test_preds}, columns=['id', 'predicted_class'])
 df['id'] = df['id'].astype(str)+ '.jpg'
df.to_csv('submission-1.csv', index=False )
test_init_df[test_init_df["Fare"].isnull() ]<categorify>
learn.load('stage-1');
test_init_df = pd.get_dummies(test_init_df,columns = ["Embarked"])
 test_init_df.head()<categorify>
learn.fit_one_cycle(5, max_lr=slice(5e-6, lr/10))
test_init_df = pd.get_dummies(test_init_df,columns = ["Salutation"])
 test_init_df.head()<categorify>
learn.save('stage-2' )
test_init_df = pd.get_dummies(test_init_df,columns = ["Pclass"])
 test_init_df.head()<categorify>
test_probs, _ = learn.get_preds(ds_type=DatasetType.Test)
 test_preds = [data.classes[pred] for pred in np.argmax(test_probs.numpy() , axis=-1)]
test_init_df = pd.get_dummies(test_init_df,columns = ["Alpha_ticket"])
 test_init_df.head()<categorify>
fnames = [f.name[:-4] for f in learn.data.test_ds.items]
df = pd.DataFrame({'id':fnames, 'predicted_class':test_preds}, columns=['id', 'predicted_class'])
 df['id'] = df['id'].astype(str)+ '.jpg'
df.to_csv('submission-2.csv', index=False )
test_init_df["Sex"] = test_init_df["Sex"].astype("category")
 test_init_df = pd.get_dummies(test_init_df, columns = ["Sex"])
 test_init_df.head()<feature_engineering>
learn.load('stage-2');
test_init_df["Fsize"] = test_init_df["SibSp"] + test_init_df["Parch"] + 1
test_init_df.head()<feature_engineering>
data =(src.transform(get_transforms() , size=256)
.databunch(path=Path(".. /")) 
.normalize(imagenet_stats))
test_init_df["family_size"] = [1 if i < 5 else 0 for i in test_init_df["Fsize"]]
test_init_df.head()<categorify>
learn.data = data
learn = learn.to_fp16()
test_init_df = pd.get_dummies(test_init_df, columns= ["family_size"])
 test_init_df.head()<drop_column>
lr = 5e-3
test_init_df.drop(['PassengerId'],axis=1,inplace=True)
 test_init_df.head()<save_to_csv>
learn.fit_one_cycle(5, max_lr=slice(lr))
test_survived = pd.Series(votingC.predict(test_init_df), name = "Survived" ).astype(int)
 results = pd.concat([test_PassengerId, test_survived],axis = 1)
 results.to_csv("titanic.csv", index = False )<load_from_csv>
learn.save('stage-256-1' )
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 train_df.head(5 )<data_type_conversions>
test_probs, _ = learn.get_preds(ds_type=DatasetType.Test)
 test_preds = [data.classes[pred] for pred in np.argmax(test_probs.numpy() , axis=-1)]
train = train_df.values
test = test_df.values
trainX = train[:, 1:].reshape(train.shape[0], 28, 28, 1)
 trainX = trainX.astype(float)
 trainX /= 255.0<categorify>
fnames = [f.name[:-4] for f in learn.data.test_ds.items]
df = pd.DataFrame({'id':fnames, 'predicted_class':test_preds}, columns=['id', 'predicted_class'])
 df['id'] = df['id'].astype(str)+ '.jpg'
df.to_csv('submission-3.csv', index=False )
trainY = kutils.to_categorical(train[:, 0])
 class_num = trainY.shape[1]
print(class_num )<import_modules>
learn.load('stage-256-1');
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D<choose_model_class>
learn.fit_one_cycle(5, max_lr=slice(1e-5, 1e-4))
model = Sequential()
model.add(Conv2D(16,(3, 3), padding='same', activation='relu', input_shape=(28, 28, 1)))
 model.add(Conv2D(32,(5, 5), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(1, 1)))
 model.add(Conv2D(32,(3, 3), padding='same', activation='relu'))
model.add(Conv2D(64,(5, 5), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(1, 1)))
 model.add(Conv2D(64,(3, 3), padding='same', activation='relu'))
model.add(Conv2D(128,(5, 5), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Conv2D(128,(3, 3), padding='same', activation='relu'))
model.add(Conv2D(256,(5, 5), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Flatten())
 model.add(Dense(128, activation='sigmoid'))
model.add(Dense(class_num, activation='softmax'))
model.compile(loss="categorical_crossentropy", optimizer="sgd", metrics=["accuracy"] )<train_model>
learn.save('stage-256-2' )
model.fit(trainX, trainY, batch_size=64, epochs=100, verbose=2 )<save_to_csv>
test_probs, _ = learn.get_preds(ds_type=DatasetType.Test)
 test_preds = [data.classes[pred] for pred in np.argmax(test_probs.numpy() , axis=-1)]
testX = test.reshape(test.shape[0], 28, 28, 1)
 testX = testX.astype(float)
 testX /= 255.0
yPred = model.predict_classes(testX)
 np.savetxt('mnist-cnn.csv', np.c_[range(1,len(yPred)+1),yPred], delimiter=',', header = 'ImageId,Label', comments = '', fmt='%d' )<load_from_csv>
fnames = [f.name[:-4] for f in learn.data.test_ds.items]
df = pd.DataFrame({'id':fnames, 'predicted_class':test_preds}, columns=['id', 'predicted_class'])
 df['id'] = df['id'].astype(str)+ '.jpg'
df.to_csv('submission-4.csv', index=False )
df = pd.read_csv('.. /input/train.csv')
 df.head()<data_type_conversions>
preds,y,losses = learn.get_preds(with_loss=True)
 interp = ClassificationInterpretation(learn.to_fp32() , preds, y, losses )
y = to_categorical(y ).astype("uint8")
 print(y.shape )<split>
test_probs, _ = learn.TTA(ds_type=DatasetType.Test)
 test_preds = [data.classes[pred] for pred in np.argmax(test_probs.numpy() , axis=-1)]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
 print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
 del df, X, y<choose_model_class>
fnames = [f.name[:-4] for f in learn.data.test_ds.items]
df = pd.DataFrame({'id':fnames, 'predicted_class':test_preds}, columns=['id', 'predicted_class'])
 df['id'] = df['id'].astype(str)+ '.jpg'
df.to_csv('submission-5.csv', index=False )