You need to convert array b to a (2, 1) shape array, use None or numpy.newaxis in the index tuple:

import numpy
a = numpy.array([[2,3,2],[5,6,1]])
b = numpy.array([3,5])
c = a * b[:, None]

Here is the document.

Answer from HYRY on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-multiply-2d-array-to-1d-array
NumPy | Multiply 2D Array to 1D Array - GeeksforGeeks
July 11, 2025 - import numpy as np ini_array1 = ... result print("New resulting array: ", result) ... We use None, to add a new axis to the 1D NumPy array. This reshapes the 1D array to a 2D array and allows us to multiply it ...
Discussions

python - Multiplying numpy 2d Array with 1d Array - Stack Overflow
How can I get it so that it multiplies the indices from top to bottom with the corresponding values from a 1d array when the row length of the 2d array is smaller than length of 1d array ? More on stackoverflow.com
๐ŸŒ stackoverflow.com
June 5, 2017
python - numpy: multiplying a 2D array by a 1D array - Stack Overflow
This is equivalent to reshaping the vector as (len(s), 1). Then, the shapes of the multiplied objects will be (4,2) and (4,1), which are compatible due to NumPy broadcasting rules (corresponding dimensions are either equal to each other or equal to 1). More on stackoverflow.com
๐ŸŒ stackoverflow.com
June 16, 2014
numpy - Element wise multiplication of a 2D and 1D array in python - Stack Overflow
I humbly doubt you should start dealing with pandas vs numpy speed issues as a significant problem to tackle now in your process if you are just discovering how to multiply two dataframes or arrays. 2016-10-11T18:27:37.637Z+00:00 ... Find the answer to your question by asking. More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 11, 2016
numpy - Multiply a 1d array x 2d array python - Stack Overflow
I have a 2d array and a 1d array and I need to multiply each element in the 1d array x each element in the 2d array columns. It's basically a matrix multiplication but numpy won't allow matrix More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ numpy-program-to-multiply-each-row-of-2d-array-by-1d-array.php
Numpy Program to multiply each row of 2D array by 1D array
Multiply each row of a 4x4 array by a corresponding element of a 1D array using broadcasting, and then sum each row. Create a function that scales each row of a 2D array by different factors provided in a 1D array.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ multiply-columns-of-a-2d-array-by-a-1d-array-using-numpy.php
Multiply columns of a 2D array by a 1D array using NumPy
August 30, 2025 - Learn how to multiply each column of a 2D array by a 1D array using NumPy's broadcasting. Detailed steps and example code provided.
๐ŸŒ
Sharp Sight
sharpsight.ai โ€บ blog โ€บ numpy-multiply
How to Use the Numpy Multiply Function - Sharp Sight
November 12, 2021 - When we do this, Numpy performs what is called โ€œbroadcasting.โ€ Effectively, it takes the 1D vector, treats it as a row of data, and multiplies that vector by every row in the 2D array.
Find elsewhere
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ matrix-product-of-a-2d-first-argument-and-a-1d-array-second-argument-in-numpy
numpy.matmul()
The numpy.matmul() function returns the matrix product of two arrays. While it returns a normal product for 2-D arrays, if dimensions of either argument is >2, it is treated as a stack of matrices residing in the last two indexes and is broadcast ...
๐ŸŒ
Dataquest Community
community.dataquest.io โ€บ q&a โ€บ dq courses
Why does matrix multiplication with a 1D-array work? - DQ Courses - Dataquest Community
July 1, 2019 - a = np.array([[1,1], [2,2], [3,3]]) b = np.array([10,20]) a.shape b.shape a@b Output: (3, 2) (2,) array([30, 60, 90]) Something must be happening to b in the backend? I expected @ to require 2D matrices for both operands
๐ŸŒ
YouTube
youtube.com โ€บ watch
Understanding Numpy Matrix Multiplication in 1D and 2D through Examples - YouTube
I'll take you through examples of Numpy's matrix multiplication function in 1D and 2D. By the end of the video, you'll have a solid understanding of how the ...
Published ย  August 17, 2023
๐ŸŒ
Quora
quora.com โ€บ How-do-you-multiply-2D-arrays-in-Python
How to multiply 2D arrays in Python - Quora
Solution: Use the np.matmul(a, b) function that takes two NumPy arrays as input and returns the result of the multiplication of both arrays.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ absolute_beginners.html
NumPy: the absolute basics for beginners โ€” NumPy v2.6.dev0 Manual
Basic operations are simple with NumPy. If you want to find the sum of the elements in an array, youโ€™d use sum(). This works for 1D arrays, 2D arrays, and arrays in higher dimensions.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ articles on trending technologies โ€บ matrix product of 1d and 2d arrays in numpy
Matrix Product of 1D and 2D Arrays in NumPy
February 7, 2022 - 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.
๐ŸŒ
Finxter
blog.finxter.com โ€บ how-to-multiply-2d-matrices-in-numpy
How to Multiply 2D Matrices in Numpy? โ€“ Be on the Right Side of Change
In other words, the number of columns of a is the same as the number of rows of b. How to multiply a with b using standard matrix multiplication? Solution: Use the np.matmul(a, b) function that takes two NumPy arrays as input and returns the result of the multiplication of both arrays.
Top answer
1 of 7
21

