【问题标题】:Logical addressing numpy mess up with other matrices逻辑寻址 numpy 与其他矩阵混淆
【发布时间】:2013-10-18 11:45:44
【问题描述】:

我刚刚发现了一个问题,我不知道是不是故意的,还是我做错了。当我在 numpy 矩阵中使用逻辑寻址来更改矩阵的所有值时,例如等于 1。所有其他与该矩阵有关的矩阵也将被修改。

In [1]: import numpy as np
In [2]: from numpy import matrix as mtx
In [3]: A=mtx(np.eye(6))
In [4]: A
Out[4]: 
matrix([[ 1.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  1.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  1.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  1.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  1.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  1.]])

In [5]: B=A

In [6]: C=B

In [7]: D=C

In [8]: A[A==1]=5

In [9]: A
Out[9]: 
matrix([[ 5.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  5.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  5.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  5.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  5.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  5.]])

In [10]: B
Out[10]: 
matrix([[ 5.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  5.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  5.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  5.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  5.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  5.]])

In [11]: C
Out[11]: 
matrix([[ 5.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  5.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  5.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  5.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  5.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  5.]])

In [12]: D
Out[12]: 
matrix([[ 5.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  5.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  5.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  5.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  5.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  5.]])

谁能告诉我我做错了什么?这是一个错误吗?

【问题讨论】:

    标签: numpy matrix indexing addressing


    【解决方案1】:

    这不是错误。在python中说B=A意味着BA都指向同一个对象。您需要复制矩阵。

    >>> import numpy as np
    >>> from numpy import matrix as mtx
    >>> A = mtx(np.eye(6))
    >>> B = A.copy()
    >>> C = A
    
    #Check memory locations.
    >>> id(A)
    19608352
    >>> id(C)
    19608352    #Same object as A
    >>> id(B)
    19607992    #Different object then A
    
    >>> A[A==1] = 5
    >>> B   #B is a different object then A
    matrix([[ 1.,  0.,  0.,  0.,  0.,  0.],
            [ 0.,  1.,  0.,  0.,  0.,  0.],
            [ 0.,  0.,  1.,  0.,  0.,  0.],
            [ 0.,  0.,  0.,  1.,  0.,  0.],
            [ 0.,  0.,  0.,  0.,  1.,  0.],
            [ 0.,  0.,  0.,  0.,  0.,  1.]])
    
    >>> C   #C is the same object as A
    matrix([[ 5.,  0.,  0.,  0.,  0.,  0.],
            [ 0.,  5.,  0.,  0.,  0.,  0.],
            [ 0.,  0.,  5.,  0.,  0.,  0.],
            [ 0.,  0.,  0.,  5.,  0.,  0.],
            [ 0.,  0.,  0.,  0.,  5.,  0.],
            [ 0.,  0.,  0.,  0.,  0.,  5.]])
    

    使用 python list 可以看到同样的问题:

    >>> A = [5,3]
    >>> B = A
    >>> B[0] = 10
    >>> A
    [10, 3]
    

    请注意,这是 不同的,然后在这种情况下返回一个 numpy 视图:

    >>> A = mtx(np.eye(6))
    >>> B = A[0]  #B is a view and now points to the first row of A
    
    >>> id(A)
    28088720
    >>> id(B)  #Different objects!
    28087568  
    #B still points to the memory location of A's first row, but through numpy trickery
    
    >>> B
    matrix([[ 1.,  0.,  0.,  0.,  0.,  0.]])
    >>> B *= 5 #In place multiplication, updates B which is the same as A's first row
    >>> A
    matrix([[ 5.,  0.,  0.,  0.,  0.,  0.],
            [ 0.,  1.,  0.,  0.,  0.,  0.],
            [ 0.,  0.,  1.,  0.,  0.,  0.],
            [ 0.,  0.,  0.,  1.,  0.,  0.],
            [ 0.,  0.,  0.,  0.,  1.,  0.],
            [ 0.,  0.,  0.,  0.,  0.,  1.]])
    

    由于视图B 指向A 的第一行,A 发生了变化。现在让我们强制复制。

    >>> B = B*10 #Assigns B*10 to a different chunk of memory
    >>> A
    matrix([[ 5.,  0.,  0.,  0.,  0.,  0.],
            [ 0.,  1.,  0.,  0.,  0.,  0.],
            [ 0.,  0.,  1.,  0.,  0.,  0.],
            [ 0.,  0.,  0.,  1.,  0.,  0.],
            [ 0.,  0.,  0.,  0.,  1.,  0.],
            [ 0.,  0.,  0.,  0.,  0.,  1.]])
    >>> B
    matrix([[ 50.,   0.,   0.,   0.,   0.,   0.]])
    

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 2021-03-15
      • 2019-05-26
      • 2020-07-08
      • 2013-03-25
      • 2021-11-30
      • 2021-09-03
      • 2016-07-25
      • 2019-01-09
      相关资源
      最近更新 更多