我的数据集格式如下所示:
8,2,1,1,1,0,3,2,6,2,2,2,28,2,1,2,0,0,15,2,1,2,2,2,15,5,4,4,0,0,6,1,6,2,2,1,28,2,1,3,0,0,2,2,6,2,2,2,28,2,1,2,0,0,3,2,1,2,2,2,18,2,1,4,0,1,3,2,1,2,2,2,18,2,1,2,0,0,3,2,1,2,2,2,18,2,1,3,0,0,2,2,6,2,2,2,28,2,1,12,0,0,5,2,2,2,2,2,13,1,1,2,0,0,3,2,1,2,2,2,1
它全部由类别数据组成,每个特征都以数值编码。我尝试了以下代码:
monthly_income = tf.contrib.layers.sparse_column_with_keys("monthly_income", keys=['1','2','3','4','5','6']) #其他列也以相同的方式声明 m = tf.contrib.learn.LinearClassifier(feature_columns=[ caste, religion, differently_abled, nature_of_activity, school, dropout, qualification, computer_literate, monthly_income, smoke,drink,tobacco,sex], model_dir=model_dir)
但我遇到了以下错误:
TypeError: 签名不匹配。键必须是dtype <dtype: 'string'>,但得到的是<dtype: 'int64'>。
回答:
我认为问题不在于你展示的代码之外。我猜测csv文件中的特征被读取为整数,但你通过传递keys=['1', '2', ...]
期望它们是字符串。
尽管如此,在这种情况下,我建议你使用sparse_column_with_integerized_feature:
monthly_income = tf.contrib.layers.sparse_column_with_integerized_feature("monthly_income", bucket_size=7)