以下代码段
odd_nums = np.array([1,3,5,7,9,11,13])
def window_transform_series(series, window_size):
X = np.asarray([series[i:(i+window_size)] for i in range(series.size)])
return X
X = window_transform_series(odd_nums, 2)
print (X)
应生成输出
[[ 1 3]
[ 3 5]
[ 5 7]
[ 7 9]
[ 9 11]]
相反,它会生成
[array([1, 3]), array([3, 5]), array([5, 7]), array([7, 9]), array([ 9, 11]), array([11, 13]), array([13])]
我做错了什么?
答案 0 :(得分:0)
我自己发现:问题与list to np.array转换无关,但是函数中的窗口逻辑出错:
odd_nums = np.array([1,3,5,7,9,11,13])
def window_transform_series(series, window_size):
X = np.asarray([series[i:(i+window_size)] for i in range(series.size-window_size)])
return X
X = window_transform_series(odd_nums, 2)
print (X)
完全符合我的要求。问题是我把窗口移得太远,导致一个悬空的一维数组,这样np.asarray就无法创建一个形状为5x2的数组。