python - 如何通过将另外两个 tf.feature_column 相乘来创建 tf.feature_column?

标签 python tensorflow machine-learning feature-extraction tensorflow-estimator

在 Tensorflow 中已经有一个通过交叉列创建特征的函数 tf.feature_column.crossed_column ,但它更多地用于类别数据。数字数据怎么样?

例如,已有 2 列

age = tf.feature_column.numeric_column("age")
education_num = tf.feature_column.numeric_column("education_num")

如果我想像这样根据年龄和教育数字创建第三和第四个特征列

my_feature = age * education_num
my_another_feature = age * age

如何做到这一点?

最佳答案

您可以声明自定义数字列并将其添加到 input function 中的数据框中。 :

# Existing features
age = tf.feature_column.numeric_column("age")
education_num = tf.feature_column.numeric_column("education_num")
# Declare a custom column just like other columns
my_feature = tf.feature_column.numeric_column("my_feature")

...
# Add to the list of features
feature_columns = { ... age, education_num, my_feature, ... }

...
def input_fn():
  df_data = pd.read_csv("input.csv")
  df_data = df_data.dropna(how="any", axis=0)
  # Manually update the dataframe
  df_data["my_feature"] = df_data["age"] * df_data["education_num"]

  return tf.estimator.inputs.pandas_input_fn(x=df_data,
                                             y=labels,
                                             batch_size=100,
                                             num_epochs=10)

...
model.train(input_fn=input_fn())

关于python - 如何通过将另外两个 tf.feature_column 相乘来创建 tf.feature_column?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46904972/

相关文章:

python - python2.7 中的正则表达式

python - 有没有办法从 python 数组中删除相似的(数字)元素

Tensorflow Argmax : What's the difference between "axis" and "dimension" parameter?

python - sklearn : Naive Bayes classifier gives low accuracy

python - 从特征集中选择集成特征

python - sqlite3 不按计数分组

python - 有没有办法直接将 python 输出发送到剪贴板?

python - InvalidArgumentError : Specified a list with shape [60, 9] 来自形状为 [56,9] 的张量

tensorflow - 在keras的反向传播中跳过层

python - RBF 层 - 理解困难