Search code examples
pythontypeerror

Python throwing error on np.array type for unidentifiable reason


The following code:


import numpy as np


N=4
m=2**N

I=[0]*16
for j in range(m):
    I[j] = ("{0:04b}".format(j))

i = np.array([-1,-1,1,1,1,1,1,1,1,1,1,1,1,1,1,1])
ψ0 = np.array([1]*m)
HW = np.array([0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4])
sorted_i = np.array([x for _,x in sorted(zip(HW,i))])
sorted_I = np.array([x for _,x in sorted(zip(HW,I))])

when I hit 'run' gives

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-66-4e0f18a0df58> in <module>
     13 ψ0 = np.array([1]*m)
     14 HW = np.array([0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4])
---> 15 sorted_i = np.array([x for _,x in sorted(zip(HW,i))])
     16 sorted_I = np.array([x for _,x in sorted(zip(HW,I))])

TypeError: 'numpy.ndarray' object is not callable

However copy + pasting the exact same code and running it in isolation from a separate jupyter notebook file gives no errors. What on Earth is causing this? I thought it might be vestiges of variable names messing things up but I have no idea.


Solution

  • Maybe np might have been assigned to a different object, try restarting your Jupyter notebook, you could also try a different alias for numpy like import numpy as npy