- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a two-dimensional array with the flattened input as an upper diagonal in Numpy
To create a two-dimensional array with the flattened input as a diagonal, use the numpy.diagflat() method in Python Numpy. The 'K parameter is used to set the diagonal; 0, the default, corresponds to the “main” diagonal, a positive (negative) k giving the number of the diagonal above (below) the main.
The first parameter is the input data, which is flattened and set as the k-th diagonal of the output. The second parameter is the diagonal to set; 0, the default, corresponds to the “main” diagonal, a positive (negative) k giving the number of the diagonal above (below) the main.
NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.
Steps
At first, import the required library −
import numpy as np
Create a 2D array using the numpy.array() method −
arr = np.array([[5, 10], [15, 20]])
Displaying our array −
print("Array...
",arr)
Get the datatype −
print("
Array datatype...
",arr.dtype)
Get the dimensions of the Array −
print("
Array Dimensions...
",arr.ndim)
Get the shape of the Array −
print("
Our Array Shape...
",arr.shape)
Get the number of elements of the Array −
print("
Elements in the Array...
",arr.size)
Create a two-dimensional array with the flattened input as a diagonal, use the numpy.diagflat() method −
print("
Result...
",np.diagflat(arr, k = 1))
Example
import numpy as np # Create a 2D array using the numpy.array() method arr = np.array([[5, 10], [15, 20]]) # Displaying our array print("Array...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr.size) # To create a two-dimensional array with the flattened input as a diagonal, use the numpy.diagflat() method in Python Numpy # The 'K parameter is used to set the diagonal; 0, the default, corresponds to the “main” diagonal, # a positive (negative) k giving the number of the diagonal above (below) the main. print("
Result...
",np.diagflat(arr, k = 1))
Output
Array... [[ 5 10] [15 20]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (2, 2) Elements in the Array... 4 Result... [[ 0 5 0 0 0] [ 0 0 10 0 0] [ 0 0 0 15 0] [ 0 0 0 0 20] [ 0 0 0 0 0]]
- Related Articles
- Create a two-dimensional array with the flattened input as a diagonal in Numpy
- Create a two-dimensional array with the flattened input as lower diagonal in Numpy
- Return the Upper triangle of an array and zero the main diagonal as well in Numpy
- Create an array with ones above the main diagonal and zeros elsewhere in Numpy
- Create an array with ones below the main diagonal and zeros elsewhere in Numpy
- Return a 2-D array with ones on the upper diagonal and zeros elsewhere in Numpy
- Create an array with zero above the main diagonal forming a lower triangular matrix in Numpy
- Return the Upper triangle of an array and set the diagonal above to zero elements in Numpy
- Return the Upper triangle of an array and zero elements just above the main diagonal in Numpy
- Create an array with ones at and below the given diagonal and zeros elsewhere in Numpy
- Return the Upper triangle of an array in Numpy
- Create an array with ones at and below the given diagonal and zeros elsewhere with a different output type in Numpy
- How to create a two dimensional array in JavaScript?
- How to create a two-dimensional array in TypeScript?
- Return the Lower triangle of an array and zero the main diagonal as well in Numpy
