【问题标题】:How to create a tf.feature_column by multiplying two other tf.feature_columns?如何通过将另外两个 tf.feature_columns 相乘来创建 tf.feature_column?
【发布时间】:2017-10-24 07:49:12
【问题描述】:

在 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

怎么做?

【问题讨论】:

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


    【解决方案1】:

    您可以声明一个自定义数值列并将其添加到 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())
    

    【讨论】:

    • 谢谢!您的示例还有助于我更多地了解输入功能的用途。
    猜你喜欢
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    相关资源
    最近更新 更多