Python:通过删除nth元素调整大小数组

发布于 2025-02-09 17:13:41 字数 457 浏览 1 评论 0原文

我有一些动态创建的数组,这些数组的长度不同,我想通过弹出每个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的长度内获得也是可以接受的,那么可以用简单的简单来牺牲其余部分

可能只是一个简单的数学问题,但我似乎在任何地方都找不到答案。

任何帮助都非常感谢。

最好的问候

马丁

I have some dynamically created arrays that have varying lengths and I would like to resize them to the same 5000 element length by popping every n element.

Here is what I got so far:

import numpy as np
random_array = np.random.rand(26975,3)

n_to_pop = int(len(random_array) / 5000)
print(n)

If I do the downsampling with n (5) I get 5395 elements

I can do 5395 / 5000 = 1.07899, but I don't know how to calculate how often I should pop a element to remove the last 0.07899 elements.

If I can get within 5000-5050 length that would also be acceptable, then the remainder can be sacrificed with a simple .resize

This is probably just a simple math question, but I couldn't seem to find an answer anywhere.

Any help is much appreciated.

Best regards

Martin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾夏年华 2025-02-16 17:13:41

您可以使用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))

You can use something like np.linspace to make your solution as uniform as possible:

subset = random_array[np.round(np.linspace(0, len(random_array), 5000, endpoint=False)).astype(int)]

You don't always want to drop a uniform number of elements. Consider the case of reducing a 5003 element array to 5000 elements vs a 50003 element array. The trick is to create a set of elements to keep or drop that's as linear as possible in the index, which is exactly what np.linspace does.

You could also do something like

np.delete(random_array, np.round(np.linspace(0, len(random_array) len(random_array) - 5000, endpoint=False)).astype(int))
水水月牙 2025-02-16 17:13:41

您可以使用步骤解决方案使用np.random.choicenp.random。置换 as:

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]

You can use Step solution using np.random.choice or np.random.permutation as:

random_array[np.random.permutation(random_array.shape[0])[:5000]]

In case of near uniformly remove the rows, one way is:

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文