Broadcasting involves 2 steps

  • give all arrays the same number of dimensions

  • expand the 1 dimensions to match the other arrays

With your inputs

(41,6) (41,)

one is 2d, the other 1d; broadcasting can change the 1d to (1, 41), but it does not automatically expand in the other direction (41,1).

(41,6) (1,41) 

Neither (41,41) or (6,41) matches the other.

So you need to change your y to (41,1) or the x to (6,41)

x.T*y
x*y[:,None]

I'm assuming, of course, that you want element by element multiplication, not the np.dot matrix product.

Answer from hpaulj on Stack Overflow
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How can arrays with different dimensions be multiplied? - Python - Data Science Dojo Discussions
February 28, 2023 - I encountered an error while attempting to multiply two numpy arrays with different dimensions in my data science project. Could anyone kindly share insights on the correct ways to multiply matrices with disparate dimens…
Discussions

python - Multiply two arrays with different dimensions using numpy - Stack Overflow
Stack Data Licensing Data licensing offering for businesses to build and improve AI tools and models ... Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... import numpy as np a = np.array... More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
python - Numpy docs: How to multiply 2 arrays of different sizes together? - Stack Overflow
Numpy docs claims you can multiply arrays of different lengths together, however it is not working. I'm definitely misinterpreting what its saying but there's no example to go with their text. From... More on stackoverflow.com
🌐 stackoverflow.com
python - numpy array multiplication with arrays of arbitrary dimensions - Stack Overflow
I have a numpy array A, which has shape (10,). I also have, as of this moment, a numpy array B with shape (10,3,5). I want to do a multiplication between these two to get C such that C[0,:,:]=A[0... More on stackoverflow.com
🌐 stackoverflow.com
python - Multiply Numpy arrays of different sizes - Stack Overflow
I would like to perform some multiplicative operation on the two where each coefficient is multiplied by each of the times to result in an array like: array([[0, 0, 0], [1, 2, 3], [2, 4, 6], [3, 6, 9]]) ... However I was wondering whether there was a more efficient numpythonic way of performing ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
medium.com › @whyamit101 › different-ways-to-multiply-arrays-in-numpy-65aa2522e265
Different Ways to Multiply Arrays in NumPy | by why amit | Medium
February 9, 2025 - # Reshaping to make shapes compatible array1 = np.array([1, 2, 3]) array2 = np.array([[4], [5], [6]]) result = array1.reshape(3, 1) * array2 print("Reshaped arrays multiplication:\n", result) ... By reshaping array1 to (3, 1), its dimensions now align with array2’s shape (3, 1), allowing NumPy to perform broadcasting. Key Tip: When reshaping, always ensure the total number of elements remains the same.
🌐
Data Science Parichay
datascienceparichay.com › home › blog › numpy – elementwise multiplication of two arrays
Numpy - Elementwise multiplication of two arrays - Data Science Parichay
June 17, 2022 - You can use the numpy np.multiply() function to perform the elementwise multiplication of two arrays. Alternatively, you can also use the * operator.
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-186.php
Python NumPy: Multiply an array of dimension by an array with dimensions - w3resource
new_array = nums1 * nums2[:,:,None]: Performs element-wise multiplication of nums1 and the reshaped nums2, using broadcasting. Since nums1 contains ones and nums2 contains threes, the resulting new_array will be a 3D array of shape (2, 2, 3) ...
🌐
Stack Overflow
stackoverflow.com › questions › 73492199 › numpy-docs-how-to-multiply-2-arrays-of-different-sizes-together
python - Numpy docs: How to multiply 2 arrays of different sizes together? - Stack Overflow
In case it helps, I also tried ... turn up in research. ... You can multiply arrays together if every dimenssion has the same length or one of the arrays has dimension 1 in the current axis....
Find elsewhere
Top answer
1 of 2
4

You could use None (or np.newaxis) to expand A to match B:

>>> A = np.arange(10)
>>> B = np.random.random((10,3,5))
>>> C0 = np.array([A[i]*B[i,:,:] for i in range(len(A))])
>>> C1 = A[:,None,None] * B
>>> np.allclose(C0, C1)
True

But this will only work for the 2 case. Borrowing from @ajcr, with enough transposes we can get implicit broadcasting to work for the general case:

>>> C3 = (A * B.T).T
>>> np.allclose(C0, C3)
True

Alternatively, you could use einsum to provide the generality. In retrospect it's probably overkill here compared with the transpose route, but it's handy when the multiplications are more complicated.

>>> C2 = np.einsum('i,i...->i...', A, B)
>>> np.allclose(C0, C2)
True

and

>>> B = np.random.random((10,4))
>>> D0 = np.array([A[i]*B[i,:] for i in range(len(A))])
>>> D2 = np.einsum('i,i...->i...', A, B)
>>> np.allclose(D0, D2)
True
2 of 2
2

Although I like the einsum notation, I'll add a little variety to the mix ....

You can add enough extra dimensions to a so that it will broadcast across b.

>>> a.shape
(3,)
>>> b.shape
(3,2)

b has more dimensions than a

extra_dims = b.ndim - a.ndim

Add the extra dimension(s) to a

new_shape = a.shape + (1,)*extra_dims    # (3,1)
new_a = a.reshape(new_shape)

Multiply

new_a * b

As a function:

def f(a, b):
    '''Product across the first dimension of b.

    Assumes a is 1-dimensional.
    Raises AssertionError if a.ndim > b.ndim or
     - the first dimensions are different
    '''
    assert a.shape[0] == b.shape[0], 'First dimension is different'
    assert b.ndim >= a.ndim, 'a has more dimensions than b'

    # add extra dimensions so that a will broadcast
    extra_dims = b.ndim - a.ndim
    newshape = a.shape + (1,)*extra_dims
    new_a = a.reshape(newshape)

    return new_a * b
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › multiply
Python Numpy multiply() - Multiply Array Elements | Vultr Docs
November 18, 2024 - This code multiplies corresponding elements from the two arrays, [1, 2, 3] and [4, 5, 6], to produce [4, 10, 18]. Recognize that NumPy can handle operations on arrays of different sizes using broadcasting.
🌐
Stack Overflow
stackoverflow.com › questions › 64749037 › multiply-numpy-arrays-of-different-sizes
python - Multiply Numpy arrays of different sizes - Stack Overflow
>>> import numpy as np >>> a = np.array([0, 1, 2, 3]) >>> t = np.array([1, 2, 3]) >>> res1 = t * a[:, None] >>> res1 array([[0, 0, 0], [1, 2, 3], [2, 4, 6], [3, 6, 9]]) ... Ah yes, that looks good, do you know what the difference would be between a[:, None] and a[:, np.newaxis]? In terms of speed? ... I think they are same. None is just an alias for np.newaxis. It is mentioned here: numpy.org/doc/stable/reference/…
🌐
TutorialsPoint
tutorialspoint.com › multiply-arguments-element-wise-with-different-shapes-in-numpy
Multiply arguments element-wise with different shapes in Numpy
February 7, 2022 - ",np.multiply(arr1, arr2)) import numpy as np # Create two arrays with different shapes arr1 = np.arange(27.0).reshape((3, 3, 3)) arr2 = np.arange(9.0).reshape((3, 3)) # Display the arrays print("Array 1... ", arr1) print(" Array 2... ", arr2) # Get the type of the arrays print(" Our Array 1 type... ", arr1.dtype) print(" Our Array 2 type... ", arr2.dtype) # Get the dimensions of the Arrays print(" Our Array 1 Dimensions...
🌐
Medium
medium.com › @heyamit10 › numpy-multiply-in-python-7914322b2888
numpy.multiply() in Python. If you think you need to spend $2,000… | by Hey Amit | Medium
February 8, 2025 - This might remind you of a skilled chef who knows how to scale a recipe to fit a different number of guests — efficient and effective! ... Sometimes, you just need to multiply every element in an array by a single value (a scalar). It’s like applying a universal rule to everything — quick and straightforward. ... import numpy as np # Define an array array = np.array([1, 2, 3, 4]) # Multiply by a scalar result = np.multiply(array, 5) print(result) # Output: [ 5 10 15 20 ]
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.multiply.html
numpy.multiply — NumPy v2.5.dev0 Manual
Equivalent to x1 * x2 in terms of array broadcasting. ... Try it in your browser! >>> import numpy as np >>> np.multiply(2.0, 4.0) 8.0
🌐
Reddit
reddit.com › r/learnpython › how to multiply matrices with different dimensions
r/learnpython on Reddit: How to multiply matrices with different dimensions
December 5, 2021 -

I'm trying to multiply A = 20x1x10 with B = 10x20 and I'm supposed to get a 20x1x20 matrix as the output. So far I've tried

C = numpy.matmul(A,B)
# and 
C = A @ B

But all of them seem to result a 20x20x20 matrix. Any idea how to proceed from here?

For better context, A = jacobian output of derivative of an activation function in a neural network, with shape of (N samples * 1 * values) and B = changes in output of the layer

🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.multiply.html
numpy.multiply — NumPy v2.1 Manual
Equivalent to x1 * x2 in terms of array broadcasting. ... >>> x1 = np.arange(9.0).reshape((3, 3)) >>> x2 = np.arange(3.0) >>> np.multiply(x1, x2) array([[ 0., 1., 4.], [ 0., 4., 10.], [ 0., 7., 16.]])
🌐
Educative
educative.io › blog › numpy-matrix-multiplication
NumPy matrix multiplication: Get started in 5 minutes
2 weeks ago - Try one of our courses on Python programming fundamentals: ... The matmul() function gives us the matrix product of two 2-d arrays. With this method, we can’t use scalar values for our input.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.multiply.html
numpy.multiply — NumPy v2.4 Manual
January 31, 2021 - Equivalent to x1 * x2 in terms of array broadcasting. ... Try it in your browser! >>> import numpy as np >>> np.multiply(2.0, 4.0) 8.0
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.matmul.html
numpy.matmul — NumPy v2.4 Manual
If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly. 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.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › numpy tutorial › matrix multiplication in numpy
Matrix Multiplication in NumPy | Different Types of Matrix Multiplication
March 20, 2023 - A = np.array([[1,1],[1,1]]) ... multiplication. If you wish to perform element-wise matrix multiplication, then use np.multiply() function. The dimensions of the input matrices should be the same....
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Replit
replit.com › home › discover › how to multiply arrays in python
How to multiply arrays in Python | Replit
April 13, 2026 - The fix works by converting arr1 into a column vector using reshape(-1, 1). This aligns its dimensions with arr2, allowing NumPy’s broadcasting rules to apply correctly. The column is effectively stretched across the rows of the second array, ...