1
import numpy as np  # specify import if you are using it with some other name

np.array([1, 2, 3]) -> array([1, 2, 3])

np.array([1, 2, 3])[None] -> array([[1, 2, 3]])

Notice the second lines has one additional dimension. Can someone explain this behavior to me? It's not multiplication. np.array([1, 2, 3])*[None] will raise an error.

2 Answers 2

3

This is exactly the same thing as np.array([1, 2, 3])[np.newaxis]. None simply happens to be the value chosen for newaxis; it could have been anything that isn't otherwise a meaningful slice or index value.

Sign up to request clarification or add additional context in comments.

1 Comment

I would normally use np.array([1,2,3])[None, :], making it clear that I am adding a new dimension at the start. numpy adds the trailing : automatically. But often in broadcasting this None isn't needed - it's added automatically.
0
>>> numpy.array([1,2,3])[0]  # find values at specific index in array
1
>>> numpy.array([1,2,3])[1]
2
>>> numpy.array([1,2,3])[None] # when you pass None as index it gives you whole list
array([[1, 2, 3]])

To your confusion when you are using numpy.array(<your-list>)*[value] this will multiply all the values in array with value. Here all values in your list is integer which can only be multiply with integer or float values. None is a NoneType object in python (to be precise it's my statement for python3 not much aware on this for python2) and you can not multiply integer values with None object. because if you process 2 * None it doesn't make any sense.

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.