Python 类型错误:签名不匹配。密钥必须是dtype<;数据类型:';字符串'>;,得到<;数据类型:';int64'&燃气轮机;

Python 类型错误:签名不匹配。密钥必须是dtype<;数据类型:';字符串'>;,得到<;数据类型:';int64'&燃气轮机;,python,pandas,tensorflow,Python,Pandas,Tensorflow,在我的数据集上从TensorFlow运行程序时,显示以下错误 "TypeError: Signature mismatch. Keys must be dtype <dtype: 'string'>, got <dtype:'int64'>" 非常感谢您的帮助。将有助于在错误消息之前查看输出,以确定此错误在进程的哪个部分触发,但是,消息非常清楚地表明,密钥应该是字符串,而不是整数。我只是在猜测,但是在脚本的前面部分,列名是否设置正确,因为它们可能是本例中引用的键?根据判

在我的数据集上从TensorFlow运行程序时,显示以下错误

"TypeError: Signature mismatch. Keys must be dtype <dtype: 'string'>, got <dtype:'int64'>"

非常感谢您的帮助。

将有助于在错误消息之前查看输出,以确定此错误在进程的哪个部分触发,但是,消息非常清楚地表明,密钥应该是字符串,而不是整数。我只是在猜测,但是在脚本的前面部分,列名是否设置正确,因为它们可能是本例中引用的键?

根据判断,您遇到的问题是由您对功能列的输入或
输入的输出引起的。您的稀疏张量很可能是为
参数输入的非字符串数据类型;稀疏要素列需要字符串值。确保您输入了正确的数据,如果您确定输入了,可以尝试以下操作:

categorical_cols = {k: tf.SparseTensor(
  indices=[[i, 0] for i in range(df[k].size)],
  values=df[k].astype(str).values,  # Convert sparse values to string type
  shape=[df[k].size, 1])
                  for k in CATEGORICAL_COLUMNS}

我就是这样解决这个挑战的:

from sklearn.model_selection import train_test_split

# split the data set 
X_train, X_test, y_train, y_test = train_test_split(M, N, test_size=0.3)

# covert string to int64 for training set
X_train = X_train[X_train.columns] = X_train[X_train.columns].apply(np.int64)
y_train = y_train.apply(np.int64)

# covert string to int64 for testing set
X_test = X_test[X_test.columns] = X_test[X_test.columns].apply(np.int64)
y_test = y_test.apply(np.int64)
from sklearn.model_selection import train_test_split

# split the data set 
X_train, X_test, y_train, y_test = train_test_split(M, N, test_size=0.3)

# covert string to int64 for training set
X_train = X_train[X_train.columns] = X_train[X_train.columns].apply(np.int64)
y_train = y_train.apply(np.int64)

# covert string to int64 for testing set
X_test = X_test[X_test.columns] = X_test[X_test.columns].apply(np.int64)
y_test = y_test.apply(np.int64)