In [1]:
# code from http://daeson.tistory.com/267

In [2]:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import pandas as pd
import tensorflow as tf

tf.logging.set_verbosity(tf.logging.INFO)

COLUMNS = ["crim", "zn", "indus", "nox", "rm", "age",
           "dis", "tax", "ptratio", "medv"]
FEATURES = ["crim", "zn", "indus", "nox", "rm",
            "age", "dis", "tax", "ptratio"]
LABEL = "medv"


def input_fn(data_set):
  feature_cols = {k: tf.constant(data_set[k].values) for k in FEATURES}
  labels = tf.constant(data_set[LABEL].values)
  return feature_cols, labels


/home/voyageth/develop/anaconda3/envs/kaggle/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
  return f(*args, **kwds)

In [5]:
# Load datasets
training_set = pd.read_csv("input/boston_train.csv", skipinitialspace=True,
                         skiprows=1, names=COLUMNS)
test_set = pd.read_csv("input/boston_test.csv", skipinitialspace=True,
                     skiprows=1, names=COLUMNS)

# Set of 6 examples for which to predict median house values
prediction_set = pd.read_csv("input/boston_predict.csv", skipinitialspace=True,
                           skiprows=1, names=COLUMNS)

In [7]:
training_set.head()


Out[7]:
crim zn indus nox rm age dis tax ptratio medv
0 2.30040 0.0 19.58 0.605 6.319 96.1 2.1000 403 14.7 23.8
1 13.35980 0.0 18.10 0.693 5.887 94.7 1.7821 666 20.2 12.7
2 0.12744 0.0 6.91 0.448 6.770 2.9 5.7209 233 17.9 26.6
3 0.15876 0.0 10.81 0.413 5.961 17.5 5.2873 305 19.2 21.7
4 0.03768 80.0 1.52 0.404 7.274 38.3 7.3090 329 12.6 34.6

In [4]:
# Feature cols
feature_cols = [tf.contrib.layers.real_valued_column(k)
              for k in FEATURES]

# Build 2 layer fully connected DNN with 10, 10 units respectively.
regressor = tf.contrib.learn.DNNRegressor(
  feature_columns=feature_cols, hidden_units=[10, 10])

# Fit
regressor.fit(input_fn=lambda: input_fn(training_set), steps=5000)

# Score accuracy
ev = regressor.evaluate(input_fn=lambda: input_fn(test_set), steps=1)
loss_score = ev["loss"]
print("Loss: {0:f}".format(loss_score))

# Print out predictions
y = regressor.predict(input_fn=lambda: input_fn(prediction_set))
print ("Predictions: {}".format(str(y)))


INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpphi25plp
INFO:tensorflow:Using config: {'_task_type': None, '_task_id': 0, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7fccda1b4908>, '_master': '', '_num_ps_replicas': 0, '_num_worker_replicas': 0, '_environment': 'local', '_is_chief': True, '_evaluation_master': '', '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1.0
}
, '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_secs': 600, '_log_step_count_steps': 100, '_session_config': None, '_save_checkpoints_steps': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_model_dir': '/tmp/tmpphi25plp'}
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into /tmp/tmpphi25plp/model.ckpt.
INFO:tensorflow:loss = 3059.38, step = 1
INFO:tensorflow:global_step/sec: 862.502
INFO:tensorflow:loss = 85.1771, step = 101 (0.117 sec)
INFO:tensorflow:global_step/sec: 1001.47
INFO:tensorflow:loss = 79.67, step = 201 (0.100 sec)
INFO:tensorflow:global_step/sec: 1008.16
INFO:tensorflow:loss = 75.9857, step = 301 (0.099 sec)
INFO:tensorflow:global_step/sec: 978.345
INFO:tensorflow:loss = 72.6211, step = 401 (0.102 sec)
INFO:tensorflow:global_step/sec: 957.916
INFO:tensorflow:loss = 68.7145, step = 501 (0.104 sec)
INFO:tensorflow:global_step/sec: 975.88
INFO:tensorflow:loss = 65.4087, step = 601 (0.102 sec)
INFO:tensorflow:global_step/sec: 996.655
INFO:tensorflow:loss = 62.4564, step = 701 (0.100 sec)
INFO:tensorflow:global_step/sec: 849.452
INFO:tensorflow:loss = 59.126, step = 801 (0.118 sec)
INFO:tensorflow:global_step/sec: 842.775
INFO:tensorflow:loss = 57.1011, step = 901 (0.119 sec)
INFO:tensorflow:global_step/sec: 878
INFO:tensorflow:loss = 55.0949, step = 1001 (0.114 sec)
INFO:tensorflow:global_step/sec: 938.604
INFO:tensorflow:loss = 52.1141, step = 1101 (0.106 sec)
INFO:tensorflow:global_step/sec: 989.771
INFO:tensorflow:loss = 50.7771, step = 1201 (0.101 sec)
INFO:tensorflow:global_step/sec: 949.346
INFO:tensorflow:loss = 48.9888, step = 1301 (0.105 sec)
INFO:tensorflow:global_step/sec: 972.527
INFO:tensorflow:loss = 45.7959, step = 1401 (0.103 sec)
INFO:tensorflow:global_step/sec: 964.412
INFO:tensorflow:loss = 45.54, step = 1501 (0.103 sec)
INFO:tensorflow:global_step/sec: 988.901
INFO:tensorflow:loss = 43.4527, step = 1601 (0.102 sec)
INFO:tensorflow:global_step/sec: 967.236
INFO:tensorflow:loss = 42.8149, step = 1701 (0.103 sec)
INFO:tensorflow:global_step/sec: 924.193
INFO:tensorflow:loss = 41.1032, step = 1801 (0.108 sec)
INFO:tensorflow:global_step/sec: 970.515
INFO:tensorflow:loss = 41.5101, step = 1901 (0.103 sec)
INFO:tensorflow:global_step/sec: 946.949
INFO:tensorflow:loss = 39.0748, step = 2001 (0.106 sec)
INFO:tensorflow:global_step/sec: 970.681
INFO:tensorflow:loss = 39.3719, step = 2101 (0.103 sec)
INFO:tensorflow:global_step/sec: 966.358
INFO:tensorflow:loss = 38.3247, step = 2201 (0.103 sec)
INFO:tensorflow:global_step/sec: 972.567
INFO:tensorflow:loss = 37.4803, step = 2301 (0.103 sec)
INFO:tensorflow:global_step/sec: 932.264
INFO:tensorflow:loss = 37.0188, step = 2401 (0.107 sec)
INFO:tensorflow:global_step/sec: 1029.72
INFO:tensorflow:loss = 36.3153, step = 2501 (0.097 sec)
INFO:tensorflow:global_step/sec: 969.703
INFO:tensorflow:loss = 35.5511, step = 2601 (0.104 sec)
INFO:tensorflow:global_step/sec: 917.644
INFO:tensorflow:loss = 35.4734, step = 2701 (0.108 sec)
INFO:tensorflow:global_step/sec: 962.977
INFO:tensorflow:loss = 35.7221, step = 2801 (0.104 sec)
INFO:tensorflow:global_step/sec: 952.658
INFO:tensorflow:loss = 34.1604, step = 2901 (0.105 sec)
INFO:tensorflow:global_step/sec: 935.306
INFO:tensorflow:loss = 33.9006, step = 3001 (0.107 sec)
INFO:tensorflow:global_step/sec: 944.684
INFO:tensorflow:loss = 33.7984, step = 3101 (0.106 sec)
INFO:tensorflow:global_step/sec: 971.086
INFO:tensorflow:loss = 34.3093, step = 3201 (0.103 sec)
INFO:tensorflow:global_step/sec: 872.993
INFO:tensorflow:loss = 33.5976, step = 3301 (0.115 sec)
INFO:tensorflow:global_step/sec: 813.553
INFO:tensorflow:loss = 33.2365, step = 3401 (0.123 sec)
INFO:tensorflow:global_step/sec: 866.125
INFO:tensorflow:loss = 32.8032, step = 3501 (0.115 sec)
INFO:tensorflow:global_step/sec: 977.053
INFO:tensorflow:loss = 32.6006, step = 3601 (0.102 sec)
INFO:tensorflow:global_step/sec: 983.47
INFO:tensorflow:loss = 32.4609, step = 3701 (0.102 sec)
INFO:tensorflow:global_step/sec: 925.736
INFO:tensorflow:loss = 32.062, step = 3801 (0.110 sec)
INFO:tensorflow:global_step/sec: 887.176
INFO:tensorflow:loss = 31.7894, step = 3901 (0.110 sec)
INFO:tensorflow:global_step/sec: 879.033
INFO:tensorflow:loss = 31.5601, step = 4001 (0.114 sec)
INFO:tensorflow:global_step/sec: 997.214
INFO:tensorflow:loss = 31.3666, step = 4101 (0.100 sec)
INFO:tensorflow:global_step/sec: 991.278
INFO:tensorflow:loss = 31.1811, step = 4201 (0.101 sec)
INFO:tensorflow:global_step/sec: 808.463
INFO:tensorflow:loss = 30.9313, step = 4301 (0.125 sec)
INFO:tensorflow:global_step/sec: 882.109
INFO:tensorflow:loss = 30.7537, step = 4401 (0.113 sec)
INFO:tensorflow:global_step/sec: 972.915
INFO:tensorflow:loss = 30.5951, step = 4501 (0.102 sec)
INFO:tensorflow:global_step/sec: 966.735
INFO:tensorflow:loss = 30.45, step = 4601 (0.103 sec)
INFO:tensorflow:global_step/sec: 819.438
INFO:tensorflow:loss = 30.3194, step = 4701 (0.122 sec)
INFO:tensorflow:global_step/sec: 847.245
INFO:tensorflow:loss = 30.2002, step = 4801 (0.118 sec)
INFO:tensorflow:global_step/sec: 945.766
INFO:tensorflow:loss = 30.0907, step = 4901 (0.106 sec)
INFO:tensorflow:Saving checkpoints for 5000 into /tmp/tmpphi25plp/model.ckpt.
INFO:tensorflow:Loss for final step: 29.9903.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
INFO:tensorflow:Starting evaluation at 2017-11-07-17:05:18
INFO:tensorflow:Restoring parameters from /tmp/tmpphi25plp/model.ckpt-5000
INFO:tensorflow:Evaluation [1/1]
INFO:tensorflow:Finished evaluation at 2017-11-07-17:05:18
INFO:tensorflow:Saving dict for global step 5000: global_step = 5000, loss = 14.1406
Loss: 14.140625
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
INFO:tensorflow:Restoring parameters from /tmp/tmpphi25plp/model.ckpt-5000
Predictions: <generator object DNNRegressor.predict_scores.<locals>.<genexpr> at 0x7fccb0fcc150>

In [ ]: