如何选择不规则形状的NumPy数组中的第一个和最后一个元素

huangapple go评论56阅读模式
英文:

How to select the first and last elements in an irregularly shaped numpy array

问题

I have this example:

v = np.array([
    np.array([1, 1]),
    np.array([1, 2]),
    np.array([1, 3]),
    np.array([1, 4]),
    np.array([1, 5]),
    np.array([2, 1]),
    np.array([2, 2]),
    np.array([2, 3]),
    np.array([3, 1]),
    np.array([3, 2]),
    np.array([3, 3]),
    np.array([3, 4]),
    np.array([4, 1]),
    np.array([4, 2]),
    np.array([4, 3]),
    np.array([4, 4]),
    np.array([4, 5]),
    np.array([4, 6]),
])

k = np.split(v[:, 1], np.unique(v[:, 0], return_index=True)[1][1:])

My aim is to select the first and last element of each array in the output list. What I'd like to do is something like:

k = np.array(k, dtype=object)
new_k = (([:, 0], [:, -1]))

But alas, this is not possible. Maybe there is a way to rewrite the line that creates k to just have the first and last item?

Notice that I am trying to accomplish this with no list comprehension, defining functions, or loops - just "vanilla" numpy. If that's not feasible, any direction toward the next most efficient way of accomplishing this would be great.

英文:

I've been having a hard time finding an answer for this online, so I thought I'd query good ol' Stack Overflow.

I have this example:

v = np.array([
    np.array([1, 1]),
    np.array([1, 2]),
    np.array([1, 3]),
    np.array([1, 4]),
    np.array([1, 5]),
    np.array([2, 1]),
    np.array([2, 2]),
    np.array([2, 3]),
    np.array([3, 1]),
    np.array([3, 2]),
    np.array([3, 3]),
    np.array([3, 4]),
    np.array([4, 1]),
    np.array([4, 2]),
    np.array([4, 3]),
    np.array([4, 4]),
    np.array([4, 5]),
    np.array([4, 6]),
])

k = np.split(v[:, 1], np.unique(v[:, 0], return_index=True)[1][1:])

# output below

[
    np.array([1,2,3,4,5]),
    np.array([1,2,3]),
    np.array([1,2,3,4]),
    np.array([1,2,3,4,5,6])
]

My aim is to select the first and last element of each array in the output list. What I'd like to do is something like:

k = np.array(k, dtype=object)
new_k = (([:, 0], [:, -1]))

But alas, this is not possible. Maybe there is a way to rewrite the line that creates k to just have the first and last item?

Notice that I am trying to accomplish this with no list comprehension, defining functions, or loops - just "vanilla" numpy. If that's not feasible, any direction toward the next most efficient way of accomplishing this would be great.

答案1

得分: 1

np.unique` 操作选择了值的第一次出现在普通数组上这意味着它选择了每个组的第一个而在翻转后的数组上这意味着它选择了每个组的最后一个

```python
import numpy as np

v = np.array([
    [1, 1],
    [1, 2],
    [1, 3],
    [1, 4],
    [1, 5],
    [2, 1],
    [2, 2],
    [2, 3],
    [3, 1],
    [3, 2],
    [3, 3],
    [3, 4],
    [4, 1],
    [4, 2],
    [4, 3],
    [4, 4],
    [4, 5],
    [4, 6],
])

first_values = v[np.unique(v[:, 0], return_index=True)[1], 1]
last_values = v[::-1][np.unique(v[::-1, 0], return_index=True)[1], 1]

k = np.vstack((first_values, last_values)).T

这会得到所需的结果,

array([[1, 5],
       [1, 3],
       [1, 4],
       [1, 6]])
英文:

The np.unique operation selects the first occurrence of a value. Doing that with the normal array means it selects the first of each group, and doing the same for the reversed array means it selects the last of each group.

import numpy as np

v = np.array([
    [1, 1],
    [1, 2],
    [1, 3],
    [1, 4],
    [1, 5],
    [2, 1],
    [2, 2],
    [2, 3],
    [3, 1],
    [3, 2],
    [3, 3],
    [3, 4],
    [4, 1],
    [4, 2],
    [4, 3],
    [4, 4],
    [4, 5],
    [4, 6],
])

first_values = v[np.unique(v[:, 0], return_index=True)[1], 1]
last_values = v[::-1][np.unique(v[::-1, 0], return_index=True)[1], 1]

k = np.vstack((first_values, last_values)).T

This gives the desired result,

array([[1, 5],
       [1, 3],
       [1, 4],
       [1, 6]])

huangapple
  • 本文由 发表于 2023年6月15日 12:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定