我正在使用這個 for 回圈將資料集分成組。但是串列“y”正在轉換為有錯誤的陣列。
def to_sequences(dataset, seq_size=1):
x = []
y = []
for i in range(len(dataset)-seq_size):
window = dataset[i:(i seq_size), 0]
x.append(window)
window2 = dataset[(i seq_size):i seq_size 5, 0]
y.append(window2)
return np.array(x),np.array(y)
seq_size = 5
trainX, trainY = to_sequences(train, seq_size)
print("Shape of training set: {}".format(trainX.shape))
print("Shape of training set: {}".format(trainY.shape))
這是我得到的錯誤資訊
VisibleDeprecationWarning:不推薦從不規則的嵌套序列(它是具有不同長度或形狀的串列或元組或 ndarray 的串列或元組)創建 ndarray。如果您打算這樣做,則必須在創建 ndarray 時指定“dtype=object”。回傳 np.array(x),np.array(y)
找不到為什么它適用于“x”而不適用于“y”的問題。任何的想法 ?
uj5u.com熱心網友回復:
In [247]: dataset = np.arange(20)
In [248]: def to_sequences(dataset, seq_size=1):
...: x = []
...: y = []
...: for i in range(len(dataset)-seq_size):
...: window = dataset[i:(i seq_size), 0]
...: x.append(window)
...: window2 = dataset[(i seq_size):i seq_size 5, 0]
...: y.append(window2)
...: return np.array(x),np.array(y)
...:
和示例運行:
In [250]: to_sequences(dataset[:,None], 5)
<ipython-input-248-176eb762993c>:9: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
return np.array(x),np.array(y)
Out[250]:
(array([[ 0, 1, 2, 3, 4],
[ 1, 2, 3, 4, 5],
[ 2, 3, 4, 5, 6],
[ 3, 4, 5, 6, 7],
[ 4, 5, 6, 7, 8],
[ 5, 6, 7, 8, 9],
[ 6, 7, 8, 9, 10],
[ 7, 8, 9, 10, 11],
[ 8, 9, 10, 11, 12],
[ 9, 10, 11, 12, 13],
[10, 11, 12, 13, 14],
[11, 12, 13, 14, 15],
[12, 13, 14, 15, 16],
[13, 14, 15, 16, 17],
[14, 15, 16, 17, 18]]),
array([array([5, 6, 7, 8, 9]), array([ 6, 7, 8, 9, 10]),
array([ 7, 8, 9, 10, 11]), array([ 8, 9, 10, 11, 12]),
array([ 9, 10, 11, 12, 13]), array([10, 11, 12, 13, 14]),
array([11, 12, 13, 14, 15]), array([12, 13, 14, 15, 16]),
array([13, 14, 15, 16, 17]), array([14, 15, 16, 17, 18]),
array([15, 16, 17, 18, 19]), array([16, 17, 18, 19]),
array([17, 18, 19]), array([18, 19]), array([19])], dtype=object))
第一個陣列是 (n,5) int dtype。第二個是object dtype,包含陣列。大多數陣列 (5,),但最后一個是 (4,),(3,),(2,),(1,)。
dataset[(i seq_size):i seq_size 5, 0]正在切掉dataset. Python/numpy 允許這樣做,但結果被截斷。
y如果你想要一個 (n,5) 形狀的陣列,你將不得不重新考慮切片。
切掉串列的末尾:
In [252]: [1,2,3,4,5][1:4]
Out[252]: [2, 3, 4]
In [253]: [1,2,3,4,5][3:6]
Out[253]: [4, 5]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/424166.html
上一篇:更新numpy陣列的值
下一篇:Numpy可擴展對角矩陣
