Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the diag function in Python NumPy

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "how to use the diag function in Python NumPy", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use the diag function in Python NumPy" can help you solve your doubts.

Suppose you create a 1-dimensional array an and a 3-dimensional array b:import numpy as npa = np.arange (1,4) b = np.arange (1,10) .reshape (3,3)

The results are as follows:

> > a

Array ([1,2,3])

> > b

Array ([1, 2, 3]

[4, 5, 6]

[7, 8, 9])

Using the diag function, look at the result:

> > np.diag (a)

Array ([1,0,0]

[0, 2, 0]

[0,0,3]])

> > np.diag (b)

Array ([1,5,9])

It can be found that when in np.diag (array)

When array is a 1-dimensional array, the result is a matrix with one-dimensional array as diagonal elements.

When array is a two-dimensional matrix, the diagonal elements of the resulting output matrix

Add: structure and usage of numpy.diag () | | detailed description of parameters

Numpy.diag (vQuery karma 0)

Official document

Returns the diagonal (or non-diagonal) elements of a square matrix in the form of an one-dimensional array, or converts an one-dimensional array into a square matrix (non-diagonal elements are 0). The two functional role shifts depend on the v entered. one

For a deeper look, see numpy.diagnal ()

Detailed description of parameters:

V: array_like.

If v is a 2D array, returns the diagonal of the k position.

If v is a 1D array, returns a two-dimensional array of v as the diagonal of the k position.

K: int, optional

The position of the diagonal, greater than zero is above the diagonal, and less than zero is below.

Example > x = np.arange (9). Reshape ((3p3)) > > xarray ([[0,1,2], [3,4,5], [6,7,8]]) > > np.diag (x) array ([0,4,8]) > np.diag (x, Kraft 1) array ([1,5]) > > np.diag (x, kink 1) array ([3,7]) > > np.diag (np.diag (x) array ([[0,0,0]) 0], [0,4,0], [0,0,8]) This article "how to use the diag function in Python NumPy" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report