Identity Matrices & Diagonal Arrays (eye, identity, diag)
Identity Matrices & Diagonal Arrays (eye, identity, diag)
In linear algebra, statistics, and machine learning, square matrices with ones along the main diagonal—known as Identity Matrices—act as the multiplicative identity element (analogous to the number 1 in scalar arithmetic). NumPy provides dedicated functions to construct identity matrices and manipulate matrix diagonals.
1. Creating Identity Matrices: np.eye() vs np.identity()
Both functions generate matrices with ones along the diagonal and zeros elsewhere, but np.eye() is significantly more flexible:
2. Offsetting Diagonals with the 'k' Parameter
In np.eye(), the k parameter shifts the diagonal of ones:
k = 0(Default): Main diagonal.k > 0(Positive): Shifts the diagonal above the main diagonal (super-diagonal).k < 0(Negative): Shifts the diagonal below the main diagonal (sub-diagonal).
3. The Dual-Role of np.diag()
The np.diag() function performs two completely different operations depending on what you pass into it:
Behavior A: Extracting the Diagonal from a 2D Matrix
When passed a 2D matrix, it extracts the diagonal elements into a 1D vector:
Behavior B: Constructing a 2D Matrix from a 1D Vector
When passed a 1D vector, it constructs a 2D square matrix with those values along the diagonal and zeros everywhere else:
Multiple Choice Questions
1. What is the multiplicative property of an identity matrix 'I' when multiplied with a compatible matrix 'A': 'A @ I'?
A. It returns zero B. It returns matrix A unchanged (A @ I = A) C. It inverts matrix A D. It squares all numbers in A Answer: B Explanation: The identity matrix serves as the multiplicative neutral element in linear algebra; any matrix multiplied by the identity matrix yields itself.
2. What is the key functional difference between 'np.identity()' and 'np.eye()'?
A. np.identity only works in Python 2 B. np.eye can create non-square rectangular matrices and supports the diagonal offset parameter 'k' C. np.identity can only hold text D. There is no difference Answer: B Explanation: np.identity(N) strictly creates square N x N matrices on the main diagonal, whereas np.eye(N, M, k) supports arbitrary rows, columns, and shifted diagonals.
3. What will 'np.diag([4, 7, 9])' return?
A. A 1D array: array([4, 7, 9]) B. A 3x3 square matrix with 4, 7, and 9 along the main diagonal and zeros elsewhere C. The sum: 20 D. A syntax error Answer: B Explanation: Passing a 1D vector into np.diag() constructs a square 2D matrix with those elements positioned along the diagonal.
4. What will 'np.eye(3, k=-1, dtype=int)' place along the main diagonal (index [0,0], [1,1], [2,2])?
A. Ones B. Zeros (because k=-1 shifted the ones to the sub-diagonal below the main diagonal) C. Twos D. Minus ones Answer: B Explanation: A k value of -1 shifts the diagonal of ones down by one row, leaving the main diagonal populated with zeros.
5. If matrix 'M' is a 4x4 matrix, what is the shape of the output returned by 'np.diag(M)'?
A. (4, 4) B. (4,) C. (16,) D. (1,) Answer: B Explanation: Passing a 2D matrix into np.diag() extracts its diagonal values into a 1D vector of length 4.
1D Array Indexing, Slicing & Step Strides
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Inspecting Array Attributes (shape, ndim, size, itemsize) | 1D Array Indexing, Slicing & Step Strides |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.