使用长度为1的数组才行,比如:
x = array([1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9])
y = np.int(x)
TypeError: only size-1 arrays can be converted to Python scalars
用np.vectorize
使得函数np.int
以广播的形式作用在x
上:
vectorize(pyfunc, otypes=None, doc=None, excluded=None, cache=False,
signature=None)
其中:
pyfunc : callable
A python function or method.
使用:
f2 = np.vectorize(np.int)#让函数矩阵化,
f2(x)
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])