Return the identity array.
The identity array is a square array with ones on the main diagonal.
ParametersnintNumber of rows [and columns] in n x n output.
dtypedata-type, optionalData-type of the output. Defaults to float
.
Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like
supports the
__array_function__
protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.
New in version 1.20.0.
Returnsoutndarrayn x n array with its main diagonal set to one, and all other elements 0.
Examples
>>> np.identity[3] array[[[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]]
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
numpy.identity[n, dtype = None] : Return a identity matrix i.e. a square matrix with ones on the main diagonal.
Parameters : n : [int] Dimension n x n of output array dtype : [optional, float[by Default]] Data type of returned array.
Returns : identity array of dimension n x n, with its main diagonal set to one, and all other elements 0.
Example:
Python
import
numpy as geek
b
=
geek.identity[
2
, dtype
=
float
]
print
[
"Matrix b : \n"
, b]
a
=
geek.identity[
4
]
print
[
"\nMatrix a : \n"
, a]
Output :
Matrix b : [[ 1. 0.] [ 0. 1.]] Matrix a : [[ 1. 0. 0. 0.] [ 0. 1. 0. 0.] [ 0. 0. 1. 0.] [ 0. 0. 0. 1.]]
Note :
These codes won’t run on online-ID. Please run them on your systems to explore
the working.
This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about
the topic discussed above.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Introduction to Identity Matrix :
The dictionary definition of an Identity Matrix is a square matrix
in which all the elements of the principal or main diagonal are 1’s and all other elements are zeros. In the below image, every matrix is an Identity Matrix.
In linear algebra, this is sometimes called as a Unit Matrix, of a square matrix [size = n x n] with ones on the main diagonal and zeros elsewhere. The identity matrix is denoted by “ I
“. Sometimes U or E is also used to denote an Identity Matrix.
A property of the identity matrix is that it leaves a matrix unchanged if it is multiplied by an Identity Matrix.
Examples:
Input : 2 Output : 1 0 0 1 Input : 4 Output : 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 The explanation is simple. We need to make all the elements of principal or main diagonal as 1 and everything else as 0.
Program to print Identity Matrix :
The logic is simple. You need to the print 1 in those positions where row is equal to column of a matrix and make all other positions as 0.
Python3
def
Identity[size]:
for
row
in
range
[
0
, size]:
for
col
in
range
[
0
, size]:
if
[row
=
=
col]:
print
[
"1 "
, end
=
" "
]
else
:
print
[
"0 "
, end
=
" "
]
print
[]
size
=
5
Identity[size]
Output:
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
Time complexity: O[R*C] where R and C is no of rows and column in matrix respectively
Program to check if a given square matrix is Identity Matrix :
Python3
MAX
=
100
;
def
isIdentity[mat, N]:
for
row
in
range
[N]:
for
col
in
range
[N]:
if
[row
=
=
col
and
mat[row][col] !
=
1
]:
return
False
;
elif
[row !
=
col
and
mat[row][col] !
=
0
]:
return
False
;
return
True
;
N
=
4
;
mat
=
[[
1
,
0
,
0
,
0
],
[
0
,
1
,
0
,
0
],
[
0
,
0
,
1
,
0
],
[
0
,
0
,
0
,
1
]];
if
[isIdentity[mat, N]]:
print
[
"Yes "
];
else
:
print
[
"No "
];
Output:
Yes
Time complexity: O[N2] where N is number of rows and columns of matrix
Auxiliary Space:
O[1]