NumPy: identity() function
identity() function
The identity array is a square array with ones on the main diagonal. The identity() function return the identity array.
Syntax:
numpy.identity(n, dtype=None)
Version: 1.15.0
Parameter:
Name | Description | Required / Optional |
---|---|---|
n | Number of rows (and columns) in n x n output. | Required |
dtype | Data-type of the output. Defaults to float. | optional |
Return value:
[ndarray] n x n array with its main diagonal set to one, and all other elements 0.
Example: numpy.identity() function
>>> import numpy as np
>>> np.identity(2)
array([[ 1., 0.],
[ 0., 1.]])
Pictorial Presentation:
Example: numpy.identity() function
>> import numpy as np
>>> np.identity(3, dtype=int)
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
>>> np.identity(3) #default data type is float
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
Pictorial Presentation:
Python - NumPy Code Editor:
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework