gpt4 book ai didi

Python:类型错误 - 单例数组

转载 作者:行者123 更新时间:2023-11-30 09:47:51 24 4
gpt4 key购买 nike

正如我的示例代码所示,我正在使用 LSTM 神经网络使用发现的 CSV 格式数据集 in this link 来开发预测模型。 。

import numpy as np
from keras.models import Sequential
from keras.layers import LSTM, Dense
from sklearn.preprocessing import MinMaxScaler
import pandas as pd
import math
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error


np.random.seed(7)

# Load data
#df = pd.read_csv('test32_C_data.csv')
df = pd.DataFrame(np.random.randint(0,100, size=(100,3)), columns = ['time', 'X', 'Y'])
n_features = 100


def create_sequences(data, window=15, step=1, prediction_distance=15):
x = []
y = []

for i in range(0, len(data) - window - prediction_distance, step):
x.append(data[i:i + window])
y.append(data[i + window + prediction_distance][1])

x, y = np.asarray(x), np.asarray(y)

return x, y


# Scaling prior to splitting
scaler_x = MinMaxScaler(feature_range=(0.01, 0.99))
scaler_y = MinMaxScaler(feature_range=(0.01, 0.99))

scaled_x = scaler_x.fit_transform(df.loc[:, "X"].reshape([-1,1]))
scaled_y = scaler_y.fit_transform(df.loc[:, "Y"].reshape([-1,1]))

scaled_data = np.column_stack((scaled_x, scaled_y))

# Build sequences
x_sequence, y_sequence = create_sequences(scaled_data)

test_len = int(len(x_sequence) * 0.90)
valid_len = int(len(x_sequence) * 0.90)
train_end = len(x_sequence) - (test_len + valid_len)
x_train, y_train = x_sequence[:train_end], y_sequence[:train_end]
x_valid, y_valid = x_sequence[train_end:train_end + valid_len], y_sequence[train_end:train_end + valid_len]
x_test, y_test = x_sequence[train_end + valid_len:], y_sequence[train_end + valid_len:]

# Initialising the RNN
model = Sequential()

# Adding the input layerand the LSTM layer
model.add(LSTM(15, input_shape=(15, 2)))

# Adding the output layer
model.add(Dense(1))

# Compiling the RNN
model.compile(loss='mse', optimizer='rmsprop')

# Fitting the RNN to the Training set
model.fit(x_train, y_train, epochs=5)

# Getting the predicted values
y_pred = model.predict(x_test)

# invert the predictions
y_pred = scaler_y.inverse_transform(y_pred)
y_test = scaler_y.inverse_transform(y_test)

最后,我想根据我的预测模型计算均方根误差 (RMSE),如下所示

rmse_out = math.sqrt(mean_squared_error(y_test[0], y_pred[:,0]))

但是,它抛出了这个错误:TypeError: Singleton array 225.0 can not be believe a valid collection. 我们如何修复这个错误?

最佳答案

删除数组上的切片,因为您正在获取整个数组的 mse。请参阅此处的文档:http://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html

old: rmse_out = math.sqrt(mean_squared_error(y_test[0], y_pred[:,0]))

new: rmse_out = math.sqrt(mean_squared_error(y_test, y_pred))

关于Python:类型错误 - 单例数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50004043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com