Complete the code to create a 3x3 identity matrix using NumPy.
from numpy import eye matrix = eye([1])
eye with ones or zeros.The eye function creates an identity matrix of the given size. Here, a 3x3 identity matrix is created by passing 3.
Complete the code to create a 2x4 matrix of zeros using NumPy.
from numpy import zeros matrix = zeros(([1]))
The zeros function creates a matrix filled with zeros. Passing the shape as a tuple (2, 4) creates a 2 rows by 4 columns matrix.
Fix the error in the code to multiply two matrices using NumPy.
from numpy import array A = array([[1, 2], [3, 4]]) B = array([[5, 6], [7, 8]]) result = A [1] B
* instead of @ for matrix multiplication.dot as an operator instead of a method.Matrix multiplication in Python with arrays uses the @ operator. Using * does element-wise multiplication, which is not matrix multiplication.
Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
words = ['data', 'science', 'is', 'fun'] lengths = {word: [1] for word in words if [2]
The dictionary comprehension maps each word to its length using len(word). The condition len(word) > 3 filters words longer than 3 characters.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if the length is greater than 2.
words = ['cat', 'dog', 'elephant'] result = { [1]: [2] for word in words if [3] }
The dictionary comprehension uses word.upper() as the key, len(word) as the value, and filters words with length greater than 2 using len(word) > 2.