张量流中的实施决策树

时间:2020-08-19 22:32:33

标签: python tensorflow keras

feature_columns = []
for feature_name in train.columns.tolist() :
    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(X_train, y_train)
eval_input_fn = make_input_fn(X_test, y_test, shuffle=False, n_epochs=1)
n_batches = 1
est = tf.estimator.BoostedTreesClassifier(feature_columns,
                                      n_batches_per_layer=n_batches)
est.train(train_input_fn, max_steps=100)
result = est.evaluate(eval_input_fn)
result

建立了决策树模型。像一切正常,训练,检查验证。但我只是无法运行测试样本(

test_input_fn = tf.data.Dataset.from_tensors(dict(X))
prediction = list(est.predict(test_input_fn))

enter image description here

我研究的例子 https://www.tensorflow.org/tutorials/estimator/boosted_trees

这是我读取各种参数的地方。我只是想不通如何获得测试样本的预测 https://www.tensorflow.org/api_docs/python/tf/estimator/BoostedTreesClassifier

1 个答案:

答案 0 :(得分:0)

test_input_fn = make_input_fn(test, test.index, shuffle=False, n_epochs=1)
preds = est.predict(test_input_fn)
preds = [pred['class_ids'][0] for pred in preds]
pd.DataFrame({'PassengerId': dataTest.PassengerId, 'Survived': 
preds}).to_csv('submission.csv', index=False)
!head submission.csv