【发布时间】: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