python - 如何在 tensorflow 中将数值分类数据转换为稀疏张量?

标签 python machine-learning tensorflow logistic-regression

我的数据集格式如下所示:

8,2,1,1,1,0,3,2,6,2,2,2,2
8,2,1,2,0,0,15,2,1,2,2,2,1
5,5,4,4,0,0,6,1,6,2,2,1,2
8,2,1,3,0,0,2,2,6,2,2,2,2
8,2,1,2,0,0,3,2,1,2,2,2,1
8,2,1,4,0,1,3,2,1,2,2,2,1
8,2,1,2,0,0,3,2,1,2,2,2,1
8,2,1,3,0,0,2,2,6,2,2,2,2
8,2,1,12,0,0,5,2,2,2,2,2,1
3,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'])
        #Other columns are also declared in the same way

        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: Signature mismatch. Keys must be dtype <dtype: 'string'>, got <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)

关于python - 如何在 tensorflow 中将数值分类数据转换为稀疏张量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41178047/

相关文章:

python - 值错误 : The shape of the input to "Flatten" is not fully defined

machine-learning - 为什么我的体重没有更新?

python - 一个非常短的代码,但 python 中的语法无效?

python - 数组数组的平均值

machine-learning - 粒子群优化 pbest 和 gbest

python - `tf.reciprocal` 与 `tf.inv` : is there any difference?

python - 比较不等长度的列表没有错误

python - 如何使用 Tkinter 根据长度创建彩色线条?

python - 如何用 3d flat 绘制多元线性回归

machine-learning - 贝叶斯优化不会提高预测准确性