python - 如何使用 keras 将数组 reshape 为 3d

标签 python numpy machine-learning keras data-science

我有 1260 行和 2 列,代表每列的一个特征(即“开盘”、“最高”),用于股票预测。我需要将此数组 reshape 为 3D 数组,以便使用 LSTM 输入到神经网络中。

这是我到目前为止的代码:

X_train, y_train = np.array(X_train), np.array(y_train)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 2))

代码的最后一行我对它告诉我的内容感到困惑。它的目的是将其放入 3D 数组中,但对我来说它似乎是 2D 数组。

如何向网络提供一个数组(或开放值向量)和一个高值数组。

最佳答案

我假设X_train.shape是一个2D张量,如果你想使其成为3D,你可以使用np.expand_dims

示例

arr = np.random.randint(0,5,(3,3))

array([[3, 4, 1],
       [1, 0, 1],
       [4, 4, 2]])

arr.shape = (3,3)
reshape_arr = np.np.expand_dims(arr, -1)
reshape_arr.shape = (3,3,1)

关于python - 如何使用 keras 将数组 reshape 为 3d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59924799/

相关文章:

python-3.x - 重新索引数据帧会用 NaN 替换我的所有数据,为什么?

python - 具有硬边距和 C 值的支持向量机

python - Keras model.fit ValueError : Input arrays should have the same number of samples as target arrays

python : How to create mysql databases with PyMySQL?

python - matplotlib 可以打印网格交叉点而不是图上的网格线吗?

python - C中如何判断一个内存页是否被mmaped

python - 如何在 tensorflow 中获得协方差矩阵?

python - Elasticsearch和查询

python - 无法安装 librosa

python - 求解器 'CPLEX' 在 CVXPY 中失败