1

I've been having some problems with this code, trying to end up with an inner product of two 1-D arrays. The code of interest looks like this:

def find_percents(i):
    percents=[]
    median=1.5/(6+2*int(i/12))
    b=2*median
    m=b/(7+2*int(i/12))
    for j in xrange (1,6+2*int(i/12)):
        percents.append(float((b-m*j)))
    percentlist=numpy.asarray(percents, dtype=float)
    #print percentlist
    total=sum(percentlist)
    return total, percentlist

def playerlister(i):
    players=[]
    for i in xrange(i+1,i+6+2*int(i/12)):
        position=sheet.cell(i,2)
        points=sheet.cell(i,24)
        if re.findall('RB', str(position.value)):
            vbd=points.value-rbs[24]
            players.append(vbd)
        else:
            pass
    playerlist=numpy.asarray(players, dtype=float)
    return playerlist

def others(i,percentlist,playerlist,total):
    alternatives=[]
    playerlist=playerlister(i)
    percentlist=find_percents(i)
    players=numpy.dot(playerlist,percentlist)

I am receiving the following error in response to the very last line of this attached code:

ValueError: setting an array element with a sequence.

In most other examples of this error, I have found the error to be because of incorrect data types in the arrays percentlist and playerlist, but mine should be float type. If it helps at all, I call these functions a little later in the program, like so:

for i in xrange(1,30):
    total, percentlist= find_percents(i)
    playerlist= playerlister(i)
    print type(playerlist[i])
    draft_score= others(i,percentlist,playerlist,total)

Can anyone help me figure out why I am setting an array element with a sequence? Please let me know if any more information might be helpful! Also for clarity, the playerlister is making use of the xlrd module to extract data from a spreadsheet, but the data are numerical and testing has shown that that both lists have a type of numpy.float64.

The shape and contents of each of these for one iteration of i is

<type 'numpy.float64'>
(5,)
[  73.7  -94.4  140.9   44.8  130.9]
(5,)
[ 0.42857143  0.35714286  0.28571429  0.21428571  0.14285714]
0

1 Answer 1

1

Your function find_percents returns a two-element tuple. When you call it in others, you are binding that tuple to the variable named percentlist, which you then try to use in a dot-product.

My guess is that by writing this in others it is fixed:

def others(i,percentlist,playerlist,total):
    playerlist = playerlister(i)
    _, percentlist = find_percents(i)
    players = numpy.dot(playerlist,percentlist)

provided of course playerlist and percentlist always have the same number of elements (which we can't check because of the missing spreadsheet).

To verify, the following gives you the exact error message and the minimum of code needed to reproduce it:

>>> import numpy as np
>>> a = np.arange(5)
>>> np.dot(a, (2, a))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.
Sign up to request clarification or add additional context in comments.

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.