我需要生成一维数组,其中重复的整数序列由随机数的零分隔.
到目前为止,我正在使用下一个代码:
from random import normalvariate regular_sequence = np.array([1,2,3,4,5], dtype=np.int) n_iter = 10 lag_mean = 10 # mean length of zeros sequence lag_sd = 1 # standard deviation of zeros sequence length # Sequence of lags lengths lag_seq = [int(round(normalvariate(lag_mean, lag_sd))) for x in range(n_iter)] # Generate list of concatenated zeros and regular sequences seq = [np.concatenate((np.zeros(x, dtype=np.int), regular_sequence)) for x in lag_seq] seq = np.concatenate(seq)
当我需要很多长序列时它可以工作但看起来很慢.那么,我该如何优化呢?