Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

identity matrix in python

#Change the value 3 to the size of the identity matrix
>>>np.identity(3)
array([[1.,  0.,  0.],
       [0.,  1.,  0.],
       [0.,  0.,  1.]])
Comment

identity matrix python

# first solution using NumPy and a second one without it

# 1st ****using NumPy****
>>>import numpy as np
>>>np.identity(5) # change value 5 to change matrix size
# output will be an Array
array([[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.]])

# 2nd ****without using NumPy****

>>>matrix_size = 5 # change value 5 to change matrix size
# use list comprehension
>>>identity_matrix = [
                  [1 if num == index else 0 for index in range(matrix_size)]
                  for num in range(matrix_size)
                  ]
>>>identity_matrix
# output will be a list
[[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]]
Comment

PREVIOUS NEXT
Code Example
Python :: typage in python 
Python :: python plot cut off when saving 
Python :: qpushbutton text alignment 
Python :: how to add row to the Dataframe in python 
Python :: printing with colors 
Python :: how to take user input in a list in python 
Python :: python tkinter close gui window 
Python :: the day before today python datetime 
Python :: open a filename starting with in python 
Python :: iterative binary search python 
Python :: pandas column string first n characters 
Python :: python make a random number 
Python :: which python mac 
Python :: module turtle has no forward member 
Python :: python print a help of a script 
Python :: print(DATA.popitem()) 
Python :: django override help text 
Python :: using-len-for-text-but-discarding-spaces-in-the-count 
Python :: import pandas 
Python :: python check if number is complex 
Python :: Python program to remove duplicate characters of a given string. 
Python :: pandas timedelta to seconds 
Python :: xpath helium 
Python :: creating a new enviroment in conda 
Python :: pandas find median of non zero values in a column 
Python :: T-Test Comparison of two means python 
Python :: django admin table columns wrap text into multiple lines django 
Python :: how to find index of an element in list in python stackoverflow 
Python :: python pandas transpose table dataframe without index 
Python :: create a response object in python 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =