Kaggle competition: Titanic - Machine Learning from Disaster
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
!git clone https://gitlab.com/YannBerthelot/kaggle_pystacknet.git
print(os.listdir("kaggle_pystacknet/pystacknet"))
!pip install "kaggle_pystacknet/pystacknet"
train_NAS_large = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_2/train_NAS_large.npy')
 test_NAS_large = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_2/test_NAS_large.npy' )<split>
train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv" )
X_train, X_val, y_train, y_val = train_test_split(train_NAS_large, target, test_size=0.1, random_state=42 )<train_model>
def feature_engineering(df):
df["Cabin"]=df["Cabin"].fillna("C")
 deck = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6, "G": 7, "U": 8}
df['Deck'] = df['Cabin'].map(lambda x: re.compile("([a-zA-Z]+)" ).search(x ).group())
 mean = df["Age"].mean()
std = df["Age"].std()
is_null = df["Age"].isnull().sum()
rand_age = np.random.randint(mean - std, mean + std, size = is_null)
 age_slice = df["Age"].copy()
age_slice[np.isnan(age_slice)] = rand_age
df["Age"] = age_slice
df["Age"] = df["Age"].astype(int)
 df["Embarked"]=df['Embarked'].fillna("S")
 df["Siblings"]=df["SibSp"]+df["Parch"]
df=df.drop(["Name","Ticket","SibSp","Parch","PassengerId","Cabin","Fare"],axis=1)
 return(df)
 train=feature_engineering(train)
 test=feature_engineering(test )
xgb_params = {
'eta': 0.05,
'max_depth': 4,
'subsample': 0.8,
'colsample_bytree': 0.7,
'objective': 'reg:logistic',
'eval_metric': 'auc',
'verbosity': 0,
'tree_method': 'gpu_hist',
'predictor': 'gpu_predictor'
}
dtrain = xgb.DMatrix(X_train, y_train)
 dval = xgb.DMatrix(X_val, y_val)
 dtest = xgb.DMatrix(test_NAS_large )<drop_column>
X=train.drop("Survived",axis=1)
 Y=train["Survived"]
del train_NAS_large, test_NAS_large, X_train, X_val
gc.collect()<train_model>
x, x_test, y, y_test = train_test_split(X, Y, test_size=0.20, random_state=42,shuffle=True)
 X_oh=pd.get_dummies(X)
 test_oh=pd.get_dummies(test)
 test_oh["Deck_T"]=0
%%time
model = xgb.train(xgb_params, dtrain, num_boost_round=110 )<predict_on_test>
models=[
[RandomForestClassifier(n_estimators=100, criterion="entropy", max_depth=5, max_features=0.5, random_state=1),
ExtraTreesClassifier(n_estimators=100, criterion="entropy", max_depth=5, max_features=0.5, random_state=1),
GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=5, max_features=0.5, random_state=1),
LogisticRegression(random_state=1),
LGBMClassifier() ],
[RandomForestClassifier(n_estimators=100, criterion="entropy", max_depth=5, max_features=0.5, random_state=1),
ExtraTreesClassifier(n_estimators=100, criterion="entropy", max_depth=5, max_features=0.5, random_state=1),
GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=5, max_features=0.5, random_state=1),
LogisticRegression(random_state=1),
LGBMClassifier() ],
]
val_preds = model.predict(dval )<compute_test_metric>
model=StackNetClassifier(models, metric="auc", folds=5,
	restacking=False,use_retraining=True, use_proba=True,
	random_state=12345,n_jobs=1, verbose=1)
 model.fit(X_oh,Y)
 output=model.predict_proba(test_oh )
roc_auc_score(y_val, val_preds )<compute_test_metric>
output=pd.DataFrame(output ).rename(index=str, columns={"index": "PassengerId", 0: "Survived"})
 output=output.reset_index()
output=output.rename(columns={"index":"PassengerId"})
 output["PassengerId"]=output["PassengerId"].astype("int")+892
output["Survived"]=(output[output.columns[output.shape[1]-1]]>0.5 ).apply(int)
 output=output[["PassengerId","Survived"]]
log_loss(y_val, val_preds )<predict_on_test>
shutil.rmtree("kaggle_pystacknet" )
%%time
val_shap_preds = model.predict(dval, pred_contribs=True )<predict_on_test>
output.to_csv("results.csv",index=False,header=True )