X [:,0]中逗号的含义

问题描述 投票:1回答:2

如果X是一个数组,那么X[:,0]是什么意思?事实上,这不是我第一次看到这样的事情,这让我感到困惑,但我看不出它的含义是什么?任何人都可以给我一个例子吗?在这个逗号问题上,我们将会得到一个完整明确的答案。

请参阅文件https://github.com/lazyprogrammer/machine_learning_examples/blob/master/ann_class/forwardprop.py

提前致谢!

python comma
2个回答
3
投票
>>> x = [1, 2, 3]
>>> x[:, 0] Traceback (most recent call last):
    File "<stdin>", line 1, in <module>  TypeError: list indices must be integers, not tuple

如果你看到了,那么变量不是列表,而是其他东西。也许是一个numpy数组。


0
投票

砖块内的逗号将行与要从阵列中滑动的列分开。

x[row,column]

您可以在行和列值之前或之后放置“:”。在值之前它表示“unitl”,在值之后它表示“from”。

例如,你有:

x: array([[5.1, 3.5, 1.4, 0.2],
          [4.9, 3. , 1.4, 0.2],
          [4.7, 3.2, 1.3, 0.2],
          [4.6, 3.1, 1.5, 0.2],
          [5. , 3.6, 1.4, 0.2],
          [5.4, 3.9, 1.7, 0.4],
          [4.6, 3.4, 1.4, 0.3],
          [5. , 3.4, 1.5, 0.2],
          [4.4, 2.9, 1.4, 0.2]])

x[:,:] would mean u want every row and every column.

x[3,3] would mean u want the 3 row and the 3 column value

x[:3,:3] would mean u want the rows and columns until 3

x[:, 3] would mean u want the 3 column and every row
© www.soinside.com 2019 - 2024. All rights reserved.