In [1]:
%matplotlib notebook 
import numpy as np
import os, string
from matplotlib import pyplot as plt
import scipy as sp
import sklearn as sk
import tensorflow as tf
from tensorflow.contrib.tensor_forest.client import random_forest
from tensorflow.contrib.tensor_forest.python import tensor_forest
from sklearn.datasets import load_iris 
from sklearn.datasets import load_boston
#iris = load_iris()
#data=iris['data']
#target = iris['target']
#print(data.shape)

In [2]:
hparams = tensor_forest.ForestHParams(
        num_trees=3,
        max_nodes=1000,
        num_classes=3,
        num_features=4,
        split_after_samples=20)
classifier = random_forest.TensorForestEstimator(hparams.fill())

iris = load_iris()
data = iris.data.astype(np.float32)
labels = iris.target.astype(np.float32)

classifier.fit(x=data, y=labels, steps=100, batch_size=50)
classifier.evaluate(x=data, y=labels, steps=10)


WARNING:tensorflow:Using temporary folder as model directory: /var/folders/c3/jtwp_prn51zgrxtgn0zm8cp80000gn/T/tmpqwayw5xh
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_task_type': None, '_task_id': 0, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x106bbc978>, '_master': '', '_num_ps_replicas': 0, '_environment': 'local', '_is_chief': True, '_evaluation_master': '', '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
, '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_secs': 600, '_save_checkpoints_steps': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000}
INFO:tensorflow:Constructing forest with params = 
INFO:tensorflow:{'num_trees': 3, 'max_nodes': 1000, 'bagging_fraction': 1.0, 'feature_bagging_fraction': 1.0, 'num_splits_to_consider': 4, 'max_fertile_nodes': 500, 'split_after_samples': 20, 'min_split_samples': 5, 'valid_leaf_threshold': 1, 'dominate_method': 'bootstrap', 'dominate_fraction': 0.99, 'num_classes': 3, 'num_features': 4, 'bagged_num_features': 4, 'bagged_features': None, 'regression': False, 'num_outputs': 1, 'num_output_columns': 4, 'split_initializations_per_input': 1, 'base_random_seed': 0}
INFO:tensorflow:training graph for tree: 0
INFO:tensorflow:training graph for tree: 1
INFO:tensorflow:training graph for tree: 2
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into /var/folders/c3/jtwp_prn51zgrxtgn0zm8cp80000gn/T/tmpqwayw5xh/model.ckpt.
INFO:tensorflow:loss = -0.0, step = 1
INFO:tensorflow:TensorForestLossHook resetting last_step.
INFO:tensorflow:Saving checkpoints for 100 into /var/folders/c3/jtwp_prn51zgrxtgn0zm8cp80000gn/T/tmpqwayw5xh/model.ckpt.
INFO:tensorflow:Loss for final step: -302.667.
INFO:tensorflow:Constructing forest with params = 
INFO:tensorflow:{'num_trees': 3, 'max_nodes': 1000, 'bagging_fraction': 1.0, 'feature_bagging_fraction': 1.0, 'num_splits_to_consider': 4, 'max_fertile_nodes': 500, 'split_after_samples': 20, 'min_split_samples': 5, 'valid_leaf_threshold': 1, 'dominate_method': 'bootstrap', 'dominate_fraction': 0.99, 'num_classes': 3, 'num_features': 4, 'bagged_num_features': 4, 'bagged_features': None, 'regression': False, 'num_outputs': 1, 'num_output_columns': 4, 'split_initializations_per_input': 1, 'base_random_seed': 0}
INFO:tensorflow:training graph for tree: 0
INFO:tensorflow:training graph for tree: 1
INFO:tensorflow:training graph for tree: 2
INFO:tensorflow:Starting evaluation at 2017-03-09-08:45:58
INFO:tensorflow:Evaluation [1/10]
INFO:tensorflow:Finished evaluation at 2017-03-09-08:45:58
INFO:tensorflow:Saving dict for global step 100: global_step = 100, loss = -306.667
WARNING:tensorflow:Skipping summary for global_step, must be a float or np.float32.
Out[2]:
{'global_step': 100, 'loss': -306.66666}

In [3]:
print(classifier.predict(data))


INFO:tensorflow:Constructing forest with params = 
INFO:tensorflow:{'num_trees': 3, 'max_nodes': 1000, 'bagging_fraction': 1.0, 'feature_bagging_fraction': 1.0, 'num_splits_to_consider': 4, 'max_fertile_nodes': 500, 'split_after_samples': 20, 'min_split_samples': 5, 'valid_leaf_threshold': 1, 'dominate_method': 'bootstrap', 'dominate_fraction': 0.99, 'num_classes': 3, 'num_features': 4, 'bagged_num_features': 4, 'bagged_features': None, 'regression': False, 'num_outputs': 1, 'num_output_columns': 4, 'split_initializations_per_input': 1, 'base_random_seed': 0}
INFO:tensorflow:training graph for tree: 0
INFO:tensorflow:training graph for tree: 1
INFO:tensorflow:training graph for tree: 2
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2]

In [4]:
hparams = tensor_forest.ForestHParams(
        num_trees=3,
        max_nodes=1000,
        num_classes=1,
        num_features=13,
        regression=True,
        split_after_samples=20)

