Your idea is right, but you can write it down a bit simpler:
list_a = [1,2,3,4,5] # or hh[0]
list_b = [6,7,8,9,0] # or hh[1]
multiplied = [a * b for a, b in zip(list_a, list_b)]
Also, if you want / operator to return float, add from __future__ import division at the top of your source.
Your idea is right, but you can write it down a bit simpler:
list_a = [1,2,3,4,5] # or hh[0]
list_b = [6,7,8,9,0] # or hh[1]
multiplied = [a * b for a, b in zip(list_a, list_b)]
Also, if you want / operator to return float, add from __future__ import division at the top of your source.
You can use list comprehension, just like your previous number * array question.
Say you have two arrays:
a = [1,2,3]
b = [4,5,6]
First you zip them to obtain the pairs you wish to multiply:
pairs = zip(a,b)
This results in [(1, 4), (2, 5), (3, 6)].
You can 'unpack' a tuple like this:
val1, val2 = (1,4) # val1=1 and val2=4
Combining everything together, this would multiple arrays a and b:
c = [val1*val2 for val1,val2 in zip(a,b)]
In your example above, hh is an array containing your two arrays and the answer becomes:
hh=[[82.5], [168.5]]
N=zip(*hh)
ll = [x*y for x,y in N]
python - Multiplication of two arrays in numpy - Stack Overflow
python - How to perform element-wise multiplication of two lists? - Stack Overflow
python - Multiply arrays in array with two numbers - Stack Overflow
python - multiplying two array in python3.7 - Data Science Stack Exchange
Videos
Hi! I'm stuck with the following problem: I have two arrays of size (4,4,N) each, M1 and M2, so one can think of them as an 'array of matrices' or 'vector of matrices' of size 4x4. I want to 'multiply' the two arrays so that i get as an output an array M of the same size (4,4,N), where each element of the last dimension of M, M[:,:,i], i = {0,1, ... , N-1} is the matrix multiplication of the corresponding ith elemets of M1 and M2.
The hardcode way of doing it is
for i in rage(0,N): M[:,:,i] = M1[:,:,i] @ M2[:,:,i]
But I'm sure there's a more efficient way of doing it. I've searched on stackoverflow and tried with np.einsum() and boradcasting, but struggled in all my attempts.
I'm pretty new to Python, so don't be so hard with me😅.
Thank you for your help!
One way is to use the outer function of np.multiply (and transpose if you want the same order as in your question):
>>> np.multiply.outer(x, y).T
array([[3, 6],
[4, 8]])
Most ufuncs in NumPy have this useful outer feature (add, subtract, divide, etc.). As @Akavall suggests, np.outer is equivalent for the multiplication case here.
Alternatively, np.einsum can perform the multiplication and transpose in one go:
>>> np.einsum('i,j->ji', x, y)
array([[3, 6],
[4, 8]])
A third approach is to insert a new axis in one the arrays and then multiply, although this is a little more verbose:
>>> (x[:, np.newaxis] * y).T
array([[3, 6],
[4, 8]])
For those interested in performance, here are the timings of the operations, from quickest to slowest, on two arrays of length 15:
In [70]: x = np.arange(15)
In [71]: y = np.arange(0, 30, 2)
In [72]: %timeit np.einsum('i,j->ji', x, y)
100000 loops, best of 3: 2.88 µs per loop
In [73]: %timeit np.multiply.outer(x, y).T
100000 loops, best of 3: 5.48 µs per loop
In [74]: %timeit (x[:, np.newaxis] * y).T
100000 loops, best of 3: 6.68 µs per loop
In [75]: %timeit np.outer(x, y).T
100000 loops, best of 3: 12.2 µs per loop
You can you use np.outer.
In [7]: x = np.array([1, 2])
In [8]: y = np.array([3, 4])
In [10]: np.outer(x,y).T
Out[10]:
array([[3, 6],
[4, 8]])
Use a list comprehension mixed with zip():.
Copy[a*b for a,b in zip(lista,listb)]
Since you're already using numpy, it makes sense to store your data in a numpy array rather than a list. Once you do this, you get things like element-wise products for free:
CopyIn [1]: import numpy as np
In [2]: a = np.array([1,2,3,4])
In [3]: b = np.array([2,3,4,5])
In [4]: a * b
Out[4]: array([ 2, 6, 12, 20])
Just convert your data to arrays and then simply take a product *. The trick here is to create a 1-d vector of your two values with which you want to multiply. The * then performs element wise multiplication
import numpy as np
mult = np.array([1080, 1920])
inp = np.array([[0.4375, 0.3477366255144033], [0.3599537037037037, 0.676954732510288],
[0.5648148148148148, 0.720164609053498], [0.8483796296296297, 0.44238683127572015],
[0.8726851851851852, 0.3374485596707819]])
result = inp*mult
# array([[ 472.5 , 667.65432099],
# [ 388.75 , 1299.75308642],
# [ 610. , 1382.71604938],
# [ 916.25 , 849.38271605],
# [ 942.5 , 647.90123457]])
EDIT:: Time comparison Both methods work similarly
%timeit inp*mult
# 2.89 µs ± 365 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit np.multiply(inp, mult)
# 2.55 µs ± 322 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
You want to perform element-wise multiplication of arrays. So use numpy.multiply() method.
>>> x1 = np.array([[0.4375, 0.3477366255144033], [0.3599537037037037, 0.676954732510288], [0.5648148148148148, 0.720164609053498], [0.8483796296296297, 0.44238683127572015], [0.8726851851851852, 0.3374485596707819]])
>>>
>>> x1
array([[0.4375 , 0.34773663],
[0.3599537 , 0.67695473],
[0.56481481, 0.72016461],
[0.84837963, 0.44238683],
[0.87268519, 0.33744856]])
>>> x2 = np.array([1080, 1920])
>>> x2
array([1080, 1920])
>>> prod = np.multiply(x1, x2)
>>> prod
array([[ 472.5 , 667.65432099],
[ 388.75 , 1299.75308642],
[ 610. , 1382.71604938],
[ 916.25 , 849.38271605],
[ 942.5 , 647.90123457]])
EDIT: As answered by @Sheldore above, using * operator is fine too, and does the same job.