【问题标题】:Inverse Transform ValueError: operands could not be broadcast together with shapes逆变换值错误:操作数无法与形状一起广播
【发布时间】:2018-02-28 17:00:43
【问题描述】:

请求帮助 - 不断收到 ValueError:在执行 inverse_transform 时无法将操作数与形状一起广播(下面代码中的最后一行)。任何指针都非常感谢。 谢谢。

代码:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

training_set = pd.read_csv('daily.csv')

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

onehotencoder = OneHotEncoder(categorical_features = [0])
training_set = onehotencoder.fit_transform(training_set).toarray()

from sklearn.preprocessing import MinMaxScaler

sc = MinMaxScaler()
training_set = sc.fit_transform(training_set)
X_train = training_set[:, 0:15]
y_train = training_set[:, 15:]
dims = len(X_train[0])
X_train = np.reshape(X_train, (len(X_train), 1, dims))

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM

regressor = Sequential()
regressor.add(LSTM(units = 4, activation = 'sigmoid', input_shape = (None, dims)))
regressor.add(Dense(units = 4))
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
regressor.fit(X_train, y_train, batch_size = 32, epochs = 50)

test_set = pd.read_csv('daily_test.csv')

onehotencoder2 = OneHotEncoder(categorical_features = [0])
test_set = onehotencoder2.fit_transform(test_set).toarray()

inputs = sc.transform(test_set)
inputs = inputs[:, 0:15]
inputs = np.reshape(inputs, (len(inputs), 1, dims))

predicted = regressor.predict(inputs)
predicted = sc.inverse_transform(predicted)

【问题讨论】:

  • 看来重塑是问题所在。所以解决这个问题如下: test_seto = test_set[:, 0:15] test_set = test_seto test_set = np.reshape(test_set, (len(test_set), 1, dims)) predict = regressor.predict(test_set) test_set2 = np .concatenate((test_seto, predicted), axis=1) test_set2 = sc.inverse_transform(test_set2)

标签: python numpy


【解决方案1】:

虽然这听起来很奇怪,但它确实帮助我修复了错误。

如果您使用“excel”来篡改“csv 训练数据”文件,并且要从 excel 中删除一列,

你会在你的 csv 数据中得到一个空白的“,”值,这会给我带来问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 2022-09-24
    • 2015-05-18
    • 2017-09-09
    • 2020-09-06
    • 2012-10-31
    • 2013-04-07
    相关资源
    最近更新 更多