Python numpy index is out of bound for axis zero
Solution 1
Thanks for all of the comments. I figured my problem is that sortedList.size returns total number of elements in the array while I was expecting the number of tuples in my array (since sortedList is a list of tuples [[],[],...]). So I solved my problem using sortedList.shape
Solution 2
You don't have enough elements in your array
, for example:
In [5]: import numpy as np
In [6]: a = np.array([1,2])
In [8]: a[2] # there is no element at 2nd index
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-8-016a87a854bc> in <module>()
----> 1 a[2]
IndexError: index 2 is out of bounds for axis 0 with size 2
Solution 3
Considering how mysterious your problem is, I'd go ahead and test this with a try/except loop to be sure the code goes past that point and is only having issues at index 1658944...
something like:
for x in range(sortedList.size):
try:
sortedList[x]
except:
print "no index at", x
Report back what your results are.
Comments
-
ahajib almost 2 years
I have a code written in Python similar to the following:
def adamic_adar_prediction(graph): adjacencyMatrix = graph.get_adjacency() AAMatrix = adamic_adar_score(graph) AAMatrix = np.array(AAMatrix) i = (-AAMatrix ).argsort(axis=None, kind='mergesort') j = np.unravel_index(i, AAMatrix .shape) sortedList = np.vstack(j).T print(sortedList.size) print(sortedList[1658943]) print(sortedList[1658945])
While the result of the first print is 3,316,888 I receive the following error for the last print:
IndexError: index 1658944 is out of bounds for axis 0 with size 1658944
Any idea why this error arises for my array?
-
ahajib about 9 yearsBut as I have mentioned, the size of my numpy array is 3,317,888 !
-
Akavall about 9 years@nimafl, are you sure? The error message says otherwise, and
3,316,888
seems to be value at index1658943
. -
ahajib about 9 yearsAbsolutely. The value at 1658943 is [1287 1286].
-
Akavall about 9 years@nimafl, I see, in your questions you trying to get values at
1658943
and1658945
, but you error message shows1658944
, so it seems like you are not actually executing that code. Unfortunately, I can't help much more than this, but to me it feels like some silly mix up somewhere.