Contact: aviboots(AT)netvision.net.il
39,974 questions
51,918 answers
573 users
import numpy as np print(np.identity(5)) ''' run: [[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.]] '''
size = 5 identity_matrix = [ [1 if num == index else 0 for index in range(size)] for num in range(size) ] for row in identity_matrix: print(row) ''' run: [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] '''