Videos
I was given some code that I have to make faster in which a relatively small (about 4x100) numpy array is fed into numpy.linalg.norm as such:
nparray = nparray[:,(np.linalg.norm(nparray[0:2,:], axis=0)<variable/2)]
This ends up being called many times and is quite slow. I believe this is because the numpy function is working on such a small array compared to the size of arrays numpy is usually used for. I’ve done lots of research on what linalg.norm does to see if I can translate it to pure python, but I’m having a hard time wrapping my head around it, especially things like the axis argument and how that changes what to do in pure python. Any help is appreciated!
I’ve profiled my code and found that it spends most of its time inside a linalg.norm function which is called many times within a number of methods. I need to cut down on computation time, and I’m running out of places to do it. Is there a way I can perform the same operation so that it doesn’t have such a high overhead? I’ve scoured the internet and can’t find much. To be specific, the line is structured as such:
newArray = newArray[: , (np.linalg.norm(newArray[0:2,:], axis=0)<dia/2]
Where newArray is a preexisting array before this method is called, and dia is some real number.
Any help is appreciated!