Kaggle competition: Titanic - Machine Learning from Disaster
df = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/train.csv", index_col="id")
 df_test = pd.read_csv("/kaggle/input/cat-in-the-dat-ii/test.csv", index_col="id")
 y = df["target"]
D = df.drop(columns="target")
 features = D.columns
test_ids = df_test.index
D_all = pd.concat([D, df_test])
 num_train = len(D)
 print(f"D_all.shape = {D_all.shape}" )<categorify>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
ord_maps = {
"ord_0": {val: i for i, val in enumerate([1, 2, 3])},
"ord_1": {
val: i
for i, val in enumerate(
 ["Novice", "Contributor", "Expert", "Master", "Grandmaster"] 
)
 },
"ord_2": {
val: i
for i, val in enumerate(
 ["Freezing", "Cold", "Warm", "Hot", "Boiling Hot", "Lava Hot"] 
)
 },
**{col: {val: i for i, val in enumerate(sorted(D_all[col].dropna().unique())) } for col in ["ord_3", "ord_4", "ord_5", "day", "month"]},
}<categorify>
avg_mean = train_data["Age"].astype('float' ).mean(axis=0 )
oh_cols = D_all.columns.difference(ord_maps.keys() - {"day", "month"})
 print(f"OneHot encoding {len(oh_cols)} columns")
 one_hot = pd.get_dummies(
 D_all[oh_cols],
columns=oh_cols,
drop_first=True,
dummy_na=True,
sparse=True,
dtype="int8",
).sparse.to_coo()<data_type_conversions>
train_data.drop("Cabin", axis = 1, inplace=True )
ord_cols = pd.concat([D_all[col].map(ord_map ).fillna(max(ord_map.values())//2 ).astype("float32")for col, ord_map in ord_maps.items() ], axis=1)
 ord_cols /= ord_cols.max()
ord_cols_sqr = 4*(ord_cols - 0.5)**2<split>
train_data.drop("Name", axis = 1, inplace=True )
X = scipy.sparse.hstack([one_hot, ord_cols, ord_cols_sqr] ).tocsr()
print(f"X.shape = {X.shape}")
 X_train, X_test, y_train, y_test = train_test_split(X[:num_train], y, test_size=0.1, random_state=42, shuffle=False)
 X_train = X_train[:10000]
y_train = y_train[:10000]
X_test = X_test[:2000]
y_test = y_test[:2000]<choose_model_class>
train_data["Embarked"].replace(np.nan, 'S', inplace=True )
log = LogisticRegression(C=0.05, solver="lbfgs", max_iter=5000)
 dtree = DecisionTreeClassifier(random_state=4)
 rtree = RandomForestClassifier(n_estimators=100, random_state=4)
 svm = SVC(random_state=4, probability=True)
 nb = GaussianNB()
gbc = GradientBoostingClassifier()
knn = KNeighborsClassifier(n_neighbors=400)
 adaboost = AdaBoostClassifier(n_estimators=500, learning_rate=0.01, random_state=42,
base_estimator=DecisionTreeClassifier(max_depth=8,
min_samples_leaf=10, random_state=42))
labels = [0,1]
def model_algorithm(clf, X_train, y_train, X_test, y_test, name, labels, features):
clf.fit(X_train, y_train)
 y_probas = clf.predict_proba(X_test)
 y_pred = clf.predict(X_test)
 wandb.init(anonymous='allow', project="kaggle-feature-encoding", name=name, reinit=True)
 wandb.termlog('
Plotting %s.'%name)
 wandb.sklearn.plot_learning_curve(clf, X_train, y_train)
 wandb.termlog('Logged learning curve.')
 wandb.sklearn.plot_confusion_matrix(y_test, y_pred, labels)
 wandb.termlog('Logged confusion matrix.')
 wandb.sklearn.plot_summary_metrics(clf, X=X_train, y=y_train, X_test=X_test, y_test=y_test)
 wandb.termlog('Logged summary metrics.')
 wandb.sklearn.plot_class_proportions(y_train, y_test, labels)
 wandb.termlog('Logged class proportions.')
 if(not isinstance(clf, naive_bayes.MultinomialNB)) :
wandb.sklearn.plot_calibration_curve(clf, X_train, y_train, name)
 wandb.termlog('Logged calibration curve.')
 wandb.sklearn.plot_roc(y_test, y_probas, labels)
 wandb.termlog('Logged roc curve.')
 wandb.sklearn.plot_precision_recall(y_test, y_probas, labels)
 wandb.termlog('Logged precision recall curve.')
 csv_name = "submission_"+name+".csv" 
<compute_test_metric>
train_data["Age"] = train_data["Age"].astype("int" )
model_algorithm(log, X_train, y_train, X_test, y_test, 'LogisticRegression', labels, features )<compute_test_metric>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
model_algorithm(svm, X_train, y_train, X_test, y_test, 'SVM', labels, features )<compute_test_metric>
avg_mean = test_data["Age"].astype('float' ).mean(axis=0)
 test_data["Age"].replace(np.nan, avg_mean, inplace=True)
 test_data["Age"].astype("int")
 Favg_mean = test_data["Fare"].astype('float' ).mean(axis=0)
 test_data["Fare"].replace(np.nan, Favg_mean, inplace=True )
model_algorithm(knn, X_train, y_train, X_test, y_test, 'KNearestNeighbor', labels, features )<compute_test_metric>
parameters = {
"n_estimators":[5,10,50,100,250],
"max_depth":[2,4,8,16,32,None]
}
model_algorithm(adaboost, X_train, y_train, X_test, y_test, 'AdaBoost', labels, features )<compute_test_metric>
lm =RandomForestClassifier(n_estimators=250, max_depth=8, random_state=1)
 y = train_data["Survived"]
features = ["Pclass", "Sex", "Age", "SibSp", "Parch","Fare","Embarked"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 lm.fit(X, train_data['Survived'] )
model_algorithm(gbc, X_train, y_train, X_test, y_test, 'GradientBoosting', labels, features )<compute_test_metric>
predictions = lm.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )