You can multiply numpy arrays by scalars and it just works.

>>> import numpy as np
>>> np.array([1, 2, 3]) * 2
array([2, 4, 6])
>>> np.array([[1, 2, 3], [4, 5, 6]]) * 2
array([[ 2,  4,  6],
       [ 8, 10, 12]])

This is also a very fast and efficient operation. With your example:

>>> a_1 = np.array([1.0, 2.0, 3.0])
>>> a_2 = np.array([[1., 2.], [3., 4.]])
>>> b = 2.0
>>> a_1 * b
array([2., 4., 6.])
>>> a_2 * b
array([[2., 4.],
       [6., 8.]])
Answer from iz_ on Stack Overflow
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.multiply.html
numpy.multiply โ€” NumPy v2.5.dev0 Manual
>>> 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.]]) The * operator can be used as a shorthand for np.multiply on ndarrays.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.multiply.html
numpy.multiply โ€” NumPy v2.2 Manual
The * operator can be used as a shorthand for np.multiply on ndarrays. >>> x1 = np.arange(9.0).reshape((3, 3)) >>> x2 = np.arange(3.0) >>> x1 * x2 array([[ 0., 1., 4.], [ 0., 4., 10.], [ 0., 7., 16.]])
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-multiply-in-python
numpy.multiply() in Python - GeeksforGeeks
July 11, 2025 - The numpy.multiply() is a numpy function in Python which is used to find element-wise multiplication of two arrays or scalar (single value).
๐ŸŒ
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 - You might be wondering, โ€œWhat exactly is a shape mismatch?โ€ Itโ€™s when two arrays have incompatible dimensions for the intended operation. Imagine trying to fit a square peg into a round hole โ€” it just doesnโ€™t work. ... import numpy as np # Attempting invalid multiplication array1 = np.array([1, 2, 3]) array2 = np.array([[4, 5], [6, 7]]) try: result = array1 * array2 except ValueError as e: print("Error:", e)
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_array_multiplication.htm
NumPy - Array Multiplication
Element-wise Multiplication โˆ’ Each element in the first array is multiplied by the corresponding element in the second array. Matrix Multiplication โˆ’ Performs a dot product of rows and columns, following linear algebra rules.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.matmul.html
numpy.matmul โ€” NumPy v2.1 Manual
Broadcasting is conventional for stacks of arrays ยท >>> a = np.arange(2 * 2 * 4).reshape((2, 2, 4)) >>> b = np.arange(2 * 2 * 4).reshape((2, 4, 2)) >>> np.matmul(a,b).shape (2, 2, 2) >>> np.matmul(a, b)[0, 1, 1] 98 >>> sum(a[0, 1, :] * b[0 , :, 1]) 98 ยท Vector, vector returns the scalar inner product, but neither argument is complex-conjugated: >>> np.matmul([2j, 3j], [2j, 3j]) (-13+0j) Scalar multiplication raises an error.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.multiply.html
numpy.multiply โ€” NumPy v2.1 Manual
The * operator can be used as a shorthand for np.multiply on ndarrays. >>> x1 = np.arange(9.0).reshape((3, 3)) >>> x2 = np.arange(3.0) >>> x1 * x2 array([[ 0., 1., 4.], [ 0., 4., 10.], [ 0., 7., 16.]])
Find elsewhere
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ multiply-every-element-in-a-numpy-array-a-comprehensive-guide
Multiply Every Element in a Numpy Array: A Guide | Saturn Cloud Blog
July 23, 2023 - # Multiply every element in the array by 2 arr3 = np.multiply(arr, 2) print(arr3) ... Both methods will give the same result. The choice between the two often comes down to personal preference and the specific requirements of your code.
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ matrix-multiplication
NumPy Matrix Multiplication
import numpy as np result = np.matmul(matrix_a, matrix_b) # or result = matrix_a @ matrix_b ยท In these syntaxes, matrix_a and matrix_b are arrays or matrices to be multiplied, producing a new matrix as the result.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ absolute_beginners.html
NumPy: the absolute basics for beginners โ€” NumPy v2.4 Manual
The dimensions of your array must be compatible, for example, when the dimensions of both arrays are equal or when one of them is 1. If the dimensions are not compatible, you will get a ValueError. Learn more about broadcasting here. This section covers maximum, minimum, sum, mean, product, standard deviation, and more ยท NumPy also performs aggregation functions. In addition to min, max, and sum, you can easily run mean to get the average, prod to get the result of multiplying the elements together, std to get the standard deviation, and more.
Top answer
1 of 3
44
 you can do this in two simple steps using NumPy:

