0

What is the difference between these two numpy arrays?

array([array([1,2,3]),array([4,5,6])])

and

array([[1,2,3],[4,5,6]])

How can we convert one to other?

4 Answers 4

5

The result is the same. There's no need to convert anything:

A = np.array([np.array([1,2,3]), np.array([4,5,6])])
B = np.array([[1,2,3], [4,5,6]])

assert np.array_equal(A, B)
Sign up to request clarification or add additional context in comments.

Comments

0

The result would be the same, but the standard would normally be:

array([[1,2,3],[4,5,6]])

4 Comments

How is this the standard way?
You come across this more often, more recognisable, if you will
That doesn't make it a standard.
I'm not specifically saying that it's a standard in that sense, I'm stating that you're more likely to come across this version than the other
0

As per the docs

numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)

Parameters: object : array_like

  • An array, any object exposing the array interface, an object whose array method returns an array, or any (nested) sequence.

This means using:

array([array([1,2,3]),array([4,5,6])])

Is just redundant to:

array([[1,2,3],[4,5,6]])

As Numpy accepts nested lists (arrays) and will handle them accordingly.

Comments

0

These are just equivalent ways to create an array.

From the doc to np.array:

numpy.array(object, ...

object : array_like

    An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence

What you passed are both correct ways to initialize an array. Your first option is a nested sequence, the second one is a nested list.

.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.