Syntax in Python (.T)

后端 未结 3 677
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 06:21

In the help resource for the multivariate normal sampling function in SciPy, they give the following example:

x,y = np.random.multivariate_normal(mean,cov,50         


        
相关标签:
3条回答
  • 2020-12-08 06:30

    Example

    import numpy as np
    a = [[1, 2, 3]]
    b = np.array(a).T  # ndarray.T The transposed array. [[1,2,3]] -> [[1][2][3]]
    print("a=", a, "\nb=", b)
    for i in range(3):
        print(" a=", a[0][i])  # prints  1 2 3
    for i in range(3):
        print(" b=", b[i][0])  # prints  1 2 3 
    
    0 讨论(0)
  • 2020-12-08 06:36

    The .T accesses the attribute T of the object, which happens to be a NumPy array. The T attribute is the transpose of the array, see the documentation.

    Apparently you are creating random coordinates in the plane. The output of multivariate_normal() might look like this:

    >>> np.random.multivariate_normal([0, 0], [[1, 0], [0, 1]], 5)  
    array([[ 0.59589335,  0.97741328],
           [-0.58597307,  0.56733234],
           [-0.69164572,  0.17840394],
           [-0.24992978, -2.57494471],
           [ 0.38896689,  0.82221377]])
    

    The transpose of this matrix is:

    array([[ 0.59589335, -0.58597307, -0.69164572, -0.24992978,  0.38896689],
           [ 0.97741328,  0.56733234,  0.17840394, -2.57494471,  0.82221377]])
    

    which can be conveniently separated in x and y parts by sequence unpacking.

    0 讨论(0)
  • 2020-12-08 06:48

    .T is just np.transpose(). Best of luck

    0 讨论(0)
提交回复
热议问题