An example of how shape calculates a matrix in Python

  • 2020-05-30 20:28:17
  • OfStack

The example in this paper describes the method of shape to calculate the matrix in Python. I will share it with you for your reference as follows:

When you look at machine learning algorithms, you notice that shape computes matrices and I'm going to tell you what I understand


>>> from numpy import *
>>> import operator
>>> a =mat([[1,2,3],[5,6,9]])
>>> a
matrix([[1, 2, 3],
    [5, 6, 9]])


>>> shape(a)
(2, 3)
>>> a.shape[0] # Calculate number of lines 
2
>>> a.shape[1] # Calculate the number of columns 
3

Here is the explanation in Python


Examples
--------
>>> np.shape(np.eye(3))
(3, 3)
>>> np.shape([[1, 2]])
(1, 2)
>>> np.shape([0])
(1,)
>>> np.shape(0)
()
>>> a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
>>> np.shape(a)
(2,)
>>> a.shape
(2,)

For more information about Python, please check out the topics on this site: Python data structure and algorithm tutorial, Python Socket programming skills summary, Python function skills summary, Python string manipulation skills summary, Python introduction and advanced classic tutorial and Python file and directory manipulation skills summary.

I hope this article is helpful to you Python programming.


Related articles: