2

So I've created a numpy array:

import numpy as np

a = np.array([[1,2,3],[4,5,6],[7,8,9]])

I'm trying to delete the end element of this array's subarray:

a[0] = (a[0])[:-1]

And encounter this issue:

a[0] = (a[0])[:-1] ValueError: could not broadcast input array from shape (2) into shape (3)

Why can't I change it ? How do I do it?

grzyb
  • 23
  • 4

4 Answers4

1

Given:

>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

You can do:

>>> a[:,0:2]
array([[1, 2],
       [4, 5],
       [7, 8]])

Or:

>>> np.delete(a,2,1)
array([[1, 2],
       [4, 5],
       [7, 8]])

Then in either case, assign that back to a since the result is a new array.

So:

>>> a=a[:,0:2]
>>> a
array([[1, 2],
       [4, 5],
       [7, 8]])

If you wanted only to delete 3 in the first row, that is a different problem. You can only do that if you have have an array of python lists since the sublists are not the same length.

Example:

>>> a = np.array([[1,2],[4,5,6],[7,8,9]])
>>> a
array([list([1, 2]), list([4, 5, 6]), list([7, 8, 9])], dtype=object)

If you do that, just stick to Python. You will have lost all the speed and other advantages of Numpy.


If by 'universal' you mean the last element of each row of a N x M array, just use .shape to find the dimensions:

>>> a
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> a.shape
(3, 4)
>>> np.delete(a,a.shape[1]-1,1)
array([[ 1,  2,  3],
       [ 5,  6,  7],
       [ 9, 10, 11]])

Or,

>>> a[:,0:a.shape[1]-1]
array([[ 1,  2,  3],
       [ 5,  6,  7],
       [ 9, 10, 11]])
dawg
  • 98,345
  • 23
  • 131
  • 206
  • Thanks! By the way, how do I make it universal? How can I get the number of elements of a subarray? – grzyb Jun 28 '20 at 00:23
  • Define 'universal' in this case? Do you mean given an 2D array `X x Y` dimensions to delete the last element of each row? – dawg Jun 28 '20 at 00:26
  • Do you mean given an 2D array X x Y dimensions to delete the last element of each row? Yes – grzyb Jun 28 '20 at 00:37
1
>>> a = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> type(a)
<class 'numpy.ndarray'>
>>> a.shape
(3, 3)

The variable a is matrix (2D array). It has certain number of rows and columns. In a matrix all the rows must be of same length. As so, in the above example, the matrix cannot be formed if the first row has length 2 and others 3. So deleting the last element of only the first(or any other subset) sub-array is not possible.

Instead you have to delete the last element of all the sub-arrays at the same time.

That can be done as

>>> a[:,0:2]
array([[1, 2],
       [4, 5],
       [7, 8]])

Or,

>>> np.delete(a,2,1)
array([[1, 2],
       [4, 5],
       [7, 8]])

This also applies to the elements of other positions. Deleting can be done of any element of the sub-arrays keeping in mind that all the sub-arrays should have same length.

However you can manipulate the last element(or any other) of any sub-array unless the shape remains constant.

>>> a[0][-1] = 19
>>> a
array([[ 1,  2, 19],
       [ 4,  5,  6],
       [ 7,  8,  9]])

In case you try to form a matrix with rows of unequal length, a 1D array of lists is formed on which no Numpy operations like vector processing, slicing, etc. works (the list operation works)

>>> b = np.array([[1,2,3],[1,2,3]])
>>> c = np.array([[1,2],[1,2,3]])

>>> b
array([[1, 2, 3],
       [1, 2, 3]])
>>> b.shape
(2, 3)

>>> c
array([list([1, 2]), list([1, 2, 3])], dtype=object)
>>> c.shape
(2,)

>>> print(type(b),type(c))
<class 'numpy.ndarray'> <class 'numpy.ndarray'>

Both are ndarray, but you can see the second variable c has is a 1D array of lists.

>>> b+b
array([[2, 4, 6],
       [2, 4, 6]])
>>> c+c
array([list([1, 2, 1, 2]), list([1, 2, 3, 1, 2, 3])], dtype=object)

Similarly, b+b operation performs the element-wise addition of b with b, but c+c performs the concatenation operation among the two lists.

For Further Ref

How to make a multidimension numpy array with a varying row size?

0

Here is how:

import numpy as np

a = np.array([[1,2,3],[4,5,6],[7,8,9]])
a = a[:-1]

print(a)

Output:

[[1 2 3]
 [4 5 6]]
Red
  • 26,798
  • 7
  • 36
  • 58
0

It is solved quite easily with the help of list comprehension: [a[0:-1] for a in array]

For example, there are input data:

Input:

array = [[1,2,3,4],[5,6,7],[8,9]]


print([a[0:-1] for a in array])

Output:

[[1,2,3],[5,6],[8]]

Loop iteration occurs:

for a in array

In each iteration, a[0:-1] is executed and the result is added to a new array.

InsafProg
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 07 '23 at 15:03