[英]receiving “ValueError: setting an array element with a sequence.”
我在使用此代碼時遇到了一些問題,試圖以兩個一維數組的內積結尾。 感興趣的代碼如下所示:
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)
針對此附加代碼的最后一行,我收到以下錯誤:
ValueError:使用序列設置數組元素。
在此錯誤的大多數其他示例中,我發現該錯誤是由於數組percentlist
和playerlist
中的數據類型不正確,但是我的應該是float類型。 如果有幫助,我稍后在程序中調用這些函數,如下所示:
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)
誰能幫我弄清楚為什么要使用序列設置數組元素? 請讓我知道是否有更多信息可能有所幫助! 同樣為了清楚起見, playerlister
正在使用xlrd
模塊從電子表格中提取數據,但是數據是數字的,測試表明這兩個列表的類型均為numpy.float64
。
i
一次迭代的每個形狀和內容為
<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]
您的函數find_percents
返回一個包含兩個元素的元組。 當您在others
調用它時,會將那個元組綁定到名為percentlist
的變量,然后嘗試在點積中使用它。
我的猜測是,通過用others
方式編寫此代碼,它是固定的:
def others(i,percentlist,playerlist,total):
playerlist = playerlister(i)
_, percentlist = find_percents(i)
players = numpy.dot(playerlist,percentlist)
當然,提供的playerlist
和percentlist
始終具有相同數量的元素(由於缺少電子表格,因此我們無法檢查)。
為了進行驗證,以下內容為您提供了確切的錯誤消息以及重現該錯誤消息的最少代碼:
>>> 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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.