2

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?

2
  • 1
    Use print(sortedList[1658943]) instead. Commented Apr 20, 2015 at 1:39
  • Could you provide the exact code? How 'similar' is this code? Commented Apr 20, 2015 at 1:40

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

4 Comments

But as I have mentioned, the size of my numpy array is 3,317,888 !
@nimafl, are you sure? The error message says otherwise, and 3,316,888 seems to be value at index 1658943.
Absolutely. The value at 1658943 is [1287 1286].
@nimafl, I see, in your questions you trying to get values at 1658943 and 1658945, but you error message shows 1658944, 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.
2

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

2

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

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.