Introduction to NumPy block
From nested lists of blocks, the numpy.block() function creates a nd-array. The blocks in the innermost lists are concatenate along the last dimension (-1), then along the second-last dimension (-2), and so on until they reach the outermost list.
Blocks can be any size, but they won’t be broadcasted according to regular rules. To make block.ndim the same for all blocks, leading axes of size 1 is insert instead. This is mostly handy when working with scalars, and it implies that code like np.block([v, 1]) is legal as long as v.ndim == 1.
Block matrices can be built from their components when the nested list is two levels deep.
- arrays: If a single ndarray or scalar (a nested list of depth 0) is given. It is return unchange (and not in copied). The forms of the elements must match along the proper axes (without broadcasting), but leading 1s will be prepend to the shape as needed to match the dimensions.
Return: block array: ndarray The array is create by putting together the specified components. The dimensionality of the output is equal to the greatest of: * the dimensionality of all the inputs * the depth to which the input list is nested.
Raises: Value Error
Code Examples of NumPy block
# welcome to softhunt.net # import numpy as np import numpy as np A = np.eye(2) * 2 B = np.eye(3) * 3 C = np.block([[A, np.zeros((2, 3))],[np.ones((3, 2)), B ]]) print(C)
[[2. 0. 0. 0. 0.] [0. 2. 0. 0. 0.] [1. 1. 3. 0. 0.] [1. 1. 0. 3. 0.] [1. 1. 0. 0. 3.]]
# welcome to softhunt.net # import numpy as np import numpy as np a = np.array([1, 3, 7]) b = np.array([9, 11, 13]) c = np.block([a, b, 15]) print(c)
[ 1 3 7 9 11 13 15]
What is block multiplication?
When two block matrices have the same shape and their diagonal blocks are square matrices, then they multiply similarly to matrix multiplication.
Why should we use NumPy?
NumPy arrays are more compact and quicker than Python lists. An array uses less memory and is easier to work with. NumPy stores data in a considerably less amount of memory and has a way for specifying data types.