Kaggle competition: Titanic - Machine Learning from Disaster
train = df[~df[target].isna() ]
test = df[df[target].isna() ]<feature_engineering>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )
def to_weighted_count_vec(x, word_sets):
v = np.zeros(21)
 for w in x:
hits = []
for i in range(21):
if w in word_sets[i]:
hits.append(i)
 for i in hits:
v[i] += 1.0 / len(hits)
 return v
weighted_count_cols = [f"weighted_count_vec{k}" for k in range(21)]<create_dataframe>
women = train_data.loc[train_data.Sex == 'female']['Survived']
print('Women survived',sum(women)/len(women))
men = train_data.loc[train_data.Sex == 'male']['Survived']
print('Men survived',sum(men)/len(men))
train_weighted_count = pd.DataFrame(index=train.index, columns=weighted_count_cols, dtype=np.float32)
 for i_fold,(train_idx, valid_idx)in enumerate(kfold.split(train, train[target])) :
tr = train.loc[train_idx]
va = train.loc[valid_idx]
word_sets = [set(sum(tr[tr["department_id"] == i]["product_name"], [])) for i in range(21)]
vecs = va["product_name"].apply(lambda x : to_weighted_count_vec(x, word_sets))
vecs = np.vstack(vecs)
 vec_df = pd.DataFrame(vecs, index=va.index, columns=weighted_count_cols)
 train_weighted_count.loc[valid_idx, :] = vec_df
train = pd.concat([train, train_weighted_count], axis=1 )<create_dataframe>
train_data['female'] = pd.get_dummies(train_data['Sex'])['female']
test_data['female'] = pd.get_dummies(test_data['Sex'])['female']
test_weighted_count = pd.DataFrame(index=test.index, columns=weighted_count_cols, dtype=np.float32)
 word_sets = [set(sum(train[train["department_id"] == i]["product_name"], [])) for i in range(21)]
vecs = test["product_name"].apply(lambda x : to_weighted_count_vec(x, word_sets))
vecs = np.vstack(vecs)
 test_weighted_count.loc[:, :] = pd.DataFrame(vecs, index=test.index, columns=weighted_count_cols)
 test = pd.concat([test, test_weighted_count], axis=1 )<define_variables>
sum(train_data['Age'].isnull())
 train_data['Age'] = train_data['Age'].fillna(train_data['Age'].mean())
 test_data['Age'] = test_data['Age'].fillna(test_data['Age'].mean() )
features = fasttext_pretrain_cols + weighted_count_cols + ["order_rate", "order_dow_mode", "order_hour_of_day_mode"]<normalization>
high_fare = train_data.loc[train_data.Fare > 100]['Survived']
print('High fare survivors',sum(high_fare)/len(high_fare))
low_fare = train_data.loc[train_data.Fare < 32]['Survived']
print('High fare survivors',sum(low_fare)/len(low_fare))
scaler = preprocessing.StandardScaler()
train[features] = scaler.fit_transform(train[features])
 test[features] = scaler.transform(test[features] )<set_options>
pclass1 = train_data.loc[train_data.Pclass == 1]['Survived']
print('Class1',sum(pclass1)/len(pclass1))
pclass2 = train_data.loc[train_data.Pclass == 2]['Survived']
print('Class2',sum(pclass2)/len(pclass2))
pclass3 = train_data.loc[train_data.Pclass == 3]['Survived']
print('Class3',sum(pclass3)/len(pclass3))
def seed_everything(seed : int)-> NoReturn :
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed)
 seed_everything(1220)
 params = {
'hidden_layers': 1,
'hidden_units': 128,
'hidden_activation': 'relu',
'lr': 1e-4,
'batch_size': 128,
'epochs': 100
}
def nn_model(L : int):

 n_neuron = params['hidden_units']
inputs = layers.Input(shape=(L,), dtype='float32')
 x = layers.Dense(n_neuron, activation=params['hidden_activation'] )(inputs)
 for i in np.arange(params['hidden_layers'] - 1):
