英文:
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]])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论