Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
ftrl_model = FTRL(alpha=0.01, beta=0.1, L1=0.00001, L2=1.0, D=X_train.shape[1],
iters=60, inv_link="identity", threads=1)
 ftrl_model.fit(X_train, y_train.reshape(-1))<compute_test_metric>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
y_valid_pred = ftrl_model.predict(X_valid)
 y_valid_pred = y_valid_pred.reshape(-1, 1)
 print("RMSL error on valid set:", rmsle(y_valid, y_valid_pred))
print("MAE on valid set:", mean_absolute_error(y_valid, y_valid_pred))<predict_on_test>
test.head()
ftrl_preds = ftrl_model.predict(X_test )<train_model>

 train = train.drop(["Name", "Ticket", "Cabin"], axis=1)
 test = test.drop(["Name", "Ticket", "Cabin"], axis=1 )
fm_ftrl_model = FM_FTRL(alpha=0.01, beta=0.1, L1=0.00001, L2=0.1, D=X_train.shape[1], alpha_fm=0.01,
L2_fm=0.0, init_fm=0.01, D_fm=200, e_noise=0.0001, iters=18, inv_link="identity", threads=4)
 fm_ftrl_model.fit(X_train, y_train.reshape(-1))<compute_test_metric>
new_data_train = pd.get_dummies(train)
 new_data_test = pd.get_dummies(test )
y_valid_pred = fm_ftrl_model.predict(X_valid)
 y_valid_pred = y_valid_pred.reshape(-1, 1)
 print("RMSL error on valid set:", rmsle(y_valid, y_valid_pred))
print("MAE on valid set:", mean_absolute_error(y_valid, y_valid_pred))<predict_on_test>
new_data_train["Age"].fillna(new_data_train["Age"].mean() , inplace=True)
 new_data_test["Age"].fillna(new_data_test["Age"].mean() , inplace=True )
ft_ftrl_preds = fm_ftrl_model.predict(X_test )<train_model>
new_data_test["Fare"].fillna(new_data_test["Fare"].mean() , inplace=True )
lgb_label = y_train.ravel()
lgb_y_valid = y_valid.ravel()
lgb_train = lgb.Dataset(X_train, label=lgb_label)
 lgb_eval = lgb.Dataset(X_valid, lgb_y_valid, reference=lgb_train)
 params = {
'task': 'train',
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': {'l2', 'rmse'},
'learning_rate': 0.6,
'feature_fraction': 0.65,
'bagging_fraction': 0.6,
'bagging_freq': 5,
'verbose': 1,
'data_random_seed': 42,
'max_depth': 3,
'nthread': 4,
'min_data_in_leaf': 100,
'max_bin': 31
}
print('Start training...')
 gbm = lgb.train(params,
lgb_train,
num_boost_round=5500,
valid_sets=lgb_eval,
early_stopping_rounds=1000 )<feature_engineering>
X = new_data_train.drop("Survived", axis=1)
 y = new_data_train["Survived"]
<categorify>
tree = DecisionTreeClassifier(max_depth = 10, random_state = 0)
 tree.fit(X, y )
<prepare_x_and_y>
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
<save_to_csv>
Xtest = new_data_test
Xtest.head()
lgbm_preds = gbm.predict(X_test, num_iteration=gbm.best_iteration)
 lgbm_preds = lgbm_preds.reshape(-1, 1)
 ftrl_preds = ftrl_preds.reshape(-1, 1)
 ft_ftrl_preds = ft_ftrl_preds.reshape(-1, 1)
 preds = ridge_preds*0.05 + lgbm_preds*0.06 + ftrl_preds*0.25 + ft_ftrl_preds*0.64
data['test']["price"] = np.expm1(preds)
 data['test'][["test_id", "price"]].to_csv("submission_ridge_FT_FTRL_LGBM.csv", index = False )<import_modules>
Xtrain, Xvalidation, Ytrain, Yvalidation = train_test_split(X, y, test_size=0.2, random_state=True )
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Ridge
from sklearn.pipeline import FeatureUnion
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Input, Dropout, Dense, concatenate, GRU, Embedding, Flatten, Activation
from keras.optimizers import Adam
from keras.models import Model
from keras import backend as K<compute_test_metric>
model = RandomForestClassifier(n_estimators=100,
max_leaf_nodes=12,
max_depth=12,
random_state=0)
 model.fit(Xtrain, Ytrain)
def rmsle(Y, Y_pred):
assert Y.shape == Y_pred.shape
return np.sqrt(np.mean(np.square(Y_pred - Y)) )<load_from_csv>
Yprediction = model.predict(Xvalidation)
 accuracy_score(Yvalidation, Yprediction )
%%time
train_df = pd.read_table('.. /input/train.tsv')
 test_df = pd.read_table('.. /input/test.tsv')
 print(train_df.shape, test_df.shape )<categorify>
submission = pd.DataFrame()
submission["PassengerId"] = Xtest["PassengerId"]
submission["Survived"] = model.predict(Xtest)
 submission.to_csv("submission.csv", index=False )