>>> # multiply column 2 of the 2D array, A, by 5.2
>>> A[:,1] *= 5.2

>>> # assuming by 'cumulative sum' you meant the 'reduced' sum:
>>> A[:,1].sum()

>>> # if in fact you want the cumulative sum (ie, returns a new column)
>>> # then do this for the second step instead:
>>> NP.cumsum(A[:,1])

with some mocked data:

>>> A = NP.random.rand(8, 5)
>>> A
  array([[ 0.893,  0.824,  0.438,  0.284,  0.892],
         [ 0.534,  0.11 ,  0.409,  0.555,  0.96 ],
         [ 0.671,  0.817,  0.636,  0.522,  0.867],
         [ 0.752,  0.688,  0.142,  0.793,  0.716],
         [ 0.276,  0.818,  0.904,  0.767,  0.443],
         [ 0.57 ,  0.159,  0.144,  0.439,  0.747],
         [ 0.705,  0.793,  0.575,  0.507,  0.956],
         [ 0.322,  0.713,  0.963,  0.037,  0.509]])

>>> A[:,1] *= 5.2

>>> A
  array([[ 0.893,  4.287,  0.438,  0.284,  0.892],
         [ 0.534,  0.571,  0.409,  0.555,  0.96 ],
         [ 0.671,  4.25 ,  0.636,  0.522,  0.867],
         [ 0.752,  3.576,  0.142,  0.793,  0.716],
         [ 0.276,  4.255,  0.904,  0.767,  0.443],
         [ 0.57 ,  0.827,  0.144,  0.439,  0.747],
         [ 0.705,  4.122,  0.575,  0.507,  0.956],
         [ 0.322,  3.71 ,  0.963,  0.037,  0.509]])

>>> A[:,1].sum()
  25.596156138451427

just a few simple rules are required to grok element selection (indexing) in NumPy:

  • NumPy, like Python, is 0-based, so eg, the "1" below refers to the second column

  • commas separate the dimensions inside the brackets, so [rows, columns], eg, A[2,3] means the item ("cell") at row three, column four

  • a colon means all of the elements along that dimension, eg, A[:,1] creates a view of A's column 2; A[3,:] refers to the fourth row

2 of 3
8

Sure:

import numpy as np
# Let a be some 2d array; here we just use dummy data 
# to illustrate the method
a = np.ones((10,5))
# Multiply just the 2nd column by 5.2 in-place
a[:,1] *= 5.2

# Now get the cumulative sum of just that column
csum = np.cumsum(a[:,1])

If you don't want to do this in-place you would need a slightly different strategy:

b = 5.2*a[:,1]
csum = np.cumsum(b)
๐ŸŒ
University of Utah
web.physics.utah.edu โ€บ ~detar โ€บ lessons โ€บ python โ€บ numpy_arrays โ€บ node7.html
Multiplying an array by a constant
Next: Adding a constant to Up: A sampling of useful Previous: Array shape ยท When we multiply an array by a constant, each element is multiplied by that constant.
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
๐ŸŒ
Lawrence
www2.lawrence.edu โ€บ fast โ€บ GREGGJ โ€บ Python โ€บ numpy โ€บ numpyLA.html
numpy for Linear Algebra
For example, to print the bottom right entry in the matrix A we would do ... The first slice selects all rows in A, while the second slice selects just the middle entry in each row. To do a matrix multiplication or a matrix-vector multiplication we use the np.dot() method.
Top answer
1 of 1
475

Simplest solution

Use numpy.dot or a.dot(b). See the documentation here.

>>> a = np.array([[ 5, 1 ,3], 
                  [ 1, 1 ,1], 
                  [ 1, 2 ,1]])
>>> b = np.array([1, 2, 3])
>>> print a.dot(b)
array([16, 6, 8])

This occurs because numpy arrays are not matrices, and the standard operations *, +, -, / work element-wise on arrays.

Note that while you can use numpy.matrix (as of early 2021) where * will be treated like standard matrix multiplication, numpy.matrix is deprecated and may be removed in future releases.. See the note in its documentation (reproduced below):

It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.

Thanks @HopeKing.


Other Solutions

Also know there are other options:

  • As noted below, if using python3.5+ and numpy v1.10+, the @ operator works as you'd expect:

    >>> print(a @ b)
    array([16, 6, 8])
    
  • If you want overkill, you can use numpy.einsum. The documentation will give you a flavor for how it works, but honestly, I didn't fully understand how to use it until reading this answer and just playing around with it on my own.

    >>> np.einsum('ji,i->j', a, b)
    array([16, 6, 8])
    
  • As of mid 2016 (numpy 1.10.1), you can try the experimental numpy.matmul, which works like numpy.dot with two major exceptions: no scalar multiplication but it works with stacks of matrices.

    >>> np.matmul(a, b)
    array([16, 6, 8])
    
  • numpy.inner functions the same way as numpy.dot for matrix-vector multiplication but behaves differently for matrix-matrix and tensor multiplication (see Wikipedia regarding the differences between the inner product and dot product in general or see this SO answer regarding numpy's implementations).

    >>> np.inner(a, b)
    array([16, 6, 8])
    
    # Beware using for matrix-matrix multiplication though!
    >>> b = a.T
    >>> np.dot(a, b)
    array([[35,  9, 10],
           [ 9,  3,  4],
           [10,  4,  6]])
    >>> np.inner(a, b) 
    array([[29, 12, 19],
           [ 7,  4,  5],
           [ 8,  5,  6]])
    
  • If you have multiple 2D arrays to dot together, you may consider the np.linalg.multi_dot function, which simplifies the syntax of many nested np.dots. Note that this only works with 2D arrays (i.e. not for matrix-vector multiplication).

      >>> np.dot(np.dot(a, a.T), a).dot(a.T)
      array([[1406,  382,  446],
             [ 382,  106,  126],
             [ 446,  126,  152]])
      >>> np.linalg.multi_dot((a, a.T, a, a.T))
      array([[1406,  382,  446],
             [ 382,  106,  126],
             [ 446,  126,  152]])
    

Rarer options for edge cases

  • If you have tensors (arrays of dimension greater than or equal to one), you can use numpy.tensordot with the optional argument axes=1:

    >>> np.tensordot(a, b, axes=1)
    array([16,  6,  8])
    
  • Don't use numpy.vdot if you have a matrix of complex numbers, as the matrix will be flattened to a 1D array, then it will try to find the complex conjugate dot product between your flattened matrix and vector (which will fail due to a size mismatch n*m vs n).

๐ŸŒ
Medium
medium.com โ€บ @amit25173 โ€บ numpy-element-wise-multiplication-306fd4cb5841
NumPy Element-wise Multiplication | by Amit Yadav | Medium
January 25, 2025 - We first import numpy and define two arrays: array1 and array2. Using the * operator, we multiply each element from array1 with the corresponding element in array2.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ numpy-for-matlab-users.html
NumPy for MATLAB users โ€” NumPy v2.5.dev0 Manual
For matrix, * means matrix multiplication, and for element-wise multiplication one has to use the multiply() function. ... For array, the vector shapes 1xN, Nx1, and N are all different things. Operations like A[:,1] return a one-dimensional array of shape N, not a two-dimensional array of ...