我是 Python 新手。我想創建一個新陣列,其中包含來自現有陣列的所有值以及該步驟。
我試圖實作它,但我認為還有另一種方法可以獲得更好的性能。任何嘗試或建議都將受到高度贊賞。
例如:目前,我有:
- 一個陣列:115.200 個值(二維)
- 步數:10.000
……
array([[ 0.2735, -0.308 ],
[ 0.287 , -0.3235],
[ 0.2925, -0.324 ],
[ 0.312 , -0.329 ],
[ 0.3275, -0.345 ],
[ 0.3305, -0.352 ],
[ 0.332 , -0.3465],
...
[ 0.3535, -0.353 ],
[ 0.361 , -0.3445],
[ 0.3545, -0.329 ]])
期望:一個新的陣列以 10.000 的步長對上面的陣列進行切片。
下面是我的代碼:
for x in ecg_data:
number_samples_by_duration_exp_temp = 10000
# len(ecg_property.sample) = 115200
times = len(ecg_property.sample) / number_samples_by_duration_exp_temp
index_by_time = [int(y)*number_samples_by_duration_exp_temp for y in np.arange(1, times, 1)]
list = []
temp = 0
for z in index_by_time:
arr_samples_by_duration = ecg_property.sample[temp:z]
list.append(arr_samples_by_duration)
temp = z
uj5u.com熱心網友回復:
numpy 不能用于此目的,因為len(ecg_property.sample) #115,200它不能被完全整除,number_samples_by_duration_exp_temp #10,000并且 numpy 不能允許不同長度的元素:)
您可以嘗試串列理解。
result_list = [ecg_property.sample[temp :temp step] for temp in np.range(times)*step ]
在哪里
step=10000和times = len(ecg_property.sample) / step
如果需要,可以根據需要進一步修改。
(您可以在此答案中嘗試上述代碼行中的每個步驟,并查看輸出以了解每個步驟)希望這能成功。泰!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/469101.html
上一篇:如何使用新陣列串列更新字典陣列?
