Python:类型错误 - 单例数组

标签 python pandas numpy machine-learning keras

正如我的示例代码所示,我正在使用 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/

相关文章:

python - 重置 pandas 中多级列的索引,以便较高的索引 perfaces 较低的索引

python - 水平连接numpy向量和矩阵

python - 查找模式时出现无法散列的类型错误

python - tf.reduce_sum() uint8 出现意外结果

python - Django 。在管理员的弹出窗口中编辑模型表单

python - 在 Windows 7 上比较 archiwum.rar 内容和从文件夹中的 .rar 中提取的数据

python - 如何在Python中找到每周表现最好的10个值?

python - 生成一维数组多次移位的二维数组的有效方法

python - 将事件日志映射到 pandas 中的间隔日志

python - 在python中生成多个独立的随机流