在这里,我有一个使用生成的数据训练的增强决策树,并保存为est:
from sklearn.datasets import make_blobs
import pandas as pd
import tensorflow as tf
#creates an input function for a tf model
def make_input_fn(X, Y, n_epochs=None, shuffle=True, verbose=False):
batch_len = len(Y)
def input_fn():
dataset = tf.data.Dataset.from_tensor_slices((dict(X), Y))
if shuffle:
dataset = dataset.shuffle(batch_len)
# For training, cycle thru dataset as many times as need (n_epochs=None).
dataset = dataset.repeat(n_epochs)
#dividing data into batches
dataset = dataset.batch(batch_len)
return dataset
return input_fn
#making data
trainX, trainY = make_blobs(n_samples=10, centers=2, n_features=3, random_state=0)
#xVals
trainX = pd.DataFrame(trainX)
trainX.columns = ['feature{}'.format(num) for num in trainX.columns]
#yVals
trainY = pd.DataFrame(trainY)
trainY.columns = ['flag']
# Defining input function
train_input_fn = make_input_fn(trainX, trainY)
#defining tf feature columns
feature_columns=[]
for feature_name in list(trainX.columns):
feature_columns.append(tf.feature_column.numeric_column(feature_name,dtype=tf.float32))
#creating the estimator
n_batches = 1
est = tf.estimator.BoostedTreesClassifier(feature_columns, n_batches_per_layer=n_batches)
est.train(train_input_fn, max_steps=10)
我想使用该模型根据一行训练数据进行预测以用于测试目的;像这样的事情:res = est.predict(trainX.loc[0])但是,我很难弄清楚如何去做。
慕哥6287543
相关分类