[code]tensorflow分桶

  • 分桶 获取ID
In [27]: sess= tf.InteractiveSession()
In [31]: from tensorflow.contrib.layers.python.ops import bucketization_op
In [32]: a= bucketization_op.bucketize(tf.constant([0,1.1,3], shape=(3,)), [1,2])
In [33]: a.eval()
Out[33]: array([0, 1, 2], dtype=int32)

In [35]: a= bucketization_op.bucketize(tf.constant([0,1.1,3,3], shape=(2,2)), [1,2])
In [36]: a.eval()
Out[36]:
array([[0, 1],
       [2, 2]], dtype=int32)
  • 特征分桶转one-hot格式
def bucketized(features, feature_name, boundaries):
    v = tf.contrib.layers.real_valued_column(feature_name)
    bucket = tf.contrib.layers.bucketized_column(v, boundaries=boundaries)
    x = tf.contrib.layers.input_from_feature_columns(
                    columns_to_tensors=features,
                    feature_columns=[bucket])
    return x

  • 新版本 见 tf.raw_ops.Bucketize
原文地址:https://www.cnblogs.com/bregman/p/13814083.html