Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

Here's an example of behavior I cannot understand, maybe someone can share the insight into the logic behind it:

ccn = np.ones(1)
bbb = 7
bbn = np.array(bbb)
bbn * ccn # this is OK
    array([7.])
np.prod((bbn,ccn)) # but this is NOT
    Traceback (most recent call last):
    File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.2.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "<__array_function__ internals>", line 5, in prod
  File "C:\Users\...\venv\lib\site-packages\numpy\core\fromnumeric.py", line 2999, in prod
    return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
  File "C:\Users\...\venv\lib\site-packages\numpy\core\fromnumeric.py", line 87, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
numpy.VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray

Why? Why would a simple multiplication of two numbers be a problem? As far as formal algebra goes there's no dimensional problems, no datatype problems? The result is invariably also a single number, there's no chance it "suddenly" turn vector or object anything alike. prod(a,b) for a and b being scalars or 1by1 "matrices" is something MATLAB or Octave would eat no problem.

I know I can turn this error off and such, but why is it even and error?

like image 831
Igal Avatar asked May 19 '26 21:05

Igal


1 Answers

In [346]: ccn = np.ones(1)
     ...: bbb = 7
     ...: bbn = np.array(bbb)
In [347]: ccn.shape
Out[347]: (1,)
In [348]: bbn.shape
Out[348]: ()
In [349]: np.array((bbn,ccn))
<ipython-input-349-997419ba7a2f>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  np.array((bbn,ccn))
Out[349]: array([array(7), array([1.])], dtype=object)

You have arrays with different dimensions, that can't be combined into one numeric array.

That np.prod expression is actually:

np.multiply.reduce(np.array([bbn,ccn]))

can be deduced from your traceback.

In Octave both objects have shape (1,1), 2d

>> ccn = ones(1)
ccn =  1
>> ccn = ones(1);
>> size(ccn)
ans =

   1   1

>> bbn = 7;
>> size(bbn)
ans =

   1   1

>> [bbn,ccn]
ans =

   7   1

It doesn't have true scalars; everything is 2d (even 3d is a fudge on the last dimension).

And with 'raw' Python inputs:

In [350]: np.array([1,[1]])
<ipython-input-350-f17372e1b22d>:1: VisibleDeprecationWarning: ...
  np.array([1,[1]])
Out[350]: array([1, list([1])], dtype=object)

The object dtype array preserves the type of the inputs.

edit

prod isn't a simple multiplication. It's a reduction operation, like the big Pi in math. Even in Octave it isn't:

>> prod([[2,3],[3;4]])
error: horizontal dimensions mismatch (1x2 vs 2x1)
>> [2,3]*[3;4]
ans =  18
>> [2,3].*[3;4]
ans =

    6    9
    8   12

The numpy equivalent:

In [97]: np.prod((np.array([2,3]),np.array([[3],[4]])))
/usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py:87: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences...
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: could not broadcast input array from shape (2,1) into shape (2,)

In [98]: np.array([2,3])@np.array([[3],[4]])
Out[98]: array([18])
In [99]: np.array([2,3])*np.array([[3],[4]])
Out[99]: 
array([[ 6,  9],
       [ 8, 12]])

The warning, and here the error, is produced by trying to make ONE array from (np.array([2,3]),np.array([[3],[4]])).

like image 155
hpaulj Avatar answered May 22 '26 13:05

hpaulj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!