指定1D数组的括号是否像[[ ]]这样做会对结构造成任何改变吗?

huangapple go评论83阅读模式
英文:

Does specifying brackets for 1D arrays like [[ ]] make any change in the structure?

问题

以下是要翻译的内容:

这个p = np.array([[mu, sig]])与仅指定np.array([mu, sig])有何不同?

英文:

Does this p = np.array([[mu, sig]]) make any change than just specifying np.array([mu, sig])?

答案1

得分: 2

是的,这两个表达式有区别。

[mu, sig] 是一个包含两个元素(musig)的数组。

[[mu, sig]] 是一个包含一个数组的数组。那个数组就是 [mu, sig],它也是一个包含两个元素(musig)的数组。所以 [[mu, sig]] 是一个二维数组(即数组的数组)。

编辑: 我最初说[[mu, sig]]是一个包含一个元素的数组。虽然这是我对多维数组的理解方式,但由于这并不完全正确,这在评论中引发了讨论。[[mu, sig]]仍然有两个元素,只是它们以二维的方式组织起来。

[[mu, sig]]只有一个元素就像说以下矩阵只有两个元素一样:

[ [1, 2, 3],
  [4, 5, 6] ]

鉴于此,为了不让任何人感到困惑,我编辑了我的答案以澄清:[[mu, sig]]是一个包含一个数组的数组。不能完全正确地说它只有一个元素。

英文:

Yes, there is a difference between the two expressions.

[mu, sig] is an array with two elements (mu and sig).

[[mu, sig]] is an array with one array inside it. That one array is [mu, sig], an array with two elements (mu and sig). So [[mu, sig]] is a two dimensional array (i.e. an array of arrays).

Edit: I originally said that [[mu, sig]] is an array with one element inside it. While that is the way I think about multi-dimensional arrays, it sparked a discussion in the comments because that is not 100% correct. [[mu, sig]] still has two elements, only that they are organized in a two dimensional fashion.

Saying that [[mu, sig]] has one element would be like saying that the following matrix has only two elements:

[ [1, 2, 3],
  [4, 5, 6] ]

In light of this, and in order not to confuse anyone, I've edited my answer to clarify: [[mu, sig]] is an array with one array inside it. It is not fully correct to say that it has only one element inside it.

huangapple
  • 本文由 发表于 2020年1月3日 19:46:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定