<jupyter_start><jupyter_text>Performing Regression Estimators API (tf.contrib.learn)<jupyter_code>import tensorflow as tf
import matplotlib.pyplot as plt
import os

import pandas as pd
from sklearn.model_selection import train_test_split
%matplotlib inline<jupyter_output><empty_output><jupyter_text>1. Load and prepare the data<jupyter_code>current_dir = os.getcwd()
dataset_path = os.path.join(os.getcwd(), os.pardir,'data','diamond_prices_original.csv')
diamonds = pd.read_csv(dataset_path)
TARGET = 'price'
## Separating into training and testing: the objects we got back are pandas DataFrames.
X_train, X_val, y_train, y_val = train_test_split(diamonds.drop(TARGET, axis=1),
                                                  diamonds[TARGET],
                                                  train_size=0.9,
                                                  random_state=60)
diamonds.head(10)<jupyter_output><empty_output><jupyter_text>2. Write one or more dataset importing functions<jupyter_code>BATCH_SIZE = 128
N_EPOCHS = 400
## Training input function
input_fn_train = tf.estimator.inputs.pandas_input_fn(
    x=X_train,
    y=y_train,
    batch_size=BATCH_SIZE,
    num_epochs=N_EPOCHS,
    shuffle = True,
    target_column=TARGET)

## Validation input function
input_fn_val = tf.estimator.inputs.pandas_input_fn(
    x=X_val,
    y=y_val,
    num_epochs=1,
    shuffle = False,
    target_column=TARGET)<jupyter_output><empty_output><jupyter_text>3. Define the feature columns  Numerical featuresSince we have only few of them let's define them explicitly. The following cell does the same as:~~~~numerical_cols = ['carat', 'depth', 'table', 'x', 'y', 'z']numeric_feature_cols = [tf.feature_column.numeric_column(col) for col in numerical_cols]~~~~<jupyter_code>carat = tf.feature_column.numeric_column('carat')
depth = tf.feature_column.numeric_column('depth')
table = tf.feature_column.numeric_column('table')
x = tf.feature_column.numeric_column('x')
y = tf.feature_column.numeric_column('y')
z = tf.feature_column.numeric_column('z')

numeric_feature_cols = [carat, depth, table, x, y, z]<jupyter_output><empty_output><jupyter_text>Categorical featuresFrom the documentation:>To feed sparse features into DNN models, wrap the column with `embedding_column` or `indicator_column`. `indicator_column` is recommended for features with only a few possible values. For features with many possible values, to reduce the size of your model, `embedding_column` is recommended.<jupyter_code>categorical_cols = ['cut', 'color', 'clarity']
unique_values = {col: list(diamonds[col].unique()) for col in categorical_cols}
categorical_feature_cols = [tf.feature_column.categorical_column_with_vocabulary_list(col, values) \
                           for col, values in unique_values.items()]

categorical_feature_cols = [tf.feature_column.indicator_column(col) \
                            for col in categorical_feature_cols]<jupyter_output><empty_output><jupyter_text>4. Instantiate the DNNRegressor Estimator<jupyter_code>reg_model = tf.estimator.DNNRegressor(
    feature_columns= numeric_feature_cols + categorical_feature_cols,
    hidden_units=[128, 64, 32],
    model_dir='./high_level_api_regression',
    optimizer='Adam')
# Loss is calculated by using mean squared error.<jupyter_output>INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_model_dir': './high_level_api_regression', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x000001CE92766BA8>, '_task_type': 'worker', '_task_id': 0, '_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}<jupyter_text>5. Train the model<jupyter_code>reg_model.train(input_fn=input_fn_train)<jupyter_output>INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into ./high_level_api_regression\model.ckpt.
INFO:tensorflow:loss = 4.36377e+09, step = 1
INFO:tensorflow:global_step/sec: 267.837
INFO:tensorflow:loss = 9.22142e+07, step = 101 (0.378 sec)
INFO:tensorflow:global_step/sec: 319.34
INFO:tensorflow:loss = 8.09456e+07, step = 201 (0.312 sec)
INFO:tensorflow:global_step/sec: 370.354
INFO:tensorflow:loss = 2.1084e+08, step = 301 (0.266 sec)
INFO:tensorflow:global_step/sec: 375.424
INFO:tensorflow:loss = 1.94623e+08, step = 401 (0.269 sec)
INFO:tensorflow:global_step/sec: 373.248
INFO:tensorflow:loss = 1.07927e+08, step = 501 (0.265 sec)
INFO:tensorflow:global_step/sec: 347.503
INFO:tensorflow:loss = 5.97992e+07, step = 601 (0.288 sec)
INFO:tensorflow:global_step/sec: 357.512
INFO:tensorflow:loss = 1.22129e+08, step = 701 (0.283 sec)
INFO:tensorflow:global_step/sec: 370.16
INFO:tensorflow:loss = 6.96402e+07, step = 801 (0.283 sec)
INFO:tensorflow:global_step/[...]<jupyter_text>6. Visualize/analyze the results of the model<jupyter_code>reg_model.evaluate(input_fn=input_fn_val)
## This returns an iterator
predictions = reg_model.predict(input_fn=input_fn_val)
## Transform the values to a pandas series
predictions = pd.Series([x['predictions'][0] for x in predictions], index=y_val.index)
fig, ax = plt.subplots(figsize=(8,5))
ax.scatter(x=predictions, y=y_val, s=0.5)
ax.set_xlabel('Predicted prices')
ax.set_ylabel('Observed prices')
ax.set_title("Predictions vs. Observed Values in the validation set");
fig, ax = plt.subplots(figsize=(8,5))
ax.hist(abs(y_val-predictions), bins=100, edgecolor='black')
ax.set_xlim(0, 4e3);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1170380768555887, 'fraction_numerical': 0.04316546762589928, 'mean_word_length': 5.658878504672897, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '628187', 'license': 'MIT', 'n_tokens_mistral': 2133, 'n_tokens_neox': 1934, 'n_words': 404, 'path': 'Section3/2_performing_regression_with_estimators.ipynb', 'repo_name': 'PacktPublishing/TensorFlow-1.X-Recipes-for-Supervised-and-Unsupervised-Learning', 'size': '5699'}