Tflearn KeyError [] 不在索引中

Posted

技术标签:

【中文标题】Tflearn KeyError [] 不在索引中【英文标题】:Tflearn KeyError [] not in index 【发布时间】:2017-11-04 11:26:37 【问题描述】:

我有这段代码,我认为我在神经网络中输入数据时做错了(神经网络只是暂时的)

这是我的代码:

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import preprocessing

import tflearn

data = pd.read_csv('winequality-white.csv', sep=';')

X = data[['fixed acidity',
         'volatile acidity',
         'citric acid',
         'residual sugar',
         'chlorides',
         'free sulfur dioxide',
         'total sulfur dioxide',
         'density',
         'pH',
         'sulphates',
         'alcohol']]
y = data['quality']

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.1)


# Build neural network
net = tflearn.input_data(shape=[None, 11])
net = tflearn.fully_connected(net, 110)

net = tflearn.regression(net)

# Define model
model = tflearn.DNN(net)
# Start training (apply gradient descent algorithm)
model.fit(X_train, y_train, n_epoch=10, batch_size=1, show_metric=True)

我使用的数据是这样的:

"fixed acidity";"volatile acidity";"citric acid";"residual sugar";"chlorides";"free sulfur dioxide";"total sulfur dioxide";"density";"pH";"sulphates";"alcohol";"quality"
7;0.27;0.36;20.7;0.045;45;170;1.001;3;0.45;8.8;6
6.3;0.3;0.34;1.6;0.049;14;132;0.994;3.3;0.49;9.5;6
8.1;0.28;0.4;6.9;0.05;30;97;0.9951;3.26;0.44;10.1;6
7.2;0.23;0.32;8.5;0.058;47;186;0.9956;3.19;0.4;9.9;6
7.2;0.23;0.32;8.5;0.058;47;186;0.9956;3.19;0.4;9.9;6
8.1;0.28;0.4;6.9;0.05;30;97;0.9951;3.26;0.44;10.1;6
6.2;0.32;0.16;7;0.045;30;136;0.9949;3.18;0.47;9.6;6
...

这是我得到的错误:

=== RESTART: /home/tux/Schreibtisch/Projects/Wineclassifier/classifier.py ===
hdf5 is not supported on this machine (please install/reinstall h5py for optimal experience)
---------------------------------
Run id: Q6AJQP
Log directory: /tmp/tflearn_logs/
---------------------------------Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.5/dist-packages/tflearn/data_flow.py", line 187, in fill_feed_dict_queue
    data = self.retrieve_data(batch_ids)
  File "/usr/local/lib/python3.5/dist-packages/tflearn/data_flow.py", line 222, in retrieve_data
    utils.slice_array(self.feed_dict[key], batch_ids)
  File "/usr/local/lib/python3.5/dist-packages/tflearn/utils.py", line 187, in slice_array
    return X[start]
  File "/usr/local/lib/python3.5/dist-packages/pandas/core/frame.py", line 2056, in __getitem__
    return self._getitem_array(key)
  File "/usr/local/lib/python3.5/dist-packages/pandas/core/frame.py", line 2100, in _getitem_array
    indexer = self.loc._convert_to_indexer(key, axis=1)
  File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexing.py", line 1231, in _convert_to_indexer
    raise KeyError('%s not in index' % objarr[mask])
KeyError: '[3374] not in index'


Training samples: 4408
Validation samples: 0
--

有人知道怎么了?

【问题讨论】:

【参考方案1】:

将以下代码放在代码开头以重置图表。如果您在 IPython 环境中以交互方式运行代码,张量流图可能会变得非常混乱。

import tensorflow as tf
tf.reset_default_graph()

很多人都和你一样。有关更多详细信息,请参阅this Github 问题。

【讨论】:

以上是关于Tflearn KeyError [] 不在索引中的主要内容,如果未能解决你的问题,请参考以下文章

拟合时 Keras GRU NN KeyError:“不在索引中”

KeyError:“[Int64Index dtype='int64', length=9313)] 都不在 [columns] 中”

接收 KeyError:“[Int64Index([ ... dtype='int64', length=1323)] 均不在 [columns] 中”

KeyError:“未在索引中找到”-创建的虚拟变量未在索引中找到

熊猫数据框索引 datetime.date 转换为对象 KeyError

KeyError:'col''发生在索引col'在应用函数期间[重复]