我有一个LSTM神经网络来测试该网络的预测能力,并且它可用于一列。但是现在我想对不同的项目使用几列,并为每一列计算“ ABSE”。例如,如果我有两列:
它需要分别为每一列计算“ ABSE”函数。
我下面的代码失败。有人可以帮我吗?
这是我尝试的方法,但出现值错误:
ValueError: non-broadcastable output operand with shape (1,1) doesn't
match the broadcast shape (1,2)
这发生在行上:
---> 51 trainPredict = scaler.inverse_transform(trainPredict)
代码:
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
return numpy.array(dataX), numpy.array(dataY)
def ABSE(a,b):
ABSE = abs((b-a)/b)
return numpy.mean(ABSE)
columns = df[['Item1','Item2']]
for i in columns:
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.5)
test_size = 1- train_size
train, test = dataset[0:train_size,:],
dataset[train_size:len(dataset),:]
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(1, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=1, batch_size = 1, verbose = 0)
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
# calculate root mean squared error
trainScore = ABSE(trainY[0], trainPredict[:,0])
print('Train Score: %.2f ABSE' % (trainScore))
testScore = ABSE(testY[0], testPredict[:,0])
print('Test Score: %.2f ABSE' % (testScore))
print(testY[0].T,testPredict[:,0].T)