简体   繁体   English

为什么我会收到日志方法的“ufunc 循环不支持 numpy.ndarray 类型的参数 0”错误?

[英]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.

相关问题 为什么我会收到 numpy.exp 的“ufunc 循环不支持 int 类型的参数 0”错误? - Why do I get the 'loop of ufunc does not support argument 0 of type int' error for numpy.exp? ufunc 的到期循环中的错误不支持 str 类型的参数 0,该参数没有可调用的日志方法 - Error in due loop of ufunc does not support argument 0 of type str which has no callable log method TypeError: ufunc 的循环不支持没有可调用日志方法的 ArrayBox 类型的参数 0 - TypeError: loop of ufunc does not support argument 0 of type ArrayBox which has no callable log method 我想要 Plot Circle and its Solid Revolution (Sphere) 但得到 Error: loop of ufunc does not support argument 0 o - I want to Plot Circle and its Solid Revolution (Sphere) but get Error: loop of ufunc does not support argument 0 o 有谁知道如何处理这个错误? TypeError:不可散列的类型:'numpy.ndarray' - Does anyone know what to do with this error? TypeError: unhashable type: 'numpy.ndarray' 错误:需要浮点参数,而不是 numpy.ndarray - Error: float argument required, not numpy.ndarray “ufunc循环不支持Mul类型的参数0”是什么意思? - What does "loop of ufunc does not support argument 0 of type Mul" mean? ufunc 的循环不支持浮点类型的参数 0,它没有可调用的 exp 方法 - loop of ufunc does not support argument 0 of type float which has no callable exp method 类型错误:ufunc 的循环不支持没有可调用 sin 方法的 Add 类型的参数 0 - TypeError: loop of ufunc does not support argument 0 of type Add which has no callable sin method TypeError:ufunc 的循环不支持浮点类型的参数 0,它没有可调用的 exp 方法 - TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM