【问题标题】:How to assign 1st value to range of rows如何将第一个值分配给行范围
【发布时间】:2023-01-11 16:13:54
【问题描述】:

我想从名为“pred”的数组中获取第一个值并分配给另一个变量 Y_actual,这样第一个 pred 值被分配给第 0 到第 160 行,然后第二个预测值被分配给第 161 到第 320 行,依此类推。

Y_actual = []
seq_length = 160
stride = 1
k=0
label_counter=0

for k in range(0,len(X_test[(seq_length-1):])):   
    
    if label_counter <= seq_length:
        label_counter = label_counter
    elif label_counter > seq_length and label_counter % seq_length==0:
        label_counter += stride
        seq_length += seq_length
        
        
    Y_actual.insert(k, prediction[label_counter])```

我尝试了上面的代码,但是预测的第一个值被分配给了所有的 y_actual。我怎样才能达到上述条件。

例子:

pred= array[[1, 2], [3,4]]
Y_actual=array[[1,2], 
               [1,2],
               .
               .
               160 times
               [3,4]
               .
               160 times

【问题讨论】:

    标签: python loops insert literate-programming


    【解决方案1】:

    我认为您可以为此使用itertools

    from itertools import repeat, chain
    
    # ...
    
    Y_actual = list(
        chain(
            *(
                list(repeat(p, seq_length))
                for p in pred
            )
        )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-05
      • 2021-01-04
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多