简体   繁体   English

关于python中numpy矩阵的问题

[英]Questions about numpy matrix in python

#these are defined as [a b]
hyperplanes = np.mat([[0.7071,    0.7071, 1], 
                      [-0.7071,   0.7071, 1],
                      [0.7071,   -0.7071, 1],
                      [-0.7071,  -0.7071, 1]]);

a = hyperplanes[:][:,0:2].T;
b = hyperplanes[:,2];

what does these denotes of [:][:,0:2] mean? 这些表示[:] [:,0:2]是什么意思? What is the final results of a and b? a和b的最终结果是什么?

We can use the interactive interpreter to find this out. 我们可以使用交互式解释器来找到它。

In [3]: hyperplanes = np.mat([[0.7071,    0.7071, 1], 
   ...:                       [-0.7071,   0.7071, 1],
   ...:                       [0.7071,   -0.7071, 1],
   ...:                       [-0.7071,  -0.7071, 1]])

Notice that we don't need semicolons at the end of lines in Python. 请注意,我们在Python的行末尾不需要分号。

In [4]: hyperplanes
Out[4]: 
matrix([[ 0.7071,  0.7071,  1.    ],
        [-0.7071,  0.7071,  1.    ],
        [ 0.7071, -0.7071,  1.    ],
        [-0.7071, -0.7071,  1.    ]])

We get back a matrix object. 我们找回一个matrix对象。 NumPy typically uses an ndarray (you would do np.array instead of np.mat above), but in this case, everything is the same whether it's a matrix or ndarray . NumPy通常使用ndarray (您可以使用np.array而不是上面的np.mat ),但在这种情况下,无论是矩阵还是ndarray ,一切都是相同的。

Let's look at a . 让我们来看看a

In [7]: hyperplanes[:][:,0:2].T
Out[7]: 
matrix([[ 0.7071, -0.7071,  0.7071, -0.7071],
        [ 0.7071,  0.7071, -0.7071, -0.7071]])

The slicing on this is a little strange. 切片对此有点奇怪。 Notice that: 请注意:

In [9]: hyperplanes[:]
Out[9]: 
matrix([[ 0.7071,  0.7071,  1.    ],
        [-0.7071,  0.7071,  1.    ],
        [ 0.7071, -0.7071,  1.    ],
        [-0.7071, -0.7071,  1.    ]])

In [20]: np.all(hyperplanes == hyperplanes[:])
Out[20]: True

In other words, you don't need the [:] in there at all. 换句话说,你根本不需要[:] Then, we're left with hyperplanes[:,0:2].T . 然后,我们留下了hyperplanes[:,0:2].T The [:,0:2] can be simplified to [:,:2] , which mean that we want to get all of the rows in hyperplanes , but only the first two columns. [:,0:2]可以简化为[:,:2] ,这意味着我们想要获得hyperplanes所有行,但只能获得前两列。

In [14]: hyperplanes[:,:2]
Out[14]: 
matrix([[ 0.7071,  0.7071],
        [-0.7071,  0.7071],
        [ 0.7071, -0.7071],
        [-0.7071, -0.7071]])

.T gives us the transpose. .T给了我们转置。

In [15]: hyperplanes[:,:2].T
Out[15]: 
matrix([[ 0.7071, -0.7071,  0.7071, -0.7071],
        [ 0.7071,  0.7071, -0.7071, -0.7071]])

Finally, b = hyperplanes[:,2] gives us all of the rows, and the second column. 最后, b = hyperplanes[:,2]给出了所有行和第二列。 In other words, all elements in column 2. 换句话说,第2列中的所有元素。

In [21]: hyperplanes[:,2]
Out[21]: 
matrix([[ 1.],
        [ 1.],
        [ 1.],
        [ 1.]])

Since Python is an interpreted language, it's easy to try things out for ourself and figure out what's going on. 由于Python是一种解释型语言,因此很容易为自己尝试并找出正在发生的事情。 In the future, if you're stuck, go back to the interpreter, and try things out -- change some numbers, take off the .T , etc. 在将来,如果你遇到困难,请回到翻译,然后尝试一下 - 改变一些数字,取下.T等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM