Implementing random forest using TensorFlow

This is a bonus section where we implement a random forest with TensorFlow. Let's take a look at the following steps and see how it is done:

  1. First, we import the modules we need, as follows:
>>> import tensorflow as tf>>> from tensorflow.contrib.tensor_forest.python import tensor_forest>>> from tensorflow.python.ops import resources
  1. Specify the parameters of the model, including 20 iterations during the training process, 10 trees in total, and 30000 maximal splitting nodes:
>>> n_iter = 20>>> n_classes = 2>>> n_features = int(X_train_enc.toarray().shape[1])>>> n_trees = 10>>> max_nodes = 30000
  1. Next, we create placeholders and build the TensorFlow graph:
>>> x = tf.placeholder(tf.float32, shape=[None, ...

Get Python Machine Learning By Example - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.