【问题标题】:Creating many feature columns in Tensorflow在 TensorFlow 中创建许多特征列
【发布时间】:2018-03-31 18:34:36
【问题描述】:

我正在开始一个 Tensorflow 项目,并且正在定义和创建我的特征列。然而,我有成百上千的特征——这是一个相当广泛的数据集。即使经过预处理和擦洗,我也有很多列。

创建feature_column 的传统方式在Tensorflow tutorial 甚至这个StackOverflow post 中定义。您基本上为每个特征列声明和初始化一个 Tensorflow 对象:

gender = tf.feature_column.categorical_column_with_vocabulary_list(
    "gender", ["Female", "Male"])

如果您的数据集只有几列,这一切都很好,但就我而言,我当然不希望有数百行代码初始化不同的 feature_column 对象。

解决此问题的最佳方法是什么?我注意到在教程中,所有的列都被收集为一个列表:

base_columns = [
    gender, native_country, education, occupation, workclass, relationship,
    age_buckets,
]

最终传递给您的估算器:

m = tf.estimator.LinearClassifier(
    model_dir=model_dir, feature_columns=base_columns)

那么,为数百列创建feature_column 的理想方法是将它们直接附加到列表中吗?像这样?

my_columns = []

for col in df.columns:
    if is_string_dtype(df[col]): #is_string_dtype is pandas function
        my_column.append(tf.feature_column.categorical_column_with_hash_bucket(col, 
            hash_bucket_size= len(df[col].unique())))

    elif is_numeric_dtype(df[col]): #is_numeric_dtype is pandas function
        my_column.append(tf.feature_column.numeric_column(col))

这是创建这些特征列的最佳方式吗?还是我错过了 Tensorflow 的一些功能,可以让我绕过这一步?

【问题讨论】:

  • 你所拥有的对我来说很有意义。 :)
  • 你能把它提升为答案吗,@greeness?谢谢! :)
  • 好吧,不过它并没有为 op 的问题添加任何内容。

标签: python tensorflow neural-network


【解决方案1】:

您在问题中发布的内容是有道理的。基于您自己的代码的小扩展:

import pandas.api.types as ptypes
my_columns = []
for col in df.columns:
  if ptypes.is_string_dtype(df[col]): 
    my_columns.append(tf.feature_column.categorical_column_with_hash_bucket(col, 
        hash_bucket_size= len(df[col].unique())))

  elif ptypes.is_numeric_dtype(df[col]): 
    my_columns.append(tf.feature_column.numeric_column(col))

  elif ptypes.is_categorical_dtype(df[col]): 
    my_columns.append(tf.feature_column.categorical_column(col, 
        hash_bucket_size= len(df[col].unique())))

【讨论】:

    【解决方案2】:

    我使用了你自己的答案。只是编辑了一点(for 循环中应该有my_columns 而不是my_column)并按照对我有用的方式发布。

    import pandas.api.types as ptypes
    
    my_columns = []
    
    for col in df.columns:
      if ptypes.is_string_dtype(df[col]): #is_string_dtype is pandas function
        my_columns.append(tf.feature_column.categorical_column_with_hash_bucket(col, 
            hash_bucket_size= len(df[col].unique())))
    
      elif ptypes.is_numeric_dtype(df[col]): #is_numeric_dtype is pandas function
        my_columns.append(tf.feature_column.numeric_column(col))
    

    【讨论】:

      【解决方案3】:

      上述两种方法仅适用于熊猫数据框中提供的数据,其中每列都有列名。但是,如果您拥有所有数字列并且您不想命名这些列。例如从一个 numpy 数组中读取几个数值列,你可以使用这样的东西:-

      feature_column = [tf.feature_column.numeric_column(key='image',shape=(784,))] 
      
      input_fn = tf.estimator.inputs.numpy_input_fn(dict({'image':x_train})  
      

      其中 X_train 是您的 numy 数组,包含 784 列。您可以查看 Vikas Sangwan 的 post 了解更多详情。

      【讨论】:

        猜你喜欢
        • 2017-08-15
        • 1970-01-01
        • 1970-01-01
        • 2020-02-06
        • 2021-01-03
        • 2019-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多