three-dim array

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

three-dim array

Jeff
hello,

sorry newbe to numpy.

I want to define a three-dim array.
I know this works:

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

        [[5, 6],
         [7, 8]]])

But can you tell why this doesnt work?

 >>> np.array([[1,2],[[1,2],[3,4]]])
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.


Thank you.
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: three-dim array

Mark Alexander Mikofski
I believe numpy arrays must be rectangular, yours is jagged, instead try

>>> x3d = np.array([[[1, 2], [1, 2], [3, 4]]])
>>> x3d.shape
(1, 3, 2)

Note: 3 opening brackets, yours has 2
And single brackets around the 3 innermost arrays, yours has single brackets for the 1st, and double brackets around the 2nd and 3rd


On Tue, Dec 25, 2018, 6:20 PM Jeff <[hidden email] wrote:
hello,

sorry newbe to numpy.

I want to define a three-dim array.
I know this works:

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

        [[5, 6],
         [7, 8]]])

But can you tell why this doesnt work?

 >>> np.array([[1,2],[[1,2],[3,4]]])
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.


Thank you.
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: three-dim array

Eric Wieser

In the latest version of numpy, this runs without an error, although may or may not be what you want:

In [1]: np.array([[1,2],[[1,2],[3,4]]])
Out[1]:
array([[1, 2],
       [list([1, 2]), list([3, 4])]], dtype=object)

Here the result is a 2x2 array, where some elements are numbers and others are lists.


On Wed, 26 Dec 2018 at 06:23 Mark Alexander Mikofski <[hidden email]> wrote:
I believe numpy arrays must be rectangular, yours is jagged, instead try

>>> x3d = np.array([[[1, 2], [1, 2], [3, 4]]])
>>> x3d.shape
(1, 3, 2)

Note: 3 opening brackets, yours has 2
And single brackets around the 3 innermost arrays, yours has single brackets for the 1st, and double brackets around the 2nd and 3rd


On Tue, Dec 25, 2018, 6:20 PM Jeff <[hidden email] wrote:
hello,

sorry newbe to numpy.

I want to define a three-dim array.
I know this works:

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

        [[5, 6],
         [7, 8]]])

But can you tell why this doesnt work?

 >>> np.array([[1,2],[[1,2],[3,4]]])
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.


Thank you.
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: three-dim array

Benjamin Root
Ewww, kinda wish that would be an error... It would be too easy for a typo to get accepted this way.

On Wed, Dec 26, 2018 at 1:59 AM Eric Wieser <[hidden email]> wrote:

In the latest version of numpy, this runs without an error, although may or may not be what you want:

In [1]: np.array([[1,2],[[1,2],[3,4]]])
Out[1]:
array([[1, 2],
       [list([1, 2]), list([3, 4])]], dtype=object)

Here the result is a 2x2 array, where some elements are numbers and others are lists.


On Wed, 26 Dec 2018 at 06:23 Mark Alexander Mikofski <[hidden email]> wrote:
I believe numpy arrays must be rectangular, yours is jagged, instead try

>>> x3d = np.array([[[1, 2], [1, 2], [3, 4]]])
>>> x3d.shape
(1, 3, 2)

Note: 3 opening brackets, yours has 2
And single brackets around the 3 innermost arrays, yours has single brackets for the 1st, and double brackets around the 2nd and 3rd


On Tue, Dec 25, 2018, 6:20 PM Jeff <[hidden email] wrote:
hello,

sorry newbe to numpy.

I want to define a three-dim array.
I know this works:

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

        [[5, 6],
         [7, 8]]])

But can you tell why this doesnt work?

 >>> np.array([[1,2],[[1,2],[3,4]]])
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.


Thank you.
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: three-dim array

mattip
On 27/12/18 3:21 am, Benjamin Root wrote:

> Ewww, kinda wish that would be an error... It would be too easy for a
> typo to get accepted this way.
>
> On Wed, Dec 26, 2018 at 1:59 AM Eric Wieser
> <[hidden email] <mailto:wieser.eric%[hidden email]>>
> wrote:
>
>     In the latest version of numpy, this runs without an error,
>     although may or may not be what you want:
>
>     |In [1]: np.array([[1,2],[[1,2],[3,4]]]) Out[1]: array([[1, 2],
>     [list([1, 2]), list([3, 4])]], dtype=object) |
>
>     Here the result is a 2x2 array, where some elements are numbers
>     and others are lists.
>
>     ​
>

Specify the dtype explicitly: `dtype=int` or so, then NumPy will refuse
to create a ragged array.


There has been occasional discussion of `dtype='not-object'`, but I
don't think it resulted in an issue or PR.

Matti

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: three-dim array

Eric Wieser
In reply to this post by Benjamin Root
The rationale for the change allowing that construction was twofold: it's easier to understand what has gone wrong when seeing the `list`s in the repr than it was from the cryptic error message; and there were some jagged cases that already succeeded in this way, and it was less confusing to be consistent.

I agree that the behavior is not terribly useful, and object arrays constructed containing lists are quite possibly something we should warn about.

Eric

On Thu, Dec 27, 2018, 2:22 AM Benjamin Root <[hidden email] wrote:
Ewww, kinda wish that would be an error... It would be too easy for a typo to get accepted this way.

On Wed, Dec 26, 2018 at 1:59 AM Eric Wieser <[hidden email]> wrote:

In the latest version of numpy, this runs without an error, although may or may not be what you want:

In [1]: np.array([[1,2],[[1,2],[3,4]]])
Out[1]:
array([[1, 2],
       [list([1, 2]), list([3, 4])]], dtype=object)

Here the result is a 2x2 array, where some elements are numbers and others are lists.


On Wed, 26 Dec 2018 at 06:23 Mark Alexander Mikofski <[hidden email]> wrote:
I believe numpy arrays must be rectangular, yours is jagged, instead try

>>> x3d = np.array([[[1, 2], [1, 2], [3, 4]]])
>>> x3d.shape
(1, 3, 2)

Note: 3 opening brackets, yours has 2
And single brackets around the 3 innermost arrays, yours has single brackets for the 1st, and double brackets around the 2nd and 3rd


On Tue, Dec 25, 2018, 6:20 PM Jeff <[hidden email] wrote:
hello,

sorry newbe to numpy.

I want to define a three-dim array.
I know this works:

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

        [[5, 6],
         [7, 8]]])

But can you tell why this doesnt work?

 >>> np.array([[1,2],[[1,2],[3,4]]])
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.


Thank you.
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: three-dim array

Marten van Kerkwijk
There is a long-standing request to require an explicit opt-in for dtype=object: https://github.com/numpy/numpy/issues/5353

-- Marten


_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion