首先,我使用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
接下来,我try 使用矩阵中的一个元素作为变量绘制结果,但出现了一个错误.
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()
对这个问题有什么 idea 吗?非常感谢你.