Created
December 3, 2019 07:08
-
-
Save fancyerii/538d4033bc94115c561a146b78beeac9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import, division, print_function, unicode_literals | |
import numpy as np | |
import pandas as pd | |
from matplotlib import pyplot as plt | |
# Load dataset. | |
dftrain = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/train.csv') | |
dfeval = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/eval.csv') | |
y_train = dftrain.pop('survived') | |
y_eval = dfeval.pop('survived') | |
import tensorflow as tf | |
tf.random.set_seed(123) | |
print(tf.__version__) | |
CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck', | |
'embark_town', 'alone'] | |
NUMERIC_COLUMNS = ['age', 'fare'] | |
def one_hot_cat_column(feature_name, vocab): | |
return tf.feature_column.indicator_column( | |
tf.feature_column.categorical_column_with_vocabulary_list(feature_name, | |
vocab)) | |
feature_columns = [] | |
for feature_name in CATEGORICAL_COLUMNS: | |
# Need to one-hot encode categorical features. | |
vocabulary = dftrain[feature_name].unique() | |
feature_columns.append(one_hot_cat_column(feature_name, vocabulary)) | |
for feature_name in NUMERIC_COLUMNS: | |
feature_columns.append(tf.feature_column.numeric_column(feature_name, | |
dtype=tf.float32)) | |
# Use entire batch since this is such a small dataset. | |
NUM_EXAMPLES = len(y_train) | |
def make_input_fn(X, y, n_epochs=None, shuffle=True): | |
def input_fn(): | |
dataset = tf.data.Dataset.from_tensor_slices((dict(X), y)) | |
if shuffle: | |
dataset = dataset.shuffle(NUM_EXAMPLES) | |
# For training, cycle thru dataset as many times as need (n_epochs=None). | |
dataset = dataset.repeat(n_epochs) | |
# In memory training doesn't use batching. | |
dataset = dataset.batch(NUM_EXAMPLES) | |
return dataset | |
return input_fn | |
# Training and evaluation input functions. | |
train_input_fn = make_input_fn(dftrain, y_train) | |
eval_input_fn = make_input_fn(dfeval, y_eval, shuffle=False, n_epochs=1) | |
linear_est = tf.estimator.LinearClassifier(feature_columns) | |
# Train model. | |
linear_est.train(train_input_fn, max_steps=100) | |
# Evaluation. | |
result = linear_est.evaluate(eval_input_fn) | |
print(pd.Series(result)) | |
# Since data fits into memory, use entire dataset per layer. It will be faster. | |
# Above one batch is defined as the entire dataset. | |
n_batches = 1 | |
est = tf.estimator.BoostedTreesClassifier(feature_columns, | |
n_batches_per_layer=n_batches) | |
# The model will stop training once the specified number of trees is built, not | |
# based on the number of steps. | |
est.train(train_input_fn, max_steps=100) | |
# Eval. | |
result = est.evaluate(eval_input_fn) | |
print(pd.Series(result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment