首先,我使用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 吗?非常感谢你.

推荐答案

不能使用构造从变量t创建batch个矩阵e

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

由于[1-t, t][0.03, 0.97]具有不同的形状,因此这将创建参差不齐的array.相反,您可以通过重复[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维度中的其他变量,利用numpy broadcast进行批量计算

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)

Python相关问答推荐

Polars map_使用多处理对UDF进行批处理

语法错误:文档. evaluate:表达式不是合法表达式

数据框,如果值在范围内,则获取范围和

按条件添加小计列

高效生成累积式三角矩阵

Python:从目录内的文件导入目录

Django抛出重复的键值违反唯一约束错误

如何在Quarto中的标题页之前创建序言页

使用Scikit的ValueError-了解

ValueError:必须在Pandas 中生成聚合值

删除另一个div中的特定div容器

如何将参数名作为参数传入到函数中?

Python键盘模块不会立即检测到按键

在Pandas 中,有没有办法让元组作为索引运行得很好?

是否在DataFrame中将所有列设置为大写?

设置邮箱附件的文件类型

如何在Python中以一种安全的方式获取Git提交散列

Polars-to_Dicts().词典目录的顺序有保证吗?

如何计算数据集中的类别值并将求和转换为新的数据集?

棋类游戏的极大极小函数