numpy.newaxis()

2022-09-03 20:49:37 浏览数 (1)

numpy.newaxis

A convenient alias for None, useful for indexing arrays.

See Also

numpy.doc.indexing

Examples

代码语言:javascript复制
>>> newaxis is None
True
>>> x = np.arange(3)
>>> x
array([0, 1, 2])
>>> x[:, newaxis]
array([[0],
[1],
[2]])
>>> x[:, newaxis, newaxis]
array([[[0]],
[[1]],
[[2]]])
>>> x[:, newaxis] * x
array([[0, 0, 0],
[0, 1, 2],
[0, 2, 4]])

Outer product, same as outer(x, y):

代码语言:javascript复制
>>> y = np.arange(3, 6)
>>> x[:, newaxis] * y
array([[ 0,  0,  0],
[ 3,  4,  5],
[ 6,  8, 10]])

x[newaxis, :] is equivalent to x[newaxis] and x[None]:

代码语言:javascript复制
>>> x[newaxis, :].shape
(1, 3)
>>> x[newaxis].shape
(1, 3)
>>> x[None].shape
(1, 3)
>>> x[:, newaxis].shape
(3, 1)

numpy.pi

pi = 3.1415926535897932384626433...

0 人点赞