Kaggle competition: Titanic - Machine Learning from Disaster
batch_size = 32
history = model.fit(data_train_split, Y_dummies, epochs = 60, batch_size=batch_size, verbose = 2 )<predict_on_test>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
Y_sarcasm = model.predict(data_valid_split,batch_size=1,verbose = 2 )<save_to_csv>
from fastai import *
from fastai.vision import *
from fastai.tabular import *
data_to_submit = pd.DataFrame({
'ID': data_valid['ID'],
'is_sarcastic':list_sarcasm
})
 data_to_submit.to_csv('csv_to_submit.csv', index = False )<set_options>
train = pd.read_csv('.. /input/train.csv')
 train.head()
sns.set()
%matplotlib inline<load_from_csv>
test = pd.read_csv(".. /input/test.csv")
 test.isnull().sum()
df_train = pd.read_csv(os.path.join(_input_path, "train.csv"), index_col=0)
 df_test = pd.read_csv(os.path.join(_input_path, "test.csv"), index_col=0 )<count_values>
train["Embarked"] = train["Embarked"].fillna("C")
 test["Embarked"] = test["Embarked"].fillna("C")
 train['Fare'].fillna(train['Fare'].median() , inplace = True)
 test['Fare'].fillna(test['Fare'].median() , inplace = True)
 train.Cabin.fillna("N", inplace=True)
 test.Cabin.fillna("N", inplace=True )
df_train["Engine"].str[-3:].value_counts()<count_values>
train_title = [i.split(",")[1].split(".")[0].strip() for i in train["Name"]]
train["Title"] = pd.Series(train_title)
 train["Title"].head()
test_title = [i.split(",")[1].split(".")[0].strip() for i in test["Name"]]
test["Title"] = pd.Series(test_title)
 test["Title"].head()
df_test["Engine"].str[-3:].value_counts()<filter>
test_id = test['PassengerId']
df_train[df_train[["Engine", "Power"]].isnull().sum(axis=1)>= 1]<define_variables>
dep_var = 'Survived'
cat_names = ['Title', 'Sex', 'Ticket', 'Cabin', 'Embarked']
cont_names = [ 'Age', 'SibSp', 'Parch', 'Fare']
print("Categorical columns are : ", cat_names)
 print('Continuous numerical columns are :', cont_names)
 procs = [FillMissing, Categorify, Normalize]
drop_origin_cols = []<data_type_conversions>
test = TabularList.from_df(test, cat_names=cat_names, cont_names=cont_names, procs=procs)
 data =(TabularList.from_df(train, path='.', cat_names=cat_names, cont_names=cont_names, procs=procs)
.split_by_idx(list(range(0,200)))
.label_from_df(cols = dep_var)
.add_test(test, label=0)
.databunch() )
df_train["Engine_num"] = df_train["Engine"].str[: -3].astype(np.float16)
 df_test["Engine_num"] = df_test["Engine"].str[: -3].astype(np.float16)
 if "Engine" not in drop_origin_cols: drop_origin_cols.append("Engine" )<data_type_conversions>
data.show_batch(rows=10)
df_train["Mileage_num"] = df_train["Mileage"].apply(lambda x: str(x ).split(" ")[0] ).astype(np.float)
 df_test["Mileage_num"] = df_test["Mileage"].apply(lambda x: str(x ).split(" ")[0] ).astype(np.float)
 if "Mileage" not in drop_origin_cols: drop_origin_cols.append("Mileage" )<count_values>
learn = tabular_learner(data, layers=[200,100], metrics=accuracy, emb_drop=0.1)
df_train["Power"].str[-4: ].value_counts()<data_type_conversions>
learn.lr_find()
df_train["Power_num"] = df_train["Power"].str[: -4].replace({"null": np.nan} ).astype(np.float)
 df_test["Power_num"] = df_test["Power"].str[: -4].replace({"null": np.nan} ).astype(np.float)
 if "Power" not in drop_origin_cols: drop_origin_cols.append("Power" )<drop_column>
learn.fit(15, slice(1e-01))
df_train.drop(drop_origin_cols, axis=1, inplace=True)
 df_test.drop(drop_origin_cols, axis=1, inplace=True )<feature_engineering>
preds, targets = learn.get_preds()
predictions = np.argmax(preds, axis = 1)
 pd.crosstab(predictions, targets)
df_train["maker"] = df_train["Name"].apply(lambda x: x.split(" ")[0])
 df_test["maker"] = df_test["Name"].apply(lambda x: x.split(" ")[0] )<categorify>
predictions, *_ = learn.get_preds(DatasetType.Test)
 labels = np.argmax(predictions, 1 )
<categorify><EOS> 
submission = pd.DataFrame({'PassengerId': test_id, 'Survived': labels})
 submission.to_csv('submission.csv', index=False)
 submission.head()