我试着逐行分解一个程序。Y是一个数据矩阵,但是我找不到任何关于.shape[0]具体做什么的具体数据。
for i in range(Y.shape[0]):
if Y[i] == -1:这个程序使用numpy、scipy、matplotlib.pyplot和cvxopt。
发布于 2012-04-18 06:44:45
numpy数组的shape属性返回数组的维数。如果Y有n行和m列,则Y.shape为(n,m)。所以Y.shape[0]就是n。
In [46]: Y = np.arange(12).reshape(3,4)
In [47]: Y
Out[47]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [48]: Y.shape
Out[48]: (3, 4)
In [49]: Y.shape[0]
Out[49]: 3发布于 2014-01-18 13:12:12
shape是一个给出数组维度的元组。
>>> c = arange(20).reshape(5,4)
>>> c
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])
c.shape[0]
5给出行数
c.shape[1]
4给出列数
发布于 2012-04-18 06:45:38
shape是一个元组,它指示数组中的维数。因此,在您的示例中,由于Y.shape[0]的索引值为0,因此将沿着数组的第一维进行操作。
来自http://www.scipy.org/Tentative_NumPy_Tutorial#head-62ef2d3c0a5b4b7d6fdc48e4a60fe48b1ffe5006
An array has a shape given by the number of elements along each axis:
>>> a = floor(10*random.random((3,4)))
>>> a
array([[ 7., 5., 9., 3.],
[ 7., 2., 7., 8.],
[ 6., 8., 3., 2.]])
>>> a.shape
(3, 4)http://www.scipy.org/Numpy_Example_List#shape还有更多的例子。
https://stackoverflow.com/questions/10200268
复制相似问题