我正在使用财务时间序列数据,并且与numpy
reshape
函数有点混淆。我的目标是为log-returns
参数计算adj_close
。
inputs = np.array([df_historical_data[key][-W:], axis = 1).values for key in stock_list])
inputs.shape //(8, 820, 5)
prices = inputs[:, :, 0]
prices.shape //(8, 820)
prices[:,0]
array([ 4.17000004e+02, 4.68800000e+00, 8.47889000e-03,
3.18835850e+00, 3.58412583e+00, 8.35364850e-01,
5.54610005e-04, 3.33600003e-05]) //close prices of 8 stock for 0 day
但是对于我的程序,我需要输入的形状为(820, 8, 5)
所以我决定重塑我的numpy数组
inputs = np.array([df_historical_data[key][-W:], axis = 1).values for key in stock_list]).reshape(820, 8, 5)
inputs.shape //(820, 8, 5)
prices = inputs[:, :, 0]
prices.shape //(820, 8)
prices[0]
array([ 417.00000354, 436.5100001 , 441.00000442, 440. ,
416.10000178, 409.45245 , 422.999999 , 432.48000001])
// close price of 1 stock for 8 days
// but should be the same as in the example above
似乎我没有正确地重新塑造我的阵列。 无论如何,我无法理解为什么会发生这种奇怪的行为。
答案 0 :(得分:1)
假设我们有一个如下数组:
import numpy as np
m, w, l = 2, 3, 4
array1 = np.array([[['m%d w%d l%d' % (mi, wi, li) for li in range(l)] for wi in range(w)] for mi in range(m)])
print(array1.shape)
print(array1)
重塑可能不是你想要的,但这是你怎么做的:
array2 = array1.reshape(w, m, l)
print(array2.shape)
print(array2)
以下是转置的完成方式:
# originally
# 0, 1, 2
# m, w, l
# -------
# transposed
array3 = array1.transpose(1, 0, 2)
# w, m, l
print(array3.shape)
print(array3)