【问题标题】:Numpy, multiply 3x3 array by 3x10 array? [closed]Numpy,将 3x3 数组乘以 3x10 数组? [关闭]
【发布时间】:2016-05-01 21:55:02
【问题描述】:

我有一个 3x10 矩阵(以 numpy 数组的形式)并且想将它乘以一个 3x3 变换矩阵。我不认为 np.dot 正在做完整的矩阵乘法。有没有办法用数组做这种乘法?

transf = np.array([ [0.1, -0.4, 0],[0.9, 0.75, -0.1],[0.5, 0.75, -0.9] ])

one = [0,1,2,3,4,5,6,8,9]
two = [1,2,3,4,5,6,8,9,10]
three = [2,3,4,5,6,8,9,10,11]

data = np.array([ one, two, three ])

new_data = np.dot(transf,data)

有没有做整个矩阵乘法的点函数,而不仅仅是"For N dimensions it is a sum product over the last axis of a and the second-to-last of b"

【问题讨论】:

  • documentation 声明对于二维数组,np.dot 等价于矩阵乘法...

标签: python arrays numpy matrix


【解决方案1】:

transf 的最后两个条目中缺少逗号。修复它们,你会得到你期望的矩阵乘法:

# Missing commas between 0.75 and -0.1, 0.75 and -0.9.
transf = np.array([ [0.1, -0.4, 0],[0.9, 0.75 -0.1],[0.5, 0.75 -0.9] ])

# Fix with commas
transf = np.array([ [0.1, -0.4, 0],[0.9, 0.75, -0.1],[0.5, 0.75, -0.9]])

因为第一个数组实际上不是合法的二维数组,np.dot 不能执行矩阵乘法。

【讨论】:

    【解决方案2】:

    这只是通过* 运算符,但您需要定义matrix 而不是array

    import numpy as np
    transf = np.matrix([ [1,2,3],[4,5,6],[1,2,3] ])     # 3x3 matrix
    data = np.matrix([[2], [3], [4] ])      # 3x1 matrix
    
    print transf * data
    

    希望对你有帮助。

    【讨论】:

    • 矩阵 * 与数组点相同。
    • 而较新的 Python/numpys 有一个 @ 操作符,其作用方式相同。
    • @hpaulj 你有那个来源吗?不知道@在python里当算子
    • @KevinOrr @在python3.5中添加(详见PEP 465)...
    猜你喜欢
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 2022-07-12
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    相关资源
    最近更新 更多