为什么我要获得UFUNC的'不支持numpy.ndarray类型的参数0'对日志方法的错误?

发布于 2025-02-12 16:09:59 字数 1728 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2025-02-19 16:09:59

您不能使用构造中的变量t从变量e创建 batch

e = np.array([[1-t, t], [0.03, 0.97]])

因为这将由于而创建一个破烂的数组[1-T,T][0.03,0.97]具有不同的形状。相反,您可以通过重复[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)

之后,e将是2x2矩阵的 batch

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]], ...

最后,在 batch dimension中展开其他变量,以利用 numpy广播批量计算

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)

,仅在最后一个轴上汇总以获得每矩阵结果。

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)

You can't create a batch of matrices e from the variable t using the construct

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. 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.

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

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文