Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Seemingly unrelated lines of Python code error when the order is changed

Could anyone tell me why I get an error based on the order of these seemingly unrelated line of code? If I call the numpy randint function before the loop I encounter no error but if I run the randint function after the loop I get this error:

  File "C:\Users\natha\OneDrive\Documents\Python\CollectionGeneration.py", line 70, in <module>
    n = np.random.randint(1, 5)
  File "mtrand.pyx", line 746, in numpy.random.mtrand.RandomState.randint
  File "_bounded_integers.pyx", line 1322, in numpy.random._bounded_integers._rand_int32
TypeError: 'tuple' object is not callable

I suspected the two events to be independent and I’m struggling to understand why this would be an issue. Perhaps even stranger, I can’t replicate the issue when trying the rand or uniform functions.

This runs fine

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import numpy as np
import math as ma

minradius = 0.1
maxradius = 0.9
maxlcm = 100

n = np.random.randint(1, 5)

a = np.linspace(int(minradius * 100), int(maxradius * 100), int((maxradius - minradius) * 100 + 1))
b = np.zeros((2, 1))

for i in a:
    for j in a:
        if ma.sqrt(i / 100) + 0.09 < ma.sqrt(j / 100):
            if ma.lcm(int(i), int(j)) <= maxlcm:
                bn = np.array = ([int(i)], [int(j)])
                b = np.hstack((b, bn))

This does not

import numpy as np
import math as ma

minradius = 0.1
maxradius = 0.9
maxlcm = 100

a = np.linspace(int(minradius * 100), int(maxradius * 100), int((maxradius - minradius) * 100 + 1))
b = np.zeros((2, 1))

for i in a:
    for j in a:
        if ma.sqrt(i / 100) + 0.09 < ma.sqrt(j / 100):
            if ma.lcm(int(i), int(j)) <= maxlcm:
                bn = np.array = ([int(i)], [int(j)])
                b = np.hstack((b, bn))

n = np.random.randint(1, 5)

Any advice would be appreciated and I’ll try to provide more information if required.

>Solution :

You’re assigning np.array to a tuple on this line:

bn = np.array = ([int(i)], [int(j)])

I think you mean:

bn = np.array([int(i)], [int(j)])

The function np.random.randint is most likely calling np.array after you assign it to a tuple in your second example which creates the exception you see in your question.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading