当我在这部分遇到这个错误时,我正在训练各种ANN。不知道有没有什么功能被贬值了
def analyze_results(rainfall_data, test_rainfall_data, name, flag=False):
optimized_params = rainfall_data.loc[rainfall_data.RMSE.argmin]
future_steps = optimized_params.future_steps
forecast_values = optimized_params[-1*int(future_steps):]
y_true = test_rainfall_data.iloc[:int(future_steps)]
forecast_values.index = y_true.index
print('=== Best parameters of ' + name + ' ===\n')
if (name == 'FNN' or name == 'LSTM'):
model = create_NN(optimized_params.look_back,
optimized_params.hidden_nodes,
optimized_params.output_nodes)
print('Input nodes(p): ' + str(optimized_params.look_back))
print('Hidden nodes: ' + str(optimized_params.hidden_nodes))
print('Output nodes: ' + str(optimized_params.output_nodes))
elif (name == 'TLNN'):
model = create_NN(len(optimized_params.look_back_lags),
optimized_params.hidden_nodes,
optimized_params.output_nodes)
s = ''
for i in optimized_params.look_back_lags:
s = s+' '+str(i)
print('Look back lags: ' + s)
print('Hidden nodes: ' + str(optimized_params.hidden_nodes))
print('Output nodes: ' + str(optimized_params.output_nodes))
elif (name == 'SANN'):
model = create_NN(optimized_params.seasonal_period,
optimized_params.hidden_nodes,
optimized_params.seasonal_period)
print('Input nodes(s): ' + str(optimized_params.seasonal_period))
print('Hidden nodes: ' + str(optimized_params.hidden_nodes))
print('Output nodes: ' + str(optimized_params.seasonal_period))
print('Number of epochs: ' + str(optimized_params.epochs))
print('Batch size: ' + str(optimized_params.batch_size))
print('Number of future steps forecasted: ' + str(optimized_params.future_steps))
print('Mean Squared Error(MSE): ' + str(optimized_params.MSE))
print('Mean Absolute Error(MAE): ' + str(optimized_params.MAE))
print('Root Mean Squared Error(RMSE): ' + str(optimized_params.RMSE))
print('\n\n')
错误显示在这一行
optimized_params = rainfall_data.loc[(rainfall_data.RMSE.argmin)]
错误显示为
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_12964\1785142711.py in ?()
9
10 # look_back, hidden_nodes, output_nodes, epochs, batch_size, future_steps
11 parameters_LSTM = [[1,2,3,4,5,6,7,8,9,10,11,12,13], [3,4,5,6], [1], [300], [20], [future_steps]]
12
---> 13 RMSE_info = compare_ANN_methods(rainfall_data, test_rainfall_data, scaler, parameters_FNN, parameters_TLNN, parameters_SANN, parameters_LSTM, future_steps)
~\AppData\Local\Temp\ipykernel_12964\2478982653.py in ?(rainfall_data, test_rainfall_data, scaler, parameters_FNN, parameters_TLNN, parameters_SANN, parameters_LSTM, future_steps)
1 def compare_ANN_methods(rainfall_data, test_rainfall_data, scaler, parameters_FNN, parameters_TLNN, parameters_SANN, parameters_LSTM, future_steps):
2
3 information_FNN_df = get_accuracies_FNN(rainfall_data, test_rainfall_data, parameters_FNN, scaler)
----> 4 optimized_params_FNN = analyze_results(information_FNN_df, test_rainfall_data, 'FNN')
5
6 information_TLNN_df = get_accuracies_TLNN(rainfall_data, test_rainfall_data, parameters_TLNN, scaler)
7 optimized_params_TLNN = analyze_results(information_TLNN_df, test_rainfall_data, 'TLNN')
~\AppData\Local\Temp\ipykernel_12964\4019196368.py in ?(rainfall_data, test_rainfall_data, name, flag)
1 def analyze_results(rainfall_data, test_rainfall_data, name, flag=False):
----> 2 optimized_params = rainfall_data.loc[(rainfall_data.RMSE.argmin)]
3 future_steps = optimized_params.future_steps
4 forecast_values = optimized_params[-1*int(future_steps):]
5 y_true = test_rainfall_data.iloc[:int(future_steps)]
~\anaconda3\Lib\site-packages\pandas\core\indexing.py in ?(self, key)
1185 else:
1186 # we by definition only have the 0th axis
1187 axis = self.axis or 0
1188
-> 1189 maybe_callable = com.apply_if_callable(key, self.obj)
1190 maybe_callable = self._check_deprecated_callable_usage(key, maybe_callable)
1191 return self._getitem_axis(maybe_callable, axis=axis)
~\anaconda3\Lib\site-packages\pandas\core\common.py in ?(maybe_callable, obj, **kwargs)
380 obj : NDFrame
381 **kwargs
382 """
383 if callable(maybe_callable):
--> 384 return maybe_callable(obj, **kwargs)
385
386 return maybe_callable
~\anaconda3\Lib\site-packages\pandas\core\base.py in ?(self, axis, skipna, *args, **kwargs)
765 def argmin(
766 self, axis: AxisInt | None = None, skipna: bool = True, *args, **kwargs
767 ) -> int:
768 delegate = self._values
--> 769 nv.validate_minmax_axis(axis)
770 skipna = nv.validate_argmin_with_skipna(skipna, args, kwargs)
771
772 if isinstance(delegate, ExtensionArray):
~\anaconda3\Lib\site-packages\pandas\compat\numpy\function.py in ?(axis, ndim)
395 ValueError
396 """
397 if axis is None:
398 return
--> 399 if axis >= ndim or (axis < 0 and ndim + axis < 0):
400 raise ValueError(f"`axis` must be fewer than the number of dimensions ({ndim})")
~\anaconda3\Lib\site-packages\pandas\core\generic.py in ?(self)
1575 @final
1576 def __nonzero__(self) -> NoReturn:
-> 1577 raise ValueError(
1578 f"The truth value of a {type(self).__name__} is ambiguous. "
1579 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1580 )
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
您遇到的错误是由于您尝试使用
RMSE
数据框中的 rainfall_data
访问提供的行,使用 argmin
检索具有最小值 RMSE
的行的正确方法,如下所示:
optimized_params = rainfall_data.loc[rainfall_data['RMSE'}.idxmin()]
这就是为什么:
问题是
argmin
是一种较旧的方法,它返回最小值的索引。
而 idxmin
是一个现代的 pandas,它直接返回具有最小值的索引标签。
https://pandas.pydata.org/docs/reference/api/pandas.Series.argmin.html
我能给出的关于错误消息的唯一解释
ValueError: The truth value of a DataFrame is ambiguous
特别表明pandas无法确定如何在布尔上下文中解释整个DataFrame(rainfall_data)
。这种混乱源于尝试使用 .argmin
,这不是查找 pandas 系列 (like rainfall_data['RMSE'])
中最小值索引的有效方法。