x = layers.Dense(n_neuron //(2 *(i+1)) , activation=params['hidden_activation'] )(x)
 out = layers.Dense(21, activation='softmax', name = 'out' )(x)
 model = models.Model(inputs=inputs, outputs=out)
 model.compile(loss="categorical_crossentropy", optimizer=optimizers.Adam(lr=params['lr']))
return model
history = model.fit(train_set['X'], train_set['y'], callbacks=[er, ReduceLR],
epochs=params['epochs'], batch_size=params['batch_size'],
validation_data=(val_set['X'], val_set['y']))
return model, fi 
<prepare_x_and_y>
sum(test_data.Pclass.isna() )
preds_test = []
scores = []
oof = np.zeros(( len(train), 21))
for i_fold,(train_idx, valid_idx)in enumerate(kfold.split(train, train[target])) :
print(f"--------fold {i_fold}-------")
 x_tr = train.loc[train_idx, features]
y_tr = train.loc[train_idx, target]
x_va = train.loc[valid_idx, features]
y_va = train.loc[valid_idx, target]
ohe = preprocessing.OneHotEncoder(sparse=False, categories='auto')
 y_tr_ohe = ohe.fit_transform(y_tr.values.reshape(-1, 1))
y_va_ohe = ohe.transform(y_va.values.reshape(-1, 1))
nn = nn_model(len(features))
er = callbacks.EarlyStopping(patience=8, min_delta=params['lr'], restore_best_weights=True, monitor='val_loss')
 ReduceLR = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, epsilon=params['lr'], mode='min')
 checkpoint_filepath = ''
model_checkpoint_callback = callbacks.ModelCheckpoint(filepath=f'mybestweight_fold{i_fold}.hdf5', save_weights_only=True, monitor='val_loss', save_best_only=True)
 nn.fit(x_tr.values, y_tr_ohe, callbacks=[er, ReduceLR, model_checkpoint_callback], epochs=params['epochs'], batch_size=params['batch_size'],
validation_data=(x_va.values, y_va_ohe))
def predict_proba(x):
return nn.predict(x)
 pred_val = predict_proba(x_va)
 oof[valid_idx] += pred_val
score = {
"logloss" : log_loss(y_va, pred_val),
"f1_micro" : f1_score(y_va, np.argmax(pred_val, axis = 1), average = "micro")}
print(score)
 scores.append(score)
 pred_test = predict_proba(test[features])
 preds_test.append(pred_test )<create_dataframe>
train_data['class1'] = pd.get_dummies(train_data.Pclass)[1]
test_data['class1'] = pd.get_dummies(test_data.Pclass)[1]
train_data['class2'] = pd.get_dummies(train_data.Pclass)[2]
test_data['class2'] = pd.get_dummies(test_data.Pclass)[2]
score_df = pd.DataFrame(scores)
 score_df<save_to_csv>
sum(test_data.SibSp.isna() )
oof_df = pd.DataFrame(oof)
 oof_df.to_csv("oof_nn.csv", index = False )<save_to_csv>
sibs = train_data.loc[train_data.SibSp <= 1]['Survived']
print(sum(sibs)/len(sibs))
train_data['many_sibs'] =(train_data.SibSp > 1)*1
test_data['many_sibs'] =(test_data.SibSp > 1)*1
for i in range(len(preds_test)) :
pred_df = pd.DataFrame(preds_test[i])
 pred_df.to_csv(f"pred_{i}_nn.csv", index = False )<prepare_output>
young = train_data.loc[train_data.Age <= 15]['Survived']
print(sum(young)/len(young))
old = train_data.loc[train_data.Age >=40]['Survived']
print(sum(old)/len(old))
pred_test_final = np.array(preds_test ).mean(axis = 0)
 pred_test_final = np.argmax(pred_test_final, axis = 1 )<save_to_csv>
bins = [0.42, 15, 30, 50,80]
train_data['bin_age'] = pd.cut(x=train_data.Age, bins=bins)
 test_data['bin_age'] = pd.cut(x=test_data.Age, bins=bins )
sub["department_id"] = pred_test_final
sub.to_csv("submission_nn.csv", index = False)
 sub.head()<set_options>
