概率神经网络

时间:2018-11-02 10:38:56

标签: python

我正在我的数据集上实现概率神经网络,在它下面的代码在虹膜数据集上进行了测试,没有错误,但是当我将其应用于数据集时,出现以下错误:

KeyError                                  Traceback (most recent call last)
<ipython-input-30-230e6aa7ae95> in <module>()
     13     for i, (train, test) in enumerate(skfold, start=1):
     14             pnn_network = PNN(std=std, step=0.2, verbose=False, batch_size=2)
---> 15             pnn_network.train(input_dataset_data[train], input_dataset_target[train])
     16             predictions = pnn_network.predict(input_dataset_data[test])
     17             print("Positive in predictions:", 1 in predictions)

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2677         if isinstance(key, (Series, np.ndarray, Index, list)):
   2678             # either boolean or fancy integer index
-> 2679             return self._getitem_array(key)
   2680         elif isinstance(key, DataFrame):
   2681             return self._getitem_frame(key)

~\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_array(self, key)
   2721             return self._take(indexer, axis=0)
   2722         else:
-> 2723             indexer = self.loc._convert_to_indexer(key, axis=1)
   2724             return self._take(indexer, axis=1)
   2725 

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _convert_to_indexer(self, obj, axis, is_setter)
   1325                 if mask.any():
   1326                     raise KeyError('{mask} not in index'
-> 1327                                    .format(mask=objarr[mask]))
   1328 
   1329                 return com._values_from_object(indexer)

KeyError: '[  0   1   2   4   5   6   7   8   9  10  11  12  15  16  17  18  19  20\n  21  22  23  25  26  27  28  29  30  31  32  33  34  35  36  38  39  40\n  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58\n  59  60  61  62  63  64  65  66  67  68  69  71  72  73  74  75  76  77\n  78  79  80  82  83  84  85  86  87  88  90  92  93  94  95  96  97  98\n  99 100 101 102 104 105 106 108 109 110 112 114 115 116 117 118 119 120\n 121 122 123 125 126 127 128 131 132 133 134 136 137 138 139 140 141 142\n 143 144 145 146 147 148 149 151 153 154 155 156 157 159 160 161 162 163\n 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 181 182 183\n 185 186 187 188 189 190 192 193 194 195 196 197 198 199 200 201 202 204\n 205 206 207 208 209 211 212 213 214 215 216 217 218 219 220 221 222 223\n 224 225 226 227 228 229 230 231 232 233 234 236 237 238 239 240 241 242\n 243 244 245 246 247 248 249 250 251 252 253 255 257 258 259 260 261 262\n 263 264 265 267 269 270 271 272 273 274 275 276 277 278 279 280 281 282\n 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 300 301\n 302 303 304 305 306 307 308 309 310 311 312 313 314 315 317 318 320 321\n 322 323 324 325 326 327] not in index'

有关虹膜示例的代码如下:

from sklearn import datasets
iris=datasets.load_iris()
input_dataset_data = iris.data
input_dataset_target = iris.target
print(input_dataset_data.shape)
print(input_dataset_target.shape)
kfold_number = 10
skfold = StratifiedKFold(input_dataset_target, kfold_number, shuffle=True)

#print("> Start classify input_dataset dataset")
for std in [0.2, 0.4,  0.6,  0.8, 1]:
    average_results = []
    for i, (train, test) in enumerate(skfold, start=1):
            pnn_network = PNN(std=std, step=0.2, verbose=False, batch_size=2)
            pnn_network.train(input_dataset_data[train], input_dataset_target[train])
            predictions = pnn_network.predict(input_dataset_data[test])
            print("Positive in predictions:", 1 in predictions)
            average_results.append(np.sum(predictions == input_dataset_target[test]) /float(len(predictions)))
    print(std, np.average(average_results))

mydataset的形状下方 形状 (328,13)

Y形 Y形 (328,)

1 个答案:

答案 0 :(得分:0)

您需要按索引访问数据框:

Content Assist Ctrl+Space