Solution Code -

import numpy as np

# Given axis along which elementwise multiplication with broadcasting 
# is to be performed
given_axis = 1

# Create an array which would be used to reshape 1D array, b to have 
# singleton dimensions except for the given axis where we would put -1 
# signifying to use the entire length of elements along that axis  
dim_array = np.ones((1,a.ndim),int).ravel()
dim_array[given_axis] = -1

# Reshape b with dim_array and perform elementwise multiplication with 
# broadcasting along the singleton dimensions for the final output
b_reshaped = b.reshape(dim_array)
mult_out = a*b_reshaped

Sample run for a demo of the steps -

In [149]: import numpy as np

In [150]: a = np.random.randint(0,9,(4,2,3))

In [151]: b = np.random.randint(0,9,(2,1)).ravel()

In [152]: whos
Variable   Type       Data/Info
-------------------------------
a          ndarray    4x2x3: 24 elems, type `int32`, 96 bytes
b          ndarray    2: 2 elems, type `int32`, 8 bytes

In [153]: given_axis = 1

Now, we would like to perform elementwise multiplications along given axis = 1. Let's create dim_array:

In [154]: dim_array = np.ones((1,a.ndim),int).ravel()
     ...: dim_array[given_axis] = -1
     ...: 

In [155]: dim_array
Out[155]: array([ 1, -1,  1])

Finally, reshape b & perform the elementwise multiplication:

In [156]: b_reshaped = b.reshape(dim_array)
     ...: mult_out = a*b_reshaped
     ...: 

Check out the whos info again and pay special attention to b_reshaped & mult_out:

In [157]: whos
Variable     Type       Data/Info
---------------------------------
a            ndarray    4x2x3: 24 elems, type `int32`, 96 bytes
b            ndarray    2: 2 elems, type `int32`, 8 bytes
b_reshaped   ndarray    1x2x1: 2 elems, type `int32`, 8 bytes
dim_array    ndarray    3: 3 elems, type `int32`, 12 bytes
given_axis   int        1
mult_out     ndarray    4x2x3: 24 elems, type `int32`, 96 bytes
2 of 7
6

Avoid copying data and wasting resources!

Utilizing casting and views, instead of actually copying data N times into a new array with appropriate shape (as existing answers do) is way more memory efficient. Here is such a method (based on @ShuxuanXU's code):

def mult_along_axis(A, B, axis):

    # ensure we're working with Numpy arrays
    A = np.array(A)
    B = np.array(B)

    # shape check
    if axis >= A.ndim:
        raise AxisError(axis, A.ndim)
    if A.shape[axis] != B.size:
        raise ValueError(
            "Length of 'A' along the given axis must be the same as B.size"
            )

    # np.broadcast_to puts the new axis as the last axis, so 
    # we swap the given axis with the last one, to determine the
    # corresponding array shape. np.swapaxes only returns a view
    # of the supplied array, so no data is copied unnecessarily.
    shape = np.swapaxes(A, A.ndim-1, axis).shape

    # Broadcast to an array with the shape as above. Again, 
    # no data is copied, we only get a new look at the existing data.
    B_brc = np.broadcast_to(B, shape)

    # Swap back the axes. As before, this only changes our "point of view".
    B_brc = np.swapaxes(B_brc, A.ndim-1, axis)

    return A * B_brc