我有一些具有不同長度的動態創建的陣列,我想通過彈出每個 n 元素將它們調整為相同的 5000 個元素長度。
這是我到目前為止得到的:
import numpy as np
random_array = np.random.rand(26975,3)
n_to_pop = int(len(random_array) / 5000)
print(n)
如果我用 n (5) 進行下采樣,我會得到 5395 個元素
我可以做 5395 / 5000 = 1.07899,但我不知道如何計算我應該多久彈出一個元素以洗掉最后的 0.07899 個元素。
如果我可以在 5000-5050 的長度范圍內得到也是可以接受的,那么可以用簡單的 .resize 犧牲其余部分
這可能只是一個簡單的數學問題,但我似乎無法在任何地方找到答案。
任何幫助深表感謝。
此致
馬丁
uj5u.com熱心網友回復:
您可以使用類似np.linspace的方法使您的解決方案盡可能統一:
subset = random_array[np.round(np.linspace(0, len(random_array), 5000, endpoint=False)).astype(int)]
您并不總是希望洗掉統一數量的元素。考慮將 5003 元素陣列減少到 5000 個元素而不是 50003 元素陣列的情況。訣竅是創建一組元素以在索引中保持或洗掉盡可能線性的元素,這正是np.linspace它所做的。
你也可以做類似的事情
np.delete(random_array, np.round(np.linspace(0, len(random_array) len(random_array) - 5000, endpoint=False)).astype(int))
uj5u.com熱心網友回復:
您可以使用Step 解決方案使用np.random.choice或np.random.permutation作為:
random_array[np.random.permutation(random_array.shape[0])[:5000]]
如果幾乎均勻地洗掉行,一種方法是:
indices = np.linspace(0, random_array.shape[0], endpoint=False, num=5000, dtype=int)
# [ 0 5 10 16 ... 26958 26964 26969] --> shape = (5000,)
result = random_array[indices]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/494396.html
上一篇:來自高吞吐量源的實時資料繪圖
