Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
%matplotlib inline
get_ipython().run_line_magic('config', "InlineBackend.figure_format ='retina'")
 sns.set(style='white', context='notebook', palette='deep')
 sns.set(rc={'figure.figsize':(11.7,8.27)})
my_pipeline.fit(X_cv, y)
 preds = my_pipeline.predict(X_sub )<save_model>
train = pd.read_csv('.. /input/titanic/train.csv', index_col=0)
 test = pd.read_csv('.. /input/titanic/test.csv', index_col=0 )
save_file(preds )<set_options>
train = pd.read_csv('.. /input/titanic/train.csv', index_col=0)
 test = pd.read_csv('.. /input/titanic/test.csv', index_col=0)
 X_train = train.drop('Survived',axis=1 ).copy()
y_train = train['Survived'].copy()
X_test = test.copy()
X = pd.concat([X_train, X_test], axis=0)
 X.fillna(-999, inplace=True)
 X = pd.get_dummies(X ).astype('int')
 X = X.rename(columns = lambda x:re.sub('[^A-Za-z0-9_]+', '', x))
X_train = X.iloc[:len(train)]
X_test = X.iloc[len(train):]
def getModelLGBM() :
model = LGBMClassifier(objective='binary', seed=42)
 return model
def fitModelLGBM(model, X_train, y_train):
model.fit(X_train, y_train)
 return model
model = getModelLGBM()
model.fit(X_train, y_train)
 preds_lgb = model.predict(X_test )
%reload_ext autoreload
%autoreload 2
%matplotlib inline<install_modules>
train = pd.read_csv('.. /input/titanic/train.csv', index_col=0)
 test = pd.read_csv('.. /input/titanic/test.csv', index_col=0)
 X_train = train.drop('Survived',axis=1 ).copy()
y_train = train['Survived'].copy()
X_test = test.copy()
X = pd.concat([X_train, X_test], axis=0)
 X.fillna(-999, inplace=True)
 X = pd.get_dummies(X ).astype('int')
 X = X.rename(columns = lambda x:re.sub('[^A-Za-z0-9_]+', '', x))
X_train = X.iloc[:len(train)]
X_test = X.iloc[len(train):]
def getModelXGB() :
model = XGBClassifier(objective='binary:hinge',
max_depth=20,
n_estimators=2000,
gamma=0,
eta=0.05,
seed=42)
 return model
def fitModelXGB(model, X_train, y_train):
model.fit(X_train, y_train)
 return model
model = getModelXGB()
model.fit(X_train, y_train)
 preds_xgb = model.predict(X_test )
!pip install git+https://github.com/fastai/fastai<import_modules>
train = pd.read_csv('.. /input/titanic/train.csv', index_col=0)
 test = pd.read_csv('.. /input/titanic/test.csv', index_col=0)
 X_train = train.drop('Survived',axis=1 ).copy()
y_train = train['Survived'].copy()
X_test = test.copy()
X_train.fillna(-999, inplace=True)
 X_test.fillna(-999, inplace=True)
 def getModelCat() :
model = CatBoostClassifier(custom_loss=['Accuracy'],
random_seed=42,
logging_level='Silent')
 return model
def fitModelCat(model, X_train, y_train):
categorical_features_indices = np.where(X_train.dtypes != np.float)[0]
model.fit(X_train, y_train,
cat_features=categorical_features_indices,
plot=False);
return model
categorical_features_indices = np.where(X_train.dtypes != np.float)[0]
model = getModelCat()
model.fit(X_train, y_train,
cat_features = categorical_features_indices)
 preds_cat = model.predict(X_test )
from fastai.vision import *<set_options>
skf = StratifiedKFold(n_splits=5)
 scores = np.array([])
 for train_idx, val_idx in skf.split(X_train, y_train):
