简体   繁体   English

为什么 Python 会产生这种奇怪的广播错误?

[英]Why does Python generate this weird broadcasting error?

I have an expression A = B/C, where A, B and C are all 1 dimentional numpy arrays with 1258 elements each.我有一个表达式 A =​​ B/C,其中 A、B 和 C 都是 1 维 numpy 数组,每个数组有 1258 个元素。 Yet Python claims that it could not broadcast input array from shape (1259) into shape (1258).然而 Python 声称它无法将输入数组从 shape (1259) 广播到 shape (1258)。 But the input array has not shape 1259. I have printed the dimensions of all three arrays before the division operation, which shows that they are of all length 1258. So why is Python behaving like this?但是输入数组没有1259的形状。我在除法运算之前打印了所有三个数组的维度,这表明它们的长度都是1258。那么为什么Python会这样呢?

Here below is the Error Message, (Note the print out of all three dimensions at the top):下面是错误消息,(注意顶部所有三个维度的打印):

print the dimensions of the arrays:
len(close[1:]) 1258
len(close[0:len(close)-1]) 1258
len(close_changes[1:]) 1258
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-f1157b8312cc> in <module>
      7 indicator_values_all_stocks = np.zeros(shape=(nr_of_stocks, nr_indicator_values_per_stock))
      8 for i, stock in enumerate(df_open.columns):
----> 9     indicator_values_all_stocks[i] = indicator.change_variance_ratio(df_close[stock], shortLength, longLength)
     10 print(time.time() - start)

~\Desktop\Python Projects Organized\Finance\Indicator Statistics\B.31. Change Variance Ratio\indicator.py in change_variance_ratio(close, shortLength, longLength)
    796     print("len(close_changes[1:])", len(close_changes[1:]))
    797 
--> 798     close_changes[1:] = close[1:]/close[0:len(close)-1]
    799     close_changes[0] = np.NaN
    800 

ValueError: could not broadcast input array from shape (1259) into shape (1258)

The code:编码:

shortLength = 5   
longLength = 50   
nr_of_stocks = len(df_open.columns) 
nr_indicator_values_per_stock = len(df_open)
indicator_values_all_stocks = np.zeros(shape=(nr_of_stocks, nr_indicator_values_per_stock))
for i, stock in enumerate(df_open.columns):
    indicator_values_all_stocks[i] = indicator.change_variance_ratio(df_close[stock], shortLength, longLength)

The function change_variance_ratio:函数change_variance_ratio:

def change_variance_ratio(close, shortLength, longLength):

    close_changes = np.zeros(len(close))
    
    print("print the dimensions of the arrays:")
    print("len(close[1:])", len(close[1:]))
    print("len(close[0:len(close)-1])", len(close[0:len(close)-1]))
    print("len(close_changes[1:])", len(close_changes[1:]))

    close_changes[1:] = close[1:]/close[0:len(close)-1]   #This line causes the error
    close_changes[0] = np.NaN

    change_variance_ratio = np.zeros(len(close))
    change_variance_ratio[0:longLength] = np.NaN

    for i in range(longLength, len(close)):
        short_val = np.var(np.log(close_changes[i-shortLength:i]))
        long_val = np.var(np.log(close_changes[i-longLength:i]))
        change_variance_ratio[i] = (short_val/long_val)
    
    return change_variance_ratio

The problem come from the division of close that is a pandas Series .问题来自于close熊猫Series的划分。

Try to modify change_variance_ratio using the to_numpy() method as follow:尝试使用to_numpy()方法修改change_variance_ratio如下:

def change_variance_ratio(close, shortLength, longLength):
    close_array = close.to_numpy()
    close_changes = np.zeros(len(close_array))
    
    print("print the dimensions of the arrays:")
    print("len(close_array[1:])", len(close_array[1:]))
    print("len(close_array[0:len(close_array)-1])", len(close[0:len(close_array)-1]))
    print("len(close_changes[1:])", len(close_changes[1:]))

    close_changes[1:] = close_array[1:]/close_array[0:len(close_array)-1]   #This line causes the error
    close_changes[0] = np.NaN

    change_variance_ratio = np.zeros(len(close_array))
    change_variance_ratio[0:longLength] = np.NaN

    for i in range(longLength, len(close_array)):
        short_val = np.var(np.log(close_changes[i-shortLength:i]))
        long_val = np.var(np.log(close_changes[i-longLength:i]))
        change_variance_ratio[i] = (short_val/long_val)
    
    return change_variance_ratio

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM