Are*_*Tor 4 numpy multidimensional-array exp python-3.x numpy-ndarray
在标题中,我需要numpy.exp在一个非常大的ndarray上执行,比方说ar,并将结果存储在ar自身中.这项操作可以就地进行吗?
您可以使用以下可选out参数exp:
a = np.array([3.4, 5])
res = np.exp(a, a)
print(res is a)
print(a)
Run Code Online (Sandbox Code Playgroud)
输出:
True
[ 29.96410005 148.4131591 ]
Run Code Online (Sandbox Code Playgroud)
exp(x [,out])
计算输入数组中所有元素的指数.
返回
out:ndarray输出数组,元素指数
x.
这里的所有元素都a将被结果取代exp.返回值res与a.相同.没有创建新数组
迈克·穆勒的答案是好的,但请注意,如果您的阵列类型为int32,int,int64等等,它会抛出一个TypeError。因此,一种安全的方法是在执行类似操作之前将数组类型转换为float64或float32等,exp
In [12]: b
Out[12]: array([1, 2, 3, 4, 5], dtype=int32)
In [13]: np.exp(b, b)
--------------------------------------------------------------------------
TypeError: ufunc 'exp' output (typecode 'd') could not be coerced to provided
output parameter (typecode 'i') according to the casting rule ''same_kind''
Run Code Online (Sandbox Code Playgroud)
类型转换 & exp:
# in-place typecasting
In [14]: b = b.astype(np.float64, copy=False)
In [15]: b
Out[15]: array([ 1., 2., 3., 4., 5.], dtype=float64)
# modifies b in-place
In [16]: np.exp(b, b)
Out[16]: array([ 2.718, 7.389, 20.086, 54.598, 148.413], dtype=float64)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1184 次 |
| 最近记录: |