# Matrix product of a 1D (first argument) and a 2D array (second argument) in Numpy

NumpyServer Side ProgrammingProgramming

To find the matrix product of a 2D and a 1D array, use the numpy.matmul() method in Python Numpy. If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.

Returns the matrix product of the inputs. This is a scalar only when both x1, x2 are 1-d vectors. The out is a location into which the result is stored. If provided, it must have a shape that matches the signature (n,k),(k,m)->(n,m). If not provided or None, a freshly-allocated array is returned.

## Steps

At first, import the required library −

import numpy as np

Create a 1D and a 2D array −

arr1 = np.array([25, 35])
arr2 = np.array([[5, 7], [10, 15]])

Display the arrays −

print("Array 1 (Two Dimensional)...\n", arr1)
print("\nArray 2 (One Dimensional)...\n", arr2)

Get the type of the arrays −

print("\nOur Array 1 type...\n", arr1.dtype)
print("\nOur Array 2 type...\n", arr2.dtype)

Get the dimensions of the Arrays −

print("\nOur Array 1 Dimensions...\n",arr1.ndim)
print("\nOur Array 2 Dimensions...\n",arr2.ndim)

Get the shape of the Arrays −

print("\nOur Array 1 Shape...\n",arr1.shape)
print("\nOur Array 2 Shape...\n",arr2.shape)

To find the matrix product of a 2D and a 1D array, use the numpy.matmul() method in Python Numpy. If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed −

print("\nResult (matrix product)...\n",np.matmul(arr1, arr2))


## Example

import numpy as np

# Create a 1D and a 2D array
arr1 = np.array([25, 35])
arr2 = np.array([[5, 7], [10, 15]])

# Display the arrays
print("Array 1 (Two Dimensional)...\n", arr1)
print("\nArray 2 (One Dimensional)...\n", arr2)

# Get the type of the arrays
print("\nOur Array 1 type...\n", arr1.dtype)
print("\nOur Array 2 type...\n", arr2.dtype)

# Get the dimensions of the Arrays
print("\nOur Array 1 Dimensions...\n",arr1.ndim)
print("\nOur Array 2 Dimensions...\n",arr2.ndim)

# Get the shape of the Arrays
print("\nOur Array 1 Shape...\n",arr1.shape)
print("\nOur Array 2 Shape...\n",arr2.shape)

# To find the matrix product of a 2D and a 1D array, use the numpy.matmul() method in Python Numpy
# If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions.
# After matrix multiplication the prepended 1 is removed.
print("\nResult (matrix product)...\n",np.matmul(arr1, arr2))

## Output

Array 1 (Two Dimensional)...
[25 35]

Array 2 (One Dimensional)...
[[ 5 7]
[10 15]]

Our Array 1 type...
int64

Our Array 2 type...
int64

Our Array 1 Dimensions...
1

Our Array 2 Dimensions...
2

Our Array 1 Shape...
(2,)

Our Array 2 Shape...
(2, 2)

Result (matrix product)...
[475 700]