I need a function to get last elements along an axis in numpy array.
For example, If I have a array,
a = np.array([1, 2, 3])
The function should work like
get_last_elements(a, axis=0)
>>> [3]
get_last_elements(a, axis=1)
>>> [1, 2, 3]
This function needs to work for multidimensional array too:
b = np.array([[1, 2],
[3, 4]])
get_last_elements(b, axis=0)
>>> [[2],
[4]]
get_last_elements(b, axis=1)
>>> [3, 4]
Does anyone have a good idea to achieve it?
get_last_elements(a, axis=1)to work for the first example? Sinceais of shape(3,), not(1, 3).np.takemay be useful.np.atleast_2dmay help when dealing with a 1d input.