5

I have two arrays, and I want all the elements of one to be divided by the second. For example,

In [24]: a = np.array([1,2,3])                                                      

In [25]: b = np.array([1,2,3])                                                      

In [26]: a/b                                                                        
Out[26]: array([1., 1., 1.])

In [27]: 1/b                                                                        
Out[27]: array([1.        , 0.5       , 0.33333333])

enter image description here

This is not the answer I want, the output I want is like (we can see all of the elements of a are divided by b)

In [28]: c = []                                                                     

In [29]: for i in a: 
    ...:     c.append(i/b) 
    ...:                                                                            

In [30]: c                                                                          
Out[30]: 
[array([1.        , 0.5       , 0.33333333]),
 array([2.        , 1.        , 0.66666667]),
In [34]: np.array(c)                                                                
Out[34]: 
array([[1.        , 0.5       , 0.33333333],
       [2.        , 1.        , 0.66666667],
       [3.        , 1.5       , 1.        ]])

enter image description here

But I don't like for loop, it's too slow for big data, so is there a function that included in numpy package or any good (faster) way to solve this problem?

4
  • As the output, do you want a single array that contains the result of each element of a divided by the corresponding element of b? Commented Jun 13, 2020 at 10:04
  • 1
    Please provide the code as actual code/text instead of images. This is for three reasons, the place where the images are hosted might remove them, text is searchable my search-engines, its easier for others to copy the code when they try to help you. Commented Jun 13, 2020 at 10:12
  • 1
    Please add actual code instead of images. Commented Jun 13, 2020 at 10:12
  • Thank you all very much for your patience in answering my questions. I have added the actual code in the text. Commented Jun 13, 2020 at 11:08

3 Answers 3

6

It is simple to do in pure numpy, you can use broadcasting to calculate the outer product (or any other outer operation) of two vectors:

import numpy as np

a = np.arange(1, 4)
b = np.arange(1, 4)

c = a[:,np.newaxis] / b

# array([[1.        , 0.5       , 0.33333333],
#        [2.        , 1.        , 0.66666667],
#        [3.        , 1.5       , 1.        ]])

This works, since a[:,np.newaxis] increases the dimension of the (3,) shaped array a into a (3, 1) shaped array, which can be used for the desired broadcasting operation.

Sign up to request clarification or add additional context in comments.

Comments

0

First you need to cast a into a 2D array (same shape as the output), then repeat for the dimension you want to loop over. Then vectorized division will work.

>>> a.reshape(-1,1)
array([[1],
       [2],
       [3]])

>>> a.reshape(-1,1).repeat(b.shape[0], axis=1) 
array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3]])


>>> a.reshape(-1,1).repeat(b.shape[0], axis=1) / b
array([[1.        , 0.5       , 0.33333333],
       [2.        , 1.        , 0.66666667],
       [3.        , 1.5       , 1.        ]])


# Transpose will let you do it the other way around, but then you just get 1 for everything
>>> a.reshape(-1,1).repeat(b.shape[0], axis=1).T
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

>>> a.reshape(-1,1).repeat(b.shape[0], axis=1).T / b
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

Comments

-1

This should do the job:

import numpy as np

a = np.array([1, 2, 3])
b = np.array([1, 2, 3])

print(a.reshape(-1, 1) / b)

Output:

[[ 1.          0.5         0.33333333]
 [ 2.          1.          0.66666667]
 [ 3.          1.5         1.        ]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.