train_predict = scaler.inverse_transform(train_predict.reshape(-1, 1)) y_train = scaler.inverse_transform([y_train.reshape(-1, 1)]) train_predict = np.tile(train_predict, (1, 4)) y_train = np.tile(y_train, (1, 4)) y_train = scaler.inverse_transform(y_train.T).reshape(110, 4) test_predict = scaler.inverse_transform(test_predict) y_test = scaler.inverse_transform([y_test])报错Traceback (most recent call last): File "C:\Users\马斌\Desktop\cnn测试\cnn改.py", line 48, in <module> train_predict = scaler.inverse_transform(train_predict.reshape(-1, 1)) File "D:\python\python3.9.5\pythonProject\venv\lib\site-packages\sklearn\preprocessing\_data.py", line 541, in inverse_transform X -= self.min_ ValueError: non-broadcastable output operand with shape (110,1) doesn't match the broadcast shape (110,4)
时间: 2024-01-26 17:02:40 浏览: 205
这个报错信息和之前的一样,都是因为在进行逆归一化操作时,输出的形状与广播形状不匹配导致的。具体来说,你在对训练数据进行逆归一化操作时,复制了4次train_predict和y_train,让它们的形状变成了(110,4)和(1,440),而逆归一化操作的结果形状是(110,1),所以就无法与广播形状(110,4)匹配了。
解决这个问题的方法就是在进行逆归一化操作之前,先将train_predict和y_train的形状改为(110,1)和(440,1),这样逆归一化操作的结果形状就能与广播形状匹配了。你可以使用numpy的reshape方法来改变形状,具体代码如下:
```
train_predict = scaler.inverse_transform(train_predict.reshape(-1, 1))
y_train = scaler.inverse_transform(y_train.reshape(-1, 1))
train_predict = np.tile(train_predict, (1, 4))
y_train = np.tile(y_train, (1, 4))
y_train = scaler.inverse_transform(y_train.T).reshape(110, 4)
test_predict = scaler.inverse_transform(test_predict)
y_test = scaler.inverse_transform([y_test])
```
这样修改后,就能够顺利地进行逆归一化操作了。
相关问题
train_predict = model.predict(X_train) test_predict = model.predict(X_test) train_predict = scaler.inverse_transform(train_predict) y_train = scaler.inverse_transform([y_train]) train_predict = np.tile(train_predict, (1, 4)) #将train_predict复制4次,变为(110,4) train_predict = scaler.inverse_transform(train_predict) #进行逆归一化操作 y_train = np.tile(y_train, (1, 4)) #将y_train复制4次,变为(1, 440) y_train = scaler.inverse_transform(y_train.T) #进行逆归一化操作,并将结果reshape为(110,4) y_train = y_train.reshape(110, 4) test_predict = scaler.inverse_transform(test_predict) y_test = scaler.inverse_transform([y_test])报错Traceback(最近一次调用): 文件 “C:\Users\马斌\Desktop\cnn测试\cnn改.py”,第 48 行,在 <module> train_predict = scaler.inverse_transform(train_predict) 文件 “D:\python\python3.9.5\pythonProject\venv\lib\site-packages\sklearn\preprocessing_data.py”, 第 541 行,在 X inverse_transform -= self.min_ 值错误:形状为 (110,1) 的不可广播输出操作数与广播形状 (110,4) 不匹配
根据报错信息,很明显是因为在进行逆归一化操作时,输出的形状与广播形状不匹配导致的。具体来说,你在对训练数据进行逆归一化操作时,复制了4次train_predict和y_train,让它们的形状变成了(110,4)和(1,440),而逆归一化操作的结果形状是(110,1),所以就无法与广播形状(110,4)匹配了。
解决这个问题的方法就是在进行逆归一化操作之前,先将train_predict和y_train的形状改为(110,1)和(440,1),这样逆归一化操作的结果形状就能与广播形状匹配了。你可以使用numpy的reshape方法来改变形状,具体代码如下:
```
train_predict = scaler.inverse_transform(train_predict.reshape(-1, 1))
y_train = scaler.inverse_transform([y_train.reshape(-1, 1)])
train_predict = np.tile(train_predict, (1, 4))
y_train = np.tile(y_train, (1, 4))
y_train = scaler.inverse_transform(y_train.T).reshape(110, 4)
test_predict = scaler.inverse_transform(test_predict)
y_test = scaler.inverse_transform([y_test])
```
这样修改后,就能够顺利地进行逆归一化操作了。
train_predict = model.predict(X_train) test_predict = model.predict(X_test) train_predict = scaler.inverse_transform(train_predict) y_train = scaler.inverse_transform([y_train]) train_predict = np.tile(train_predict, (1, 4)) #将train_predict复制4次,变为(110,4) train_predict = scaler.inverse_transform(train_predict) #进行逆归一化操作 y_train = np.tile(y_train, (1, 4)) #将y_train复制4次,变为(1, 440) y_train = scaler.inverse_transform(y_train.T) #进行逆归一化操作,并将结果reshape为(110,4) test_predict = scaler.inverse_transform(test_predict) y_test = scaler.inverse_transform([y_test])报错Traceback (most recent call last): File "C:\Users\马斌\Desktop\cnn测试\cnn改.py", line 47, in <module> train_predict = scaler.inverse_transform(train_predict) File "D:\python\python3.9.5\pythonProject\venv\lib\site-packages\sklearn\preprocessing_data.py", line 541, in inverse_transform X -= self.min_ ValueError: non-broadcastable output operand with shape (110,1) doesn't match the broadcast shape (110,4)如何修改代码
你需要将y_train的形状变为(110,4),可以使用numpy的reshape()函数。具体修改代码如下:
```
train_predict = model.predict(X_train)
test_predict = model.predict(X_test)
train_predict = scaler.inverse_transform(train_predict)
y_train = scaler.inverse_transform([y_train])
train_predict = np.tile(train_predict, (1, 4)) #将train_predict复制4次,变为(110,4)
train_predict = scaler.inverse_transform(train_predict) #进行逆归一化操作
y_train = np.tile(y_train, (1, 4)) #将y_train复制4次,变为(1, 440)
y_train = scaler.inverse_transform(y_train.T) #进行逆归一化操作,并将结果reshape为(110,4)
y_train = y_train.reshape(110, 4)
test_predict = scaler.inverse_transform(test_predict)
y_test = scaler.inverse_transform([y_test])
```
这样修改后,y_train的形状就变为了(110,4),可以进行逆归一化操作了。
阅读全文