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.