cv_train_x = X_train.iloc[train_idx]
cv_train_y = y_train.iloc[train_idx]
cv_val_x = X_train.iloc[val_idx]
cv_val_y = y_train.iloc[val_idx]
model = getModelXGB()
model = fitModelXGB(model, cv_train_x, cv_train_y)
 preds = model.predict(cv_val_x)
 score = accuracy_score(cv_val_y, preds)
 scores = np.append(scores, score)
 print("Accuracy: %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
fastai.utils.collect_env.show_install(1)
<define_variables>
train = pd.read_csv('.. /input/titanic/train.csv', index_col=0)
 test = pd.read_csv('.. /input/titanic/test.csv', index_col=0)
 X = pd.concat([train.copy() , test.copy() ],axis=0)
 X = X.drop(['Survived','Name', 'Ticket', 'Cabin'], axis=1)
 X['Pclass'] = X['Pclass'] * -1
X['Age'] = X['Age'].fillna(X['Age'].median())
 X['Fare'] = X['Fare'].fillna(X['Fare'].median())
 X['Embarked'] = X['Embarked'].fillna(X['Embarked'].mode() [0])
 X['Fare'] = np.log1p(X['Fare'])
 X['Age'] = pd.cut(X['Age'], bins=10)
 X = pd.get_dummies(X)
 X_train = X.iloc[:len(train)]
X_test = X.iloc[len(train):]
def getModelSVM() :
model = make_pipeline(StandardScaler() ,
SGDClassifier(loss='hinge',max_iter=1000, tol=1e-3, random_state=1305))
return model
def fitModelSVM(model, X_train, y_train):
model.fit(X_train, y_train)
 return model
model = getModelSVM()
model = fitModelSVM(model, X_train, y_train)
 preds_svm = model.predict(X_test)
 X_test['Survived'] = preds_svm
path = Path('/kaggle/input/')
 path.ls()<load_from_csv>
train = pd.read_csv('.. /input/titanic/train.csv', index_col=0)
 test = pd.read_csv('.. /input/titanic/test.csv', index_col=0)
 X = pd.concat([train.copy() , test.copy() ],axis=0)
 X = X.drop(['Survived','Name', 'Ticket', 'Cabin'], axis=1)
 X['Pclass'] = X['Pclass'] * -1
X['Age'] = X['Age'].fillna(X['Age'].median())
 X['Fare'] = X['Fare'].fillna(X['Fare'].median())
 X['Embarked'] = X['Embarked'].fillna(X['Embarked'].mode() [0])
 X['Fare'] = np.log1p(X['Fare'])
 X['Age'] = pd.cut(X['Age'], bins=10)
 X = pd.get_dummies(X)
 X_train = X.iloc[:len(train)]
X_test = X.iloc[len(train):]
transformer = StandardScaler()
X_train = transformer.fit_transform(X_train)
 X_test = transformer.transform(X_test)
 def getModelNN() :
model = tf.keras.models.Sequential([tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')])
 return model
def fitModelNN(model, X_train, y_train):
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
 model.fit(X_train, y_train, epochs=50)
 return model
model = getModelNN()
model = fitModelNN(model, X_train, y_train)
 preds_nn = model.predict(X_test)
 preds_nn = np.round(preds_nn)
 test['Survived'] = preds_nn
test['Survived'] = test['Survived'].astype('int' )
df = pd.read_csv(path/'train_v2.csv')
 df.head()<feature_engineering>
preds_nn = preds_nn.reshape(418, ).astype('int')
 preds = preds_nn + preds_cat + preds_svm + preds_xgb + preds_lgb
preds = np.round(preds / 5)
 test['Survived'] = preds_xgb.astype('int' )
tfms = get_transforms(flip_vert=True, max_lighting=0.1, max_zoom=1.05, max_warp=0., max_rotate=15.)<load_from_csv>
test['Survived'] = preds_xgb.astype('int' )
np.random.seed(42)
 src =(ImageList.from_csv(path, 'train_v2.csv', folder='train-jpg', suffix='.jpg')
.split_by_rand_pct(0.2)
.label_from_df(label_delim=' '))
data =(src.transform(tfms, size=128)
.databunch(num_workers=0 ).normalize(imagenet_stats))<define_variables>
test['Survived'] = preds_lgb.astype('int' )
data.show_batch(rows=3, figsize=(12,9))<choose_model_class>
submission = test['Survived']
submission.to_csv(f"submission.csv", index=True)
 print(datetime.now().strftime("%y-%m-%d %H:%M:%S")," | ","Saving submission.. " )