>>> import numpy as np
>>> a = np.arange(10).reshape(5,2)
>>> a
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
>>> a[:,0] = a[:,0] - 112
>>> a[:,1] = a[:,1] - 135
>>> a
array([[-112, -134],
[-110, -132],
[-108, -130],
[-106, -128],
[-104, -126]])
你可以直接用
np.subtract
:
>>> np.subtract(a, [112, 135])
array([[-112, -134],
[-110, -132],
[-108, -130],
[-106, -128],
[-104, -126]])
或者只是:
>>> a - [112, 135]
array([[-112, -134],
[-110, -132],
[-108, -130],
[-106, -128],
[-104, -126]])
注意,使用numpy,几乎不需要手动迭代每个元素。