user_docs
array
import numpy as np
array(object,dtype=None,copy=True,order='K',subok=False,ndmin=0)
# obj: data
# dtype: data type
# order: list
# ndmin: dimintion
nd = np.array((1,2,3,4))
nd.dtype
nd.ndim
np.array(([1,2,3,][1,2])) # dtype is object, not int32
-
order C
np.array([range(1,4),range(4,8)],order=‘C’)
1 | 2 | 3 | 4 |
---|---|---|---|
5 | 6 | 7 | 8 |
-
order F
np.array([range(1,4),range(4,8)],order=‘F’)
1 | 5 |
---|---|
2 | 6 |
3 | 7 |
4 | 8 |
ndarray
# create 2 x 2 array
np.ndarray((2,2), buffer=np.array(range(4)), dtype=int)
.shape
.size
.dtype
.data
.reshape(m,n)
# slice
a[:]
b[[0,1]]
b[::, 1]
b[[1,2],[1,2]]?
b[::,[1,4],b[::,::2]]
create ndarray ways
np.zeros()
np.zeros_like/ones_likes/empty_like(a, ...)
full(shape, fill, dtype, order)
arange(start, stop, step, stype)
linspace(start, stop, num=50)
random.rand/randn(d0, d1...)
random.randint(low, high, size)
# dtype: bool, float, int
# character code ('biufcmMOSUV')
np.typeDict
np.typecodes
np.typeNA
create data type
1. date: name, age, eg.[(‘sun’, 10), (‘yang’, 11)]
dt = np.dtype('U16','i4')
a = np.array([('sun', 10), ('yang', 11)], dtype=dt)
a['f0'] # silce
a['f1']
dt = np.dtype('3S8', '')
2. second way
3. third way
dt = np.dtype(['f0',[('f1','U2'),('f2', np.int16)]])
dt = np.dtype([('name', 'U16'),('age', 'i4')])
dt = np.dtype({'x': ('S2', 0), 'y': ('i4', 2)})
dt = np.dtype({'name': ['name', 'age'], 'formats': ['U16', 'i4']})
calculate
# broadcasting: + - * /
# boolean index
np.any
np.all
np.where(cond,[x,y])
& # 邏輯與
| # 邏輯或
np.all
np.arange(9).reshape(3,3)
# return an array representing the indices of a grid
np.indices(a.shape)
r, c = np.indices(a.shape)