1. 程式人生 > >Python講堂 np.newaxis 為 numpy.ndarray(多維陣列)增加一個軸

Python講堂 np.newaxis 為 numpy.ndarray(多維陣列)增加一個軸

https://blog.csdn.net/lanchunhui/article/details/49725065

>>> import numpy as np
>>> b = np.array(a)
>>> print(b)
[[1 2 3 4]
 [1 2 3 4]
 [1 2 3 4]]

>>> c=b[:,0]
>>> print(c)
[1 1 1]
>>> np.maximum(2,c)
array([2, 2, 2])

>>> np.maximum(2,c)[:,np.newaxis]
array([[2],
       [2],
       [2]])


>>> d=np.maximum(2,c)[:,np.newaxis]
>>> print(d)
[[2]
 [2]
 [2]]

>>> d[2]
array([2])

>>> e=np.maximum(2,c)
>>> print(e)
[2 2 2]

>>> e[2]
2