Kaggle competition: Titanic - Machine Learning from Disaster
r2 = pd.DataFrame({'codigo_mun': test['codigo_mun'], 'nota_mat': preds[1]})
 r2.to_csv('r2.csv', index=False )<import_modules>
train_data = pd.read_csv(".. /input/titanic/train.csv")
 train_data.head()
import lightgbm as lgb<compute_train_metric>
missing_val_count_by_column =(train_data.isnull().sum())
 print(missing_val_count_by_column[missing_val_count_by_column > 0] )
def cv(df, test, feats, y_name, k=5):
preds, score, fis = [], [], []
chunk = df.shape[0] // k
for i in range(k):
if i+1 < k:
valid = df.iloc[i*chunk:(i+1)*chunk]
train = df.iloc[:i*chunk].append(df.iloc[(i+1)*chunk:])
 else:
valid = df.iloc[i*chunk:]
train = df.iloc[:i*chunk]
rf = lgb.LGBMClassifier(min_child_samples=500)
 rf.fit(train[feats], train[y_name], eval_set=[(train[feats], train['nota_mat']),(valid[feats], valid['nota_mat'])], early_stopping_rounds=50)
 score.append(accuracy_score(valid[y_name], rf.predict(valid[feats])))
 preds.append(rf.predict(test[feats]))
fis.append(rf.feature_importances_)
 print(i, 'OK')
 return score, preds, fis<compute_test_metric>
features = ['Pclass', 'Sex','SibSp','Age','Parch','Fare','Embarked']
X = pd.get_dummies(train_data[features])
 y = train_data["Survived"]
score, preds, fis = cv(df, test, feats, 'nota_mat' )<save_to_csv>
my_imputer = SimpleImputer()
imputed_X = my_imputer.fit_transform(X )
r3 = pd.DataFrame({'codigo_mun': test['codigo_mun'], 'nota_mat': preds[1]})
 r3.to_csv('r3.csv', index=False )<set_options>
train_X, val_X, train_y, val_y = train_test_split(imputed_X,y,random_state=1 )
gc.enable()<set_options>
tree = DecisionTreeClassifier()
tree.fit(train_X, train_y)
 tree.score(val_X,val_y )
tf.compat.v1.disable_eager_execution()<define_variables>
dumb_tree = DecisionTreeClassifier(max_depth=1)
 dumb_tree.fit(train_X, train_y)
 dumb_tree.score(val_X,val_y )
CATEGORY_CNT = 54
BATCH_SIZE = 256
PREDICT_BATCH_SIZE = 256
EPOCHS = 10
STEPS_PER_EPOCH = 3000
VAL_STEPS_PER_EPOCH = 100
WORD2VEC_DIM = 200
FASTTEXT_DIM = 200
EXP_DECAY_COEF = 0.75
VERBOSE = 1
MAXLEN = 300
TRAINABLE_EMBED_SIZE = 75<load_pretrained>
smart_tree = DecisionTreeClassifier(max_depth=4,min_impurity_decrease=.004)
 smart_tree.fit(train_X, train_y)
 smart_tree.score(val_X,val_y )
def save_object(obj, name):
f = open(name, 'wb')
 pickle.dump(obj, f)
 f.close()
def load_object(name):
f = open(name, 'rb')
 obj = pickle.load(f)
 f.close()
return obj<load_pretrained>
def get_score(max_leaf_nodes, train_X, val_X, train_y, val_y):
model = DecisionTreeClassifier(max_leaf_nodes=max_leaf_nodes)
 model.fit(train_X, train_y)
 print("Max leaf nodes:", max_leaf_nodes," \t \t Score:", model.score(val_X,val_y))
return()
for max_leaf_nodes in [2,3,4,5,6,7,8,9]:
get_score(max_leaf_nodes, train_X, val_X, train_y, val_y )
b_cat_map = load_object(".. /input/bonus-task-pickled-data-1/b_cat_map.pkl")
 f_cat_map = load_object(".. /input/bonus-task-pickled-data-1/f_cat_map.pkl")
 embed_fasttext = load_object(".. /input/bonus-task-pickled-data-1/embed_fasttext.pkl")
 embed_word2vec = load_object(".. /input/bonus-task-pickled-data-1/embed_word2vec.pkl")
 itemid = load_object(".. /input/bonus-task-pickled-data-1/itemid.pkl")
 y_train = load_object(".. /input/bonus-task-pickled-data-1/y_train.pkl")
 word_index = load_object(".. /input/bonus-x/word_index.pkl" )<categorify>
test_data = pd.read_csv(".. /input/titanic/test.csv")
 X_test=pd.get_dummies(test_data[features])
 imputed_test=my_imputer.fit_transform(X_test )
def build_emb_matrix(word_index, embeddings_index, dim):
embedding_matrix = np.zeros(( len(word_index)+ 1, dim + 1), dtype=np.float32)
 embedding_matrix[0, dim] = 1
for word, i in tqdm(word_index.items() , disable=not(VERBOSE)) :
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[i, :dim] = embedding_vector
continue
return embedding_matrix<compute_train_metric>
final_tree = DecisionTreeClassifier(max_depth=4,min_impurity_decrease=.004)
 final_tree.fit(imputed_X,y )
%%time
embedding_matrix = np.concatenate([build_emb_matrix(word_index, embed_fasttext, FASTTEXT_DIM),
build_emb_matrix(word_index, embed_word2vec, WORD2VEC_DIM)], -1)
 del embed_fasttext, embed_word2vec
_ = gc.collect()
print("Embeddings memory usage", sys.getsizeof(embedding_matrix)/(1024*1024), "MB" )<load_from_csv>
predictions=final_tree.predict(imputed_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )