代码之家  ›  专栏  ›  技术社区  ›  mattsmith5

Python Numpy arctan2星号和.T的含义

  •  0
  • mattsmith5  · 技术社区  · 5 年前

    这是什么意思

    1. arctan2后面的星号代表什么?
    2. 什么意思?不是吗?

    只是想理解这些语法。

    https://numpy.org/doc/stable/reference/generated/numpy.arctan2.html

    angles = np.rad2deg(np.arctan2(*(np.tile(center, (len(pts), 1)) - pts).T))
    
    2 回复  |  直到 5 年前
        1
  •  1
  •   Mayank Porwal    5 年前

    经过:

    1. Asteriks in Python 了解args的解包是如何发生的。
    2. numpy.Transpose
        2
  •  0
  •   hpaulj    5 年前

    猜猜是什么 pts center 可能看起来像:

    In [322]: pts = np.array([[0,0],[1,1],[1,2],[2,1]])
    In [323]: center = np.array([.5,.5])
    

    表达式的内部是:

    In [324]: np.tile(center, (len(pts), 1)) - pts
    Out[324]: 
    array([[ 0.5,  0.5],
           [-0.5, -0.5],
           [-0.5, -1.5],
           [-1.5, -0.5]])
    

    换位,打开包装:

    In [325]: x, y = (np.tile(center, (len(pts), 1)) - pts).T
    In [326]: x
    Out[326]: array([ 0.5, -0.5, -0.5, -1.5])
    In [327]: y
    Out[327]: array([ 0.5, -0.5, -1.5, -0.5])
    

    所以完整的表达是:

    In [328]: np.arctan2(*(np.tile(center, (len(pts), 1)) - pts).T)
    Out[328]: array([ 0.78539816, -2.35619449, -2.8198421 , -1.89254688])
    

    同:

    In [329]: np.arctan2(x,y)
    Out[329]: array([ 0.78539816, -2.35619449, -2.8198421 , -1.89254688])
    

    但我们不需要 tile 减去 临时秘书处 居中 :

    In [333]: center-pts
    Out[333]: 
    array([[ 0.5,  0.5],
           [-0.5, -0.5],
           [-0.5, -1.5],
           [-1.5, -0.5]])
    

    居中 是(2,)形, 临时秘书处

    In [341]: temp = center-pts
    In [343]: np.arctan2(temp[:,0],temp[:,1])
    Out[343]: array([ 0.78539816, -2.35619449, -2.8198421 , -1.89254688])