Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
%matplotlib inline
style = "<style>svg{width: 70% !important; height: 60% !important;} </style>"
HTML(style )
LABEL='volume_in_cans'
FEATURE_COLUMNS = ['vpo_stores_nearby','closest_store_vpo','closest_store_dist','stores_count_nearby',
'population_2018_year_average_total_number','population_2018_year_average_per_mill_of_country',
'purchasing_power_2018_million_euro','purchasing_power_2018_per_mill_of_country','purchasing_power_2018_euro_per_capita',
'purchasing_power_2018_index_country_eq_100']<prepare_x_and_y>
titanic_data = pd.read_csv(".. /input/titanic/train.csv")
 titanic_data_test = pd.read_csv(".. /input/titanic/test.csv" )
def make_train_input_fn(df, num_epochs):
return tf.estimator.inputs.pandas_input_fn(
 x = df[FEATURE_COLUMNS],
y = df[LABEL],
batch_size = 256,
num_epochs = num_epochs,
shuffle = True,
queue_capacity = 1000
)<prepare_x_and_y>
titanic_data.isnull().sum()
def make_eval_input_fn(df):
return tf.estimator.inputs.pandas_input_fn(
 x = df[FEATURE_COLUMNS],
y = df[LABEL],
batch_size = 256,
shuffle = False,
queue_capacity = 1000
)<prepare_x_and_y>
train_X = titanic_data.drop(['PassengerId', 'Survived', 'Name', 'Ticket', 'Cabin'], axis = 1)
 test_X = titanic_data_test.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis = 1)
 train_y = titanic_data.Survived
def make_prediction_input_fn(df):
return tf.estimator.inputs.pandas_input_fn(
 x = df[FEATURE_COLUMNS],
y = None,
batch_size = 128,
shuffle = False,
queue_capacity = 1000
)<define_variables>
train_X = train_X.fillna({'Age': train_X.Age.median() , 'Fare': train_X.Fare.median() })
 test_X = test_X.fillna({'Age': test_X.Age.median() , 'Fare': test_X.Fare.median() } )
def make_feature_cols() :
input_columns = [tf.feature_column.numeric_column(k)for k in FEATURE_COLUMNS]
return input_columns<import_modules>
train_X = pd.get_dummies(train_X)
 test_X = pd.get_dummies(test_X )
import shutil<train_model>
test_X.isnull().sum()
tf.logging.set_verbosity(tf.logging.INFO)
 OUTDIR = './test_model'
shutil.rmtree(OUTDIR, ignore_errors = True)
 model = tf.estimator.LinearRegressor(
 feature_columns = make_feature_cols() , model_dir = OUTDIR)
 model.train(input_fn = make_train_input_fn(train_dataset_full, num_epochs = 1))<compute_test_metric>
clf_rf = RandomForestClassifier(n_jobs=-1,
criterion='entropy',
min_samples_split=2,
min_samples_leaf=1)
 parameters = {'n_estimators': range(3,40), 'max_depth': range(1,10)}
grid_search_cv_clf = GridSearchCV(clf_rf, parameters, cv = 5 )
def print_rmse(model, df):
metrics = model.evaluate(input_fn = make_eval_input_fn(df))
print('RMSE on dataset = {}'.format(np.sqrt(metrics['average_loss'])) )<compute_test_metric>
grid_search_cv_clf.fit(train_X,train_y )
print_rmse(model, train_dataset_full )<predict_on_test>
best_clf = grid_search_cv_clf.best_estimator_
grid_search_cv_clf.best_params_
predictions = model.predict(input_fn = make_prediction_input_fn(test_dataset))
predicted_vol = [item['predictions'][0] for item in predictions]<prepare_output>
best_clf.score(train_X, train_y )
<save_to_csv><EOS> 
test_y = best_clf.predict(test_X)
 test_ID= titanic_data_test.PassengerId
submission = pd.DataFrame({ "PassengerId": test_ID, "Survived": test_y})
 submission.to_csv('./gender_submission.csv', index=False )