【发布时间】:2020-09-22 13:07:56
【问题描述】:
我想试试我在互联网上找到的这段代码,
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import clear_output
from six.moves import urllib
dftrain = pd.read_csv("train.csv", header=0, delimiter=",") # training data
dfeval = pd.read_csv("eval.csv", header=0, delimiter=",") # testing data
# print(dftrain)
y_train = dftrain.pop('survived')
y_eval = dfeval.pop('survived')
CATEGORICAL_COLUMNS = ['sec', 'n_siblings_spouses', 'parch', 'class', 'deck', 'embark_town', 'alone']
NUMERICAL_COLUMNS = ['age', 'fare']
feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
vocabulary = dftrain[feature_name].unique()
feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocabulary))
for feature_name in NUMERICAL_COLUMNS:
feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))
不幸的是我总是得到这个错误:
Traceback (most recent call last):
File "C:\Users\Michael\PycharmProjects\learningTensorv3\venv\lib\site-packages\pandas\core\indexes\base.py", line 2889, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 97, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'sec'
(它必须连接到第一个“for”语句,但我自己无法弄清楚,所以我希望有人能帮助我) Stack Overflow 上还有另外 2 个出现此错误的实例,在这两个实例中,它们都只是熊猫行为异常。 我尝试重新安装所有库,检查 IDE 更新(我使用 PyCharm),使用旧版本的库并更改“for”语句,不幸的是没有任何帮助。
【问题讨论】:
标签: python pandas tensorflow