简体   繁体   English

操作数无法与 MinMaxScaler 中的形状错误一起广播

[英]operands could not be broadcast together with shapes error in MinMaxScaler

I am developing a prediction model in Python, based on a historical data of previous 45 quarters starting from q-1 of 2010. I am using LSTM for prediction.我正在基于从 2010 年第 1 季度开始的前 45 个季度的历史数据用 Python 开发一个预测模型。我正在使用 LSTM 进行预测。 While trying to implement the following line:在尝试执行以下行时:

y_perd_future = scaler.inverse_transform(forecast_copies)[:,0]
X = X.copy()
    936             if self.with_std: 
    937                 X *= self.scale_
    938             if self.with_mean:
    939                 X += self.mean_

 ValueError: operands could not be broadcast together with shapes (31,630,3) (32,) (31,630,3) 

Please dont give me examples of small self created arrays.请不要给我小的自创阵列的例子。 I am looking for advice in context of large data.我正在寻找有关大数据的建议。

Humble Regards谨致问候

I have found easiest solution, instantiate two scaler one for target variable and other independent variable, simple.我找到了最简单的解决方案,为目标变量和其他独立变量实例化两个缩放器,简单。

feature = X_train.columns<br>
xscaler = MinMaxScaler()<br>
X_train[feature] = xscaler.fit_transform(X_train[feature])<br>
X_val[feature] = xscaler.transform(X_val[feature])<br>
X_test[feature] = xscaler.transform(X_test[feature])<br>

yscaler = MinMaxScaler()<br>
y_train = yscaler.fit_transform(y_train.values.reshape(-1,1))<br>
y_val = yscaler.transform(y_val.values.reshape(-1,1))

I think the problem is that one tensor has 31 elements (31, 630, 3) while the other 32 (32,) and thus it cannot be broadcasted.我认为问题在于一个张量有 31 个元素 (31, 630, 3),而另一个 32 (32,) 则无法广播。 At least one dimension must match.至少一个维度必须匹配。

I think the problem is what is scaled first, First of all always scale, target and features separately I always scale independent variables first & then scale target variable later, I don't know why but MinMaxscaler or any scaling technique takes in to account what is scaled last.我认为问题是首先缩放什么,首先总是分别缩放、目标和特征我总是先缩放自变量,然后再缩放目标变量,我不知道为什么,但是 MinMaxscaler 或任何缩放技术都会考虑到什么最后缩放。 After prediction you can use inverse_transform fro ex.预测后,您可以使用 inverse_transform 来回。 pressure = scaler.inverse_transform(y_train_pred_lr.values.reshape(-1,1)) print(pressure) Better to scale target & features separately,scale target after independent variable Hope this solves your problem, let me know if it does. pressure = scaler.inverse_transform(y_train_pred_lr.values.reshape(-1,1)) print(pressure) 最好分别缩放目标和特征,在自变量之后缩放目标希望这能解决您的问题,如果可以,请告诉我。

If you scale features first & scale target later, this will give you broadcast error如果您先缩放功能并稍后缩放目标,这会给您广播错误

Better to use two different instance of scaling for example, 'scaler' for the independent variables and 'scaler1' for dependent ie target variable.最好使用两个不同的缩放实例,例如,“scaler”用于自变量,“scaler1”用于因变量,即目标变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Numpy错误:操作数无法与形状1一起广播 - Numpy error: operands could not be broadcast together with shapes 1 操作数不能与形状(128,)(0,)错误一起广播 - Operands could not be broadcast together with shapes (128,) (0,) error Scipy pythonoptimization error:ValueError: 操作数无法与形状一起广播 (9,) (6,) (6,) - Scipy pythonoptimization error:ValueError: operands could not be broadcast together with shapes (9,) (6,) (6,) 错误:操作数无法与形状(1776,1,2)一起广播(3896,1,2) - Error: operands could not be broadcast together with shapes (1776,1,2) (3896,1,2) ValueError: 操作数无法与形状 (100,) (99,) 一起广播 Python - ValueError: operands could not be broadcast together with shapes (100,) (99,) error in Python scipy fmin操作数不能与形状一起广播 - scipy fmin operands could not be broadcast together with shapes Python - ValueError:操作数无法与形状一起广播 - Python - ValueError: operands could not be broadcast together with shapes 操作数不能与形状 (100,) (8,8) 一起广播 - Operands could not be broadcast together with shapes (100,) (8,8) ValueError: 操作数无法与形状 (3,) (3000,) 一起广播 - ValueError: operands could not be broadcast together with shapes (3,) (3000,) ValueError:操作数不能与形状一起广播 - ValueError: operands could not be broadcast together with shapes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM