Skip to content
scipython.com

Q6.1.2Creating a NumPy array #1

Why doesn't this create a two-dimensional array?

>>> np.array((1,0,0), (0,1,0), (0,0,1), dtype=float)

What is the correct way?

Show solution

To create a 2-dimensional array, array() must be passed a sequence of sequences as a single argument: this call passes three sequence arguments instead. The correct call is

>>> np.array( ((1,0,0), (0,1,0), (0,0,1)) , dtype=float)