train_data['young'] = pd.get_dummies(train_data.bin_age ).iloc[:,0]
test_data['young'] = pd.get_dummies(test_data.bin_age ).iloc[:,0]
train_data['senior'] = pd.get_dummies(train_data.bin_age ).iloc[:,3]
test_data['senior'] = pd.get_dummies(test_data.bin_age ).iloc[:,3]
%matplotlib inline
warnings.simplefilter(action="ignore", category=FutureWarning )<load_pretrained>
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
shutil.copyfile(src=".. /input/redcarpet.py", dst=".. /working/redcarpet.py")
<load_pretrained>
X = train_data[features]
y = train_data.Survived
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 0 )
item_file = ".. /input/talent.pkl"
item_records, COLUMN_LABELS, READABLE_LABELS, ATTRIBUTES = pickle.load(open(item_file, "rb"))
item_df = pd.DataFrame(item_records)[ATTRIBUTES + COLUMN_LABELS].fillna(value=0)
 ITEM_NAMES = item_df["name"].values
ITEM_IDS = item_df["id"].values
s_items = mat_to_sets(item_df[COLUMN_LABELS].values)
 assert len(item_df)== len(s_items), "Item matrix is not the same length as item category set list."
print("Talent:", len(item_df))
print("Categories:", len(COLUMN_LABELS))
item_df.head()<load_pretrained>
log_reg = LogisticRegression()
log_reg.fit(X_train, y_train)
 y_pred = log_reg.predict(X_test)
 y_pred
csr_train, csr_test, csr_input, csr_hidden = pickle.load(open(".. /input/train_test_mat.pkl", "rb"))
m_split = [np.array(csr.todense())for csr in [csr_train, csr_test, csr_input, csr_hidden]]
m_train, m_test, m_input, m_hidden = m_split
s_train, s_test, s_input, s_hidden = pickle.load(open(".. /input/train_test_set.pkl", "rb"))
assert len(m_train)== len(s_train), "Train matrix is not the same length as train sets."
assert len(m_test)== len(s_test), "Test matrix is not the same length as test sets."
assert len(m_input)== len(s_input), "Input matrix is not the same length as input sets."
assert len(m_hidden)== len(s_hidden), "Hidden matrix is not the same length as hidden sets."
print("Train Users", len(m_train))
print("Test Users", len(m_test))
print("Minimum Test Items per User:", min(m_test.sum(axis=1)))
 print("Minimum Input Items per User:", min(m_input.sum(axis=1)))
 print("Minimum Hidden Items per User:", min(m_hidden.sum(axis=1)))
 like_df = pd.DataFrame(m_train, columns=ITEM_NAMES)
 like_df.head()<import_modules>
accuracy_score(y_pred, y_test )
from redcarpet import mapk_score, uhr_score<import_modules>
confusion_matrix(y_pred, y_test )
from redcarpet import jaccard_sim, cosine_sim<import_modules>
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X_train, y_train)
 y_pred = model.predict(X_test )
from redcarpet import collaborative_filter, content_filter, weighted_hybrid<feature_engineering>
accuracy_score(y_pred, y_test )
collab_jac10 = collaborative_filter(s_train, s_input, j=30, sim_fn=jaccard_sim, threshold=0.05, k=10 )<import_modules>
confusion_matrix(y_pred, y_test )
from redcarpet import get_recs
from redcarpet import show_user_recs, show_item_recs, show_user_detail
from redcarpet import show_apk_dist, show_hit_dist, show_score_dist<define_variables>
test_data.Fare = test_data.Fare.fillna(test_data.Fare.mean() )
k_top = 10<compute_train_metric>
param_grid = {
'n_estimators': [200, 500],
'max_features': ['auto', 'sqrt', 'log2'],
'max_depth' : [4,5,6,7,8],
'criterion' :['gini', 'entropy']
}
print("Model: Collaborative Filtering with Jacccard Similarity(j=10)")
 collab_jac10 = collaborative_filter(s_train, s_input, sim_fn=jaccard_sim, j=50, threshold=0.005, k=k_top)
 print("MAP = {0:.3f}".format(mapk_score(s_hidden, get_recs(collab_jac10), k=k_top)))
 print("UHR = {0:.3f}".format(uhr_score(s_hidden, get_recs(collab_jac10), k=k_top)) )<compute_train_metric>
rfc1=RandomForestClassifier(random_state=42, max_features='log2', n_estimators= 200, max_depth=6, criterion='entropy')
 rfc1.fit(X_train, y_train )
<compute_test_metric><EOS> 
predictions = rfc1.predict(test_data[features])
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )