0

So. I am trying to access and modify the penultimate individual element of a series of Numpy arrays.

array_1D = np.array([10,11,12,13, 14])
array_2D = np.array([[20,30,40,50,60], [43,54,65,76,87], [11,22,33,44,55]])
array_3D = np.array([[[1,2,3,4,5], [11,21,31,41,51]], [[11,12,13,14,15], [51,52,53,54,5]]])
arrays = [array_1D, array_2D, array_3D]

I wanted to do this programmatically as opposed to manually, to learn a bit deeper.

What I have is:

for array in arrays:
    print(array)  # Before
    print('CHANGE TEN')
    array[..., -2] = 10
    print(array, end='\n--------\n\n')  # After

Which returns:

[10 11 12 13 14]
CHANGE TEN
[10 11 12 10 14]
--------

[[20 30 40 50 60]
 [43 54 65 76 87]
 [11 22 33 44 55]]
CHANGE TEN
[[20 30 40 10 60]
 [43 54 65 10 87]
 [11 22 33 10 55]]
--------

[[[ 1  2  3  4  5]
  [11 21 31 41 51]]

 [[11 12 13 14 15]
  [51 52 53 54  5]]]
CHANGE TEN
[[[ 1  2  3 10  5]
  [11 21 31 10 51]]

 [[11 12 13 10 15]
  [51 52 53 10  5]]]
--------

As you can see this syntax currently accesses the penultimate individual element of all elements along the major axis.

Is there a neat way to access the penultimate individual element of an array of arbitrary dimension? (In this case that would mean a single integer.)

Strange application I know. I'm interested in understanding the limitations germane to working with arrays of arbitrary dimension.

(I googled a bit and searched SO to no avail. Google Fu not strong apparently.)

Gheko
  • 21
  • 3
  • the reason the searches fail is that this isn't a common task, or even a rare one. It's unique. – hpaulj Oct 23 '21 at 14:50
  • `penultimate individual element` is a bit vague. Are you imagining the array as a flat, 1d, one? `arr.ravel()[-2]`? – hpaulj Oct 23 '21 at 15:52
  • @hpaulj Oh nice! Ok so this is just not a prescribed use of the various pieces I'm trying to put together, that's comforting. Thank you! Regarding the terminology I used I meant to contrast the elements of an array (the term used for generic elements at the various "levels" of an array) and individual elements (the actual units of data stored at the "bottom" of any given "path" which terminates the depth of possible search. So ints, floats, strings, etc.). Apologies for the confusion. Is this not the general terminology for such elements? If not how are they typically referred to? – Gheko Oct 24 '21 at 11:17
  • @hpaulj Just remembered you asked about whether I meant imagining the array as a 1D array. Yes! That is exactly what I'm thinking. – Gheko Oct 24 '21 at 12:12
  • Just checked the docs on .ravel() . That is EXACTLY what I was looking for. You're a champion. If you want to make it into an answer I'll mark it as the correct solution <3 – Gheko Oct 24 '21 at 12:14

1 Answers1

0

Here you go:

array_1D = np.array([10,11,12,13, 14])
array_2D = np.array([[20,30,40,50,60], [43,54,65,76,87], [11,22,33,44,55]])
array_3D = np.array([[[1,2,3,4,5], [11,21,31,41,51]], [[11,12,13,14,15], [51,52,53,54,5]]])
arrays = [array_1D, array_2D, array_3D]

for array in arrays:
    print(array)  # Before
    print('CHANGE TEN')
    array[tuple([-2] * array.ndim)] = 10
    print(array, end='\n--------\n\n')  # After

print(array_1D[-2], array_2D[-2,-2], array_3D[-2,-2,-2])

Output:

10 10 10
sehan2
  • 1,700
  • 1
  • 10
  • 23
  • Thank you for the answer! This doesn't actually produce what I am looking to do however. This is likely due to the contrived nature of the exercise and the unclear language I've used to ask it :/ So hands up on my side for that. I'd be looking for a way to have the following statements produce 10s. print(array_1D[-2], array_2D[-1,-2], array_3D[-1,-1,-2]) OR print(array_1D[-2], array_2D[:,-2], array_3D[:,:,-2]) – Gheko Oct 24 '21 at 11:24
  • But at this point I think I have my answer. This is such an obscure thing to implement that an actually functional answer is not something I require. I posted the question to learn if there was in fact a relatively known way to do this that I was simply unaware of. My conclusion is that there is not, due to it being an unneeded function in another wise well structured program and if I find myself needing such an implementation I will seek to find other, deeper issues with my approach elsewhere in the code. You guys have been super helpful, I really appreciate it! – Gheko Oct 24 '21 at 11:33