执行线性分类器时的Tensorflow KeyError

时间:2018-01-18 11:50:44

标签: python tensorflow keyerror

import numpy as np 
import pandas as pd
import tensorflow as tf

GENDATA = pd.read_csv('generator_info_labels.csv')
GENDATA_SLICED = GENDATA[['latitude','longitude','status']]
GENCLASSIFIER = GENDATA_SLICED.columns[-1] #this will obtain only the last  column name only here it is status
GENCONTINOUS_COLS = GENDATA_SLICED.columns[:-1]
unique_ids = np.unique(GENDATA_SLICED[GENCLASSIFIER]) #this will help to convert the string values of the status values into unique ids automatically 
GENDICT = {v:k for k, v in enumerate(unique_ids)}
label_col = 'status'
GENDATA_SLICED[label_col] = [GENDICT[x] for x in GENDATA[GENCLASSIFIER].values.flatten()]
feature_columns = []
for f in GENCONTINOUS_COLS:
feature_columns += [tf.contrib.layers.real_valued_column(f)]
model = tf.contrib.learn.LinearClassifier(feature_columns = feature_columns,n_classes=len(unique_ids))

def input_fn(df=GENDATA_SLICED):
feature_cols = {k:tf.constant(df[k].values) for k in GENCONTINOUS_COLS}     
label = tf.constant(df[label_col].values)
return feature_cols,label

这是拟合函数:

model.fit(input_fn=input_fn,steps=3000)
new_samples = np.array(([39.8070525,-5.698599],[43.8800776,4.654769]),dtype=np.float64)

这是估算函数:

my_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x":new_samples},y=None,num_epochs=2,shuffle=False)

预测函数:

pred = model.predict_classes(input_fn=my_input_fn)

执行代码时,我收到错误消息,指出KeyError: 'latitude'。试图解决它,但没有得到错误的原因。我的数据集包含三列latitudelongitudestatus,其中status是标签,范围从0到3.

该程序的tracestack如下:

Traceback (most recent call last):
  File "modelfitterror.py", line 51, in <module>
  pred = model.predict_classes(input_fn=my_input_fn)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.py", line 381, in new_func
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/linear.py", line 563, in predict_classes
    as_iterable=as_iterable)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.py", line 316, in new_func
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 615, in predict
    as_iterable=as_iterable)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 913, in _infer_model
    infer_ops = self._get_predict_ops(features)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1252, in _get_predict_ops
    return self._call_model_fn(features, labels, model_fn_lib.ModeKeys.INFER)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1166, in _call_model_fn
    model_fn_results = self._model_fn(features, labels, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/linear.py", line 164, in _linear_model_fn
    scope=scope)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.py", line 486, in weighted_sum_from_feature_columns
    transformed_tensor = transformer.transform(column)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.py", line 838, in transform
    feature_column.insert_transformed_feature(self._columns_to_tensors)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/feature_column.py", line 1816, in insert_transformed_feature
    input_tensor = self._normalized_input_tensor(columns_to_tensors[self.name])
KeyError: 'latitude'

fit函数正确执行。但错误发生在predict_classes ..

0 个答案:

没有答案