【问题标题】:Reversing output matrix values on Numpy! Is there a specific command for this?在 Numpy 上反转输出矩阵值!是否有特定的命令?
【发布时间】:2020-03-04 16:24:09
【问题描述】:

当我计算 Vandermonde 时,我得到了正确的答案 这个矩阵的系数。但是,输出矩阵是相反的。 它应该是[6,-39,55,27] 而不是[27,55,-39,6]

我的 Vandermonde 矩阵的输出被翻转,最终解决方案 c,被翻转了。

import numpy as np
from numpy import linalg as LA


x = np.array([[4],[2],[0],[-1]])
f = np.array([[7],[29],[27],[-73]])

def main():

    A_matrix = VandermondeMatrix(x)
    print(A_matrix)
    c = LA.solve(A_matrix,f) #coefficients of Vandermonde Polynomial
    print(c)

def VandermondeMatrix(x):
    n = len(x)
    A = np.zeros((n, n))
    exponent = np.array(range(0,n))
    for j in range(n):
        A[j, :] = x[j]**exponent
    return A





if __name__ == "__main__":
    main()

【问题讨论】:

    标签: python python-3.x numpy math matrix


    【解决方案1】:

    只需从一开始就将指数range反过来,然后就不必再翻转以减少运行时间:

    def VandermondeMatrix(x):
        n = len(x)
        A = np.zeros((n, n))
        exponent = np.array(range(n-1,-1,-1))
        for j in range(n):
            A[j, :] = x[j]**exponent
        return A
    

    输出:

    #A_matrix:
    [[64. 16.  4.  1.]
     [ 8.  4.  2.  1.]
     [ 0.  0.  0.  1.]
     [-1.  1. -1.  1.]]
    
    #c:
    [[  6.]
     [-39.]
     [ 55.]
     [ 27.]]
    

    【讨论】:

    • exponent = np.arange(n)[::-1] 更短 :) 具有负跨度的数组反转并不是一项昂贵的操作,无论它在哪里完成。
    • 我知道,但这是一个不必要的操作,如果不需要,为什么还要这样做呢? ;)
    【解决方案2】:

    np.flip(c)?

    link to documentation

    【讨论】:

      【解决方案3】:

      你可以 print(c[::-1]) 这将颠倒 c 的顺序。 来自How can I flip the order of a 1d numpy array?

      【讨论】:

        【解决方案4】:
        In [3]: def VandermondeMatrix(x): 
           ...:     n = len(x) 
           ...:     A = np.zeros((n, n)) 
           ...:     exponent = np.array(range(0,n)) 
           ...:     for j in range(n): 
           ...:         A[j, :] = x[j]**exponent 
           ...:     return A 
           ...:                                                                                        
        In [4]: x = np.array([[4],[2],[0],[-1]])                                                       
        In [5]: VandermondeMatrix(x)                                                                   
        Out[5]: 
        array([[ 1.,  4., 16., 64.],
               [ 1.,  2.,  4.,  8.],
               [ 1.,  0.,  0.,  0.],
               [ 1., -1.,  1., -1.]])
        In [6]: f = np.array([[7],[29],[27],[-73]])                                                    
        In [7]: np.linalg.solve(_5,f)                                                                  
        Out[7]: 
        array([[ 27.],
               [ 55.],
               [-39.],
               [  6.]])
        

        结果是一个 (4,1) 数组;反向行:

        In [9]: _7[::-1]                                                                               
        Out[9]: 
        array([[  6.],
               [-39.],
               [ 55.],
               [ 27.]])
        

        负步幅,[::-1] 索引也用于反转 Python 列表和字符串。

        In [10]: ['a','b','c'][::-1]                                                                   
        Out[10]: ['c', 'b', 'a']
        

        【讨论】:

        • 所以你基本上回答了与 mkusz 相同的答案,但有很多不必要的中间结果
        • @LeoE,我测试了 OP 代码,部分原因是我想知道他为什么得到相反的答案。我看到他得到了 2d 答案(而不是问题所暗示的 1d),但你深入挖掘了。无论如何,我的回答往往是冗长的,旨在理解,而不仅仅是立即解决。
        猜你喜欢
        • 2011-08-27
        • 2013-04-22
        • 1970-01-01
        • 2021-03-17
        • 1970-01-01
        • 1970-01-01
        • 2019-08-10
        • 2020-04-08
        • 2021-12-14
        相关资源
        最近更新 更多