为什么我要获得UFUNC的'不支持numpy.ndarray类型的参数0'对日志方法的错误?
首先,我使用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)
结果:
0.14538525
0.45687371996485304
接下来,我尝试使用矩阵中的一个元素作为变量来绘制结果,但发生了错误。
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()
它显示以下错误:
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
以下代码没有问题,因此我认为使用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()
这个问题有什么想法吗?非常感谢。
First, I used np.array
to perform operations on multiple matrices, and it was successful.
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.
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
.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用构造中的变量
t
从变量e
创建 batch ,因为这将由于
而创建一个破烂的数组[1-T,T]
和[0.03,0.97]
具有不同的形状。相反,您可以通过重复[0.03,0.97]
来创建e
,以匹配[1-T,T]
的形状,然后堆叠它们在一起如下。之后,
e
将是2x2矩阵的 batch最后,在 batch dimension中展开其他变量,以利用 numpy广播批量计算
,仅在最后一个轴上汇总以获得每矩阵结果。
You can't create a batch of matrices
e
from the variablet
using the constructas this would create a ragged array due to
[1-t, t]
and[0.03, 0.97]
having different shapes. Instead, you can createe
by repeating[0.03, 0.97]
to match the shape of[1-t, t]
, then stack them together as follows.After this,
e
will be a batch of 2x2 matricesFinally, expand other variables in the batch dimension to take advantage of numpy broadcast to batch the calculation
and only sum across the last axis to get per-matrix result.