regressor = random_forest.TensorForestEstimator(hparams.fill())

boston = load_boston()
data = boston.data.astype(np.float32)
labels = boston.target.astype(np.float32)

regressor.fit(x=data, y=labels, steps=100, batch_size=50)
regressor.evaluate(x=data, y=labels, steps=10)


WARNING:tensorflow:Using temporary folder as model directory: /var/folders/c3/jtwp_prn51zgrxtgn0zm8cp80000gn/T/tmpy8qjm1ni
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_task_type': None, '_task_id': 0, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x11740eef0>, '_master': '', '_num_ps_replicas': 0, '_environment': 'local', '_is_chief': True, '_evaluation_master': '', '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
, '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_secs': 600, '_save_checkpoints_steps': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000}
INFO:tensorflow:Constructing forest with params = 
INFO:tensorflow:{'num_trees': 3, 'max_nodes': 1000, 'bagging_fraction': 1.0, 'feature_bagging_fraction': 1.0, 'num_splits_to_consider': 13, 'max_fertile_nodes': 500, 'split_after_samples': 20, 'min_split_samples': 5, 'valid_leaf_threshold': 1, 'dominate_method': 'bootstrap', 'dominate_fraction': 0.99, 'num_classes': 1, 'num_features': 13, 'regression': True, 'bagged_num_features': 13, 'bagged_features': None, 'num_outputs': 1, 'num_output_columns': 2, 'split_initializations_per_input': 1, 'base_random_seed': 0}
INFO:tensorflow:training graph for tree: 0
INFO:tensorflow:training graph for tree: 1
INFO:tensorflow:training graph for tree: 2
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into /var/folders/c3/jtwp_prn51zgrxtgn0zm8cp80000gn/T/tmpy8qjm1ni/model.ckpt.
INFO:tensorflow:loss = -0.0, step = 1
INFO:tensorflow:TensorForestLossHook resetting last_step.
INFO:tensorflow:Saving checkpoints for 100 into /var/folders/c3/jtwp_prn51zgrxtgn0zm8cp80000gn/T/tmpy8qjm1ni/model.ckpt.
INFO:tensorflow:Loss for final step: -182.667.
INFO:tensorflow:Constructing forest with params = 
INFO:tensorflow:{'num_trees': 3, 'max_nodes': 1000, 'bagging_fraction': 1.0, 'feature_bagging_fraction': 1.0, 'num_splits_to_consider': 13, 'max_fertile_nodes': 500, 'split_after_samples': 20, 'min_split_samples': 5, 'valid_leaf_threshold': 1, 'dominate_method': 'bootstrap', 'dominate_fraction': 0.99, 'num_classes': 1, 'num_features': 13, 'regression': True, 'bagged_num_features': 13, 'bagged_features': None, 'num_outputs': 1, 'num_output_columns': 2, 'split_initializations_per_input': 1, 'base_random_seed': 0}
INFO:tensorflow:training graph for tree: 0
INFO:tensorflow:training graph for tree: 1
INFO:tensorflow:training graph for tree: 2
INFO:tensorflow:Starting evaluation at 2017-03-09-08:46:13
INFO:tensorflow:Evaluation [1/10]
INFO:tensorflow:Finished evaluation at 2017-03-09-08:46:14
INFO:tensorflow:Saving dict for global step 100: global_step = 100, loss = -183.333
WARNING:tensorflow:Skipping summary for global_step, must be a float or np.float32.
Out[4]:
{'global_step': 100, 'loss': -183.33333}

In [15]:
a=regressor.predict(data)
b=np.vstack([a.reshape(1,506),labels.reshape(1,506)])
print(b)


INFO:tensorflow:Constructing forest with params = 
INFO:tensorflow:{'num_trees': 3, 'max_nodes': 1000, 'bagging_fraction': 1.0, 'feature_bagging_fraction': 1.0, 'num_splits_to_consider': 13, 'max_fertile_nodes': 500, 'split_after_samples': 20, 'min_split_samples': 5, 'valid_leaf_threshold': 1, 'dominate_method': 'bootstrap', 'dominate_fraction': 0.99, 'num_classes': 1, 'num_features': 13, 'regression': True, 'bagged_num_features': 13, 'bagged_features': None, 'num_outputs': 1, 'num_output_columns': 2, 'split_initializations_per_input': 1, 'base_random_seed': 0}
INFO:tensorflow:training graph for tree: 0
INFO:tensorflow:training graph for tree: 1
INFO:tensorflow:training graph for tree: 2
[[ 26.37006187  23.48611259  32.54696274 ...,  24.24166679  24.49845695
   17.7004261 ]
 [ 24.          21.60000038  34.70000076 ...,  23.89999962  22.
   11.89999962]]

In [14]:
print(b.shape)


(1, 1012)

In [19]:
help(regressor.export)


Help on method export in module tensorflow.contrib.tensor_forest.client.random_forest:

export(export_dir, input_fn, signature_fn=None, input_feature_key=None, default_batch_size=1) method of tensorflow.contrib.tensor_forest.client.random_forest.TensorForestEstimator instance
    See BaseEstimator.export.


In [ ]: