gpt4 book ai didi

python - 矩阵和向量中的除法和乘法序列

转载 作者:太空宇宙 更新时间:2023-11-04 02:52:31 25 4
gpt4 key购买 nike

我有(在 python 中)以下操作(对矩阵和向量使用 numpy):

result = (A.dot(input)/b)

使用A 矩阵和binput 向量。 Ab是固定的,input是变化的。因此,我想将 Ab 简化为单个元素,向量或矩阵,然后可以通过乘法或点积与输入向量组合.可惜我不会写

result = (A/b).dot(input)

之后会产生错误的值。我怎样才能将 Ab 连接成一个元素?

最佳答案

使用 None/np.newaxisb 扩展为 2D然后将 A 除以它:

Ab = A/b[:,None]

然后通过矩阵乘法在这些迭代中重复使用 Ab,其中您将 input 作为唯一变量:

Ab.dot(input)

作为旁注,尽量避免变量名也是 Python 内置函数名,在本例中是 input

sample 运行-

In [164]: A = np.random.rand(4,5)

In [165]: input1 = np.random.rand(5)

In [166]: b = np.random.rand(4)

In [167]: (A.dot(input1)/b)
Out[167]: array([ 2.80446671, 4.49821539, 3.73365285, 1.83176278])

In [168]: Ab = A/b[:,None]

In [169]: Ab.dot(input1)
Out[169]: array([ 2.80446671, 4.49821539, 3.73365285, 1.83176278])

关于python - 矩阵和向量中的除法和乘法序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43425983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com