日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python多維列表總是只轉為一維數組問題解決_python

作者:waws520 ? 更新時間: 2022-11-16 編程語言

正文

# 從X和Y中取出相應步長對應的數組并保存至x_data和y_data中
x_data = []
y_data = []
for i in range(len(K) - 24*n + 1):
    x_data.append(X[i:i + 24*n, :])
    y_data.append(Y[i + 24 * n:i + 24 * n + 24 * n, :])
# 將x_data和y_data轉為ndarray
n_train_hours = 24*365
x_train = np.array(x_data)[: n_train_hours, :, :]
y_train = np.array(y_data)[: n_train_hours, :, :]
12345678910

上述代碼最后一排報錯

該報錯是指y_data轉為array之后變成了一維數組,而我要取三維的數據,兩者維度不符合。
于是我做了這樣一個工作:

# 檢查y_data中的每個二維數組的shape
for i in range(len(y_data)):
    print(y_data[i].shape)
123

得到如下輸出:

原本y_data當中應該所有數組是同樣的shape,即(216,1),但通過循環打印發現shape不一致,導致多維列表轉成了一維數組,出現了上述報錯。
最終發現問題來源于循環,改為以下代碼:

x_data = []
y_data = []
for i in range(len(Y) - 24 * n - 24 * n + 1):
    x_data.append(X[i:i + 24*n, :])
    y_data.append(Y[i + 24*n:i + 24*n + 24*n, :])
12345

原文鏈接:https://juejin.cn/post/7147591475293847560

欄目分類
最近更新