Tensorflow Training 有错误 KeyError: 'Like'



我在改编这个Tensorflow教程时一直遇到错误?我在他们的网站上举了个例子,使用了我的数据集,并改变了我试图训练该项目的领域。我已经检查了CSV电子表格和代码是否相同。该列是";类似于包含"是"one_answers"否"的"列"。

我试着把"是"one_answers"否"分别改为1和2,但没有成功,而且我对编程太陌生了,已经没有什么想法了。我只是不明白我哪里出了问题,因为它看起来与示例相同,但对";关键字段";。

ds = make_input_fn(dftrain, y_train, batch_size=10)()
for feature_batch, label_batch in ds.take(1):
print('Some feature keys:', list(feature_batch.keys()))
print()
print('A batch of Like:', feature_batch['Like'].numpy())
print()
print('A batch of Labels:', label_batch.numpy())

这是错误

KeyError                                  Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_22420/4226531187.py in <module>
3   print('Some feature keys:', list(feature_batch.keys()))
4   print()
----> 5   print('A batch of Like:', feature_batch['Like'].numpy())
6   print()
7   print('A batch of Labels:', label_batch.numpy())
KeyError: 'Like'

在模型中使用之前,应该对数据帧进行预处理。假设您有一个csv文件,其中包含一列"Like"(值-是,否(,如下所示:

import pandas as pd
df=pd.read_csv("/content/GoogleDrive/MyDrive/yes_no example - Sheet1.csv")  # my sample csv
df.head()

输出:

Name    Like
0   Mohan   yes
1   Shyam   no
2   Renu    yes
3   vivek   yes
4   sohna   no

您可以定义一个函数来将字符串转换为数字,并可以将该函数映射到数据帧列:

def liking(yes_no):
if yes_no =='yes':
return 1
elif yes_no== 'no':
return 2
df.Like = df.Like.map(liking)
df.head()

输出:

Name    Like
0   Mohan   1
1   Shyam   2
2   Renu    1
3   vivek   1
4   sohna   2

最新更新