Python(.T)中的语法 [英] Syntax in Python (.T)
问题描述
在SciPy中用于多元正态采样函数的帮助资源中,他们给出了以下示例:
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,5000).T
我的问题很基本:最终的.T实际做什么?
My question is rather basic: what does the final .T actually do?
非常感谢,我知道这很简单,但是很难在Google中找到".T".
Thanks a lot, I know it is fairly simple, but it is hard to look in Google for ".T".
推荐答案
.T
访问对象的属性T
,该属性恰好是NumPy数组. T
属性是数组的转置,请参见
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.
显然,您是在平面上创建随机坐标. multivariate_normal()
的输出可能如下所示:
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]])
此矩阵的转置为:
array([[ 0.59589335, -0.58597307, -0.69164572, -0.24992978, 0.38896689],
[ 0.97741328, 0.56733234, 0.17840394, -2.57494471, 0.82221377]])
可以通过序列解包方便地分为x
和y
部分.
which can be conveniently separated in x
and y
parts by sequence unpacking.
这篇关于Python(.T)中的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!