[英]Why do I get the 'loop of ufunc does not support argument 0 of type numpy.ndarray' error for log method?
First, I used np.array
to perform operations on multiple matrices, and it was successful.首先,我使用
np.array
对多个矩阵进行操作,并且成功了。
import numpy as np import matplotlib.pyplot as plt f = np.array([[0.35, 0.65]]) e = np.array([[0.92, 0.08], [0.03, 0.97]]) r = np.array([[0.95, 0.05], [0.06, 0.94]]) d = np.array([[0.99, 0.01], [0.08, 0.92]]) c = np.array([[0, 1], [1, 0]]) D = np.sum(f@(e@r@d*c)) u = f@e I = np.sum(f@(e*np.log(e/u))) print(D) print(I)
Outcome:结果:
0.14538525 0.45687371996485304
Next, I tried to plot the result using one of the elements in the matrix as a variable, but an error occurred.接下来,我尝试将 plot 的结果使用矩阵中的一个元素作为变量,但是发生了错误。
import numpy as np import matplotlib.pyplot as plt t = np.arange(0.01, 0.99, 0.01) f = np.array([[0.35, 0.65]]) e = np.array([[1-t, t], [0.03, 0.97]]) r = np.array([[0.95, 0.05], [0.06, 0.94]]) d = np.array([[0.99, 0.01], [0.08, 0.92]]) c = np.array([[0, 1], [1, 0]]) D = np.sum(f@(e@r@d*c)) u = f@e I = np.sum(f@(e*np.log(e/u))) plt.plot(t, D) plt.plot(t, I) plt.show()
It shows the error below:它显示以下错误:
AttributeError Traceback (most recent call last) AttributeError: 'numpy.ndarray' object has no attribute 'log' The above exception was the direct cause of the following exception: TypeError Traceback (most recent call last) <ipython-input-14-0856df964382> in <module>() 10 11 u = f@e ---> 12 I = np.sum(f@(e*np.log(e/u))) 13 14 plt.plot(t, D) TypeError: loop of ufunc does not support argument 0 of type numpy.ndarray which has no callable log method
There was no problem with the following code, so I think there was something wrong with using np.array
.以下代码没有问题,所以我认为使用
np.array
有问题。
import numpy as np import matplotlib.pyplot as plt t = np.arange(0.01, 0.99, 0.01) y = np.log(t) plt.plot(t, y) plt.show()
Any idea for this problem?对这个问题有什么想法吗? Thank you very much.
非常感谢。
You can't create a batch of matrices e
from the variable t
using the construct您不能使用构造从变量
t
创建一批矩阵e
e = np.array([[1-t, t], [0.03, 0.97]])
as this would create a ragged array due to [1-t, t]
and [0.03, 0.97]
having different shapes.因为这会创建一个参差不齐的数组,因为
[1-t, t]
和[0.03, 0.97]
具有不同的形状。 Instead, you can create e
by repeating [0.03, 0.97]
to match the shape of [1-t, t]
, then stack them together as follows.相反,您可以通过重复
[0.03, 0.97]
来创建e
以匹配[1-t, t]
的形状,然后将它们堆叠在一起,如下所示。
t = np.arange(.01,.99,.01) # shape (98,) _t = np.stack([t, 1-t], axis=1) # shape (98, 2) e = np.array([[.03,.97]]) # shape (1, 2) e = np.repeat(e, len(ts), axis=0) # shape (98, 2) e = np.stack([_t, e], axis=1) # shape (98, 2, 2)
After this, e
will be a batch of 2x2 matrices在此之后,
e
将是一批2x2 矩阵
array([[[0.01, 0.99], [0.03, 0.97]], [[0.02, 0.98], [0.03, 0.97]], [[0.03, 0.97], [0.03, 0.97]], [[0.04, 0.96], [0.03, 0.97]],...
Finally, expand other variables in the batch dimension to take advantage of numpy broadcast to batch the calculation最后,扩展batch维度中的其他变量,利用numpy广播进行batch计算
f = np.array([[0.35, 0.65]])[None,:] # shape (1,1,2) r = np.array([[0.95, 0.05], [0.06, 0.94]])[None,:] # shape (1,2,2) d = np.array([[0.99, 0.01], [0.08, 0.92]])[None,:] # shape (1,2,2) c = np.array([[0, 1], [1, 0]])[None,:] # shape (1,2,2)
and only sum across the last axis to get per-matrix result.并且仅在最后一个轴上求和以获得每个矩阵的结果。
D = np.sum(f@(e@r@d*c), axis=-1) # shape (98, 1) u = f@e I = np.sum(f@(e*np.log(e/u)), axis=-1) # shape (98, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.