with sklearn multiple machine learning forecast data python pandas prediction data-science

python - sklearn - ValueError: Número incorrecto de elementos aprobados-¿Significado y sugerencias?



sklearn time series prediction (1)

Recibo el error: ValueError: Wrong number of items passed 3, placement implies 1 , y estoy luchando para averiguar dónde y cómo puedo comenzar a abordar el problema.

Realmente no entiendo el significado del error; lo que me dificulta la solución de problemas. También he incluido el bloque de código que está activando el error en mi portátil Jupyter.

Los datos son difíciles de adjuntar; así que no estoy buscando a nadie que intente recrear este error para mí. Solo estoy buscando algunos comentarios sobre cómo podría abordar este error.

KeyError Traceback (most recent call last) C:/Users/brennn1/AppData/Local/Continuum/Anaconda3/lib/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance) 1944 try: -> 1945 return self._engine.get_loc(key) 1946 except KeyError: pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)() pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)() pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)() pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)() KeyError: ''predictedY'' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) C:/Users/brennn1/AppData/Local/Continuum/Anaconda3/lib/site-packages/pandas/core/internals.py in set(self, item, value, check) 3414 try: -> 3415 loc = self.items.get_loc(item) 3416 except KeyError: C:/Users/brennn1/AppData/Local/Continuum/Anaconda3/lib/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance) 1946 except KeyError: -> 1947 return self._engine.get_loc(self._maybe_cast_indexer(key)) 1948 pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)() pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)() pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)() pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)() KeyError: ''predictedY'' During handling of the above exception, another exception occurred: ValueError Traceback (most recent call last) <ipython-input-95-476dc59cd7fa> in <module>() 26 return gp, results 27 ---> 28 gp_dailyElectricity, results_dailyElectricity = predictAll(3, 0.04, trainX_dailyElectricity, trainY_dailyElectricity, testX_dailyElectricity, testY_dailyElectricity, testSet_dailyElectricity, ''Daily Electricity'') <ipython-input-95-476dc59cd7fa> in predictAll(theta, nugget, trainX, trainY, testX, testY, testSet, title) 8 9 results = testSet.copy() ---> 10 results[''predictedY''] = predictedY 11 results[''sigma''] = sigma 12 C:/Users/brennn1/AppData/Local/Continuum/Anaconda3/lib/site-packages/pandas/core/frame.py in __setitem__(self, key, value) 2355 else: 2356 # set column -> 2357 self._set_item(key, value) 2358 2359 def _setitem_slice(self, key, value): C:/Users/brennn1/AppData/Local/Continuum/Anaconda3/lib/site-packages/pandas/core/frame.py in _set_item(self, key, value) 2422 self._ensure_valid_index(value) 2423 value = self._sanitize_column(key, value) -> 2424 NDFrame._set_item(self, key, value) 2425 2426 # check if we are modifying a copy C:/Users/brennn1/AppData/Local/Continuum/Anaconda3/lib/site-packages/pandas/core/generic.py in _set_item(self, key, value) 1462 1463 def _set_item(self, key, value): -> 1464 self._data.set(key, value) 1465 self._clear_item_cache() 1466 C:/Users/brennn1/AppData/Local/Continuum/Anaconda3/lib/site-packages/pandas/core/internals.py in set(self, item, value, check) 3416 except KeyError: 3417 # This item wasn''t present, just insert at end -> 3418 self.insert(len(self.items), item, value) 3419 return 3420 C:/Users/brennn1/AppData/Local/Continuum/Anaconda3/lib/site-packages/pandas/core/internals.py in insert(self, loc, item, value, allow_duplicates) 3517 3518 block = make_block(values=value, ndim=self.ndim, -> 3519 placement=slice(loc, loc + 1)) 3520 3521 for blkno, count in _fast_count_smallints(self._blknos[loc:]): C:/Users/brennn1/AppData/Local/Continuum/Anaconda3/lib/site-packages/pandas/core/internals.py in make_block(values, placement, klass, ndim, dtype, fastpath) 2516 placement=placement, dtype=dtype) 2517 -> 2518 return klass(values, ndim=ndim, fastpath=fastpath, placement=placement) 2519 2520 # TODO: flexible with index=None and/or items=None C:/Users/brennn1/AppData/Local/Continuum/Anaconda3/lib/site-packages/pandas/core/internals.py in __init__(self, values, placement, ndim, fastpath) 88 raise ValueError(''Wrong number of items passed %d, placement '' 89 ''implies %d'' % (len(self.values), ---> 90 len(self.mgr_locs))) 91 92 @property ValueError: Wrong number of items passed 3, placement implies 1

Mi código es el siguiente:

def predictAll(theta, nugget, trainX, trainY, testX, testY, testSet, title): gp = gaussian_process.GaussianProcess(theta0=theta, nugget =nugget) gp.fit(trainX, trainY) predictedY, MSE = gp.predict(testX, eval_MSE = True) sigma = np.sqrt(MSE) results = testSet.copy() results[''predictedY''] = predictedY results[''sigma''] = sigma print ("Train score R2:", gp.score(trainX, trainY)) print ("Test score R2:", sklearn.metrics.r2_score(testY, predictedY)) plt.figure(figsize = (9,8)) plt.scatter(testY, predictedY) plt.plot([min(testY), max(testY)], [min(testY), max(testY)], ''r'') plt.xlim([min(testY), max(testY)]) plt.ylim([min(testY), max(testY)]) plt.title(''Predicted vs. observed: '' + title) plt.xlabel(''Observed'') plt.ylabel(''Predicted'') plt.show() return gp, results gp_dailyElectricity, results_dailyElectricity = predictAll(3, 0.04, trainX_dailyElectricity, trainY_dailyElectricity, testX_dailyElectricity, testY_dailyElectricity, testSet_dailyElectricity, ''Daily Electricity'')


En general, el error ValueError: Wrong number of items passed 3, placement implies 1 sugiere que está intentando colocar demasiadas palomas en muy pocos casilleros. En este caso, el valor a la derecha de la ecuación.

results[''predictedY''] = predictedY

está tratando de poner 3 "cosas" en un contenedor que permite solo una. Debido a que el lado izquierdo es una columna de marco de datos y puede aceptar múltiples elementos en esa dimensión (columna), debería ver que hay demasiados elementos en otra dimensión.

Aquí, parece que estás usando sklearn para modelar, que es de donde viene gaussian_process.GaussianProcess() (supongo, pero corríjame y revisa la pregunta si esto está mal).

Ahora, generas valores predichos para y aquí:

predictedY, MSE = gp.predict(testX, eval_MSE = True)

Sin embargo, como podemos ver en la documentación de GaussianProcess , predict() devuelve dos elementos. El primero es y , que es similar a un arreglo (el énfasis es mío). Eso significa que puede tener más de una dimensión o, para ser concreto para personas de cabeza gruesa como yo, puede tener más de una columna: ver que puede devolver (n_samples, n_targets) que, dependiendo de testX , podría ser (1000, 3) (sólo para recoger números). Por lo tanto, su predictedY podría tener 3 columnas.

Si es así, cuando intenta colocar algo con tres "columnas" en una sola columna de marco de datos, está pasando 3 elementos donde solo cabría 1.