gpt4 book ai didi

python - numpy.sort() 错误 ValueError : The truth value of an array with more than one element is ambiguous. 使用 a.any() 或 a.all()

转载 作者:行者123 更新时间:2023-11-28 18:38:23 29 4
gpt4 key购买 nike

当我使用 numpy 对数组进行排序时这个问题出现了:

Traceback (most recent call last):
File "D:/Desktop/LIP/complier/num/f_t.py", line 75, in <module>
frogs[i].sort(order='length')
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

但是如果我注释掉这些代码,就可以了

        # if len < temp_len:
# r_w[i]['length'] = len
# else:
# r_w[i]['points'] = r_g['points']
# r_w[i]['length'] = r_g['length']

为什么?

r_g 是最好的路线,r_w 是每组中最差的路线,r_b 是每组中最好的路线。我想循环更改最差的路线 5 次。

代码如下:

# -*- coding: utf-8 -*- 
# __author__ = 'youzipi'
import numpy as np

def cal_length(route):
length = 0
for i in range(POINTS_NUM):
p1 = points[route[i]]
p2 = points[route[(i + 1) % POINTS_NUM]]
length += np.linalg.norm(p1 - p2)
return length

POINTS_NUM = 10
ROUTES_NUM = 50
routetype = np.dtype({
'names': ['points', 'length'],
'formats': ['O', 'f']})
routes = np.zeros([(ROUTES_NUM)], dtype=routetype)
points = np.random.rand(POINTS_NUM, 2) * 10
print "points=", points

for index in range(ROUTES_NUM):
temp = np.random.permutation(range(POINTS_NUM))
length = cal_length(temp)
routes[index] = (temp, length)


print "after sort"
routes.sort(order=('length'))
print routes
frogs = np.zeros([5, 10], dtype=routetype)

for i in range(5):
t = 0
for j in range(ROUTES_NUM):
if j%5 == i:
frogs[i][j/5]['points'] = routes[j]['points']
frogs[i][j/5]['length'] = routes[j]['length']

for i in range(5):
print "frogs", i
print frogs[i]
p = routes['length']
print p
r_g = routes[0]
print "r_g", r_g
r_b = frogs[:, 0]
print "r_b", r_b
r_w = frogs[:, 9]
print "r_w", r_w


#def update_frogs():
#global r_w, r_b, r_b
for i in range(5):
for j in range(5):
cut = int(np.random.rand(1)[0] * 10)
ran = np.random.permutation(range(POINTS_NUM))
print 'r_w', r_w[i]
print 'r_b', r_b[i]
temp_len = r_w[i]['length']
r_w[i]['points'] = np.hstack((r_b[i]['points'][:cut], np.linspace(-1, -1, 10)[cut:]))
for t in ran:
if t not in r_w[i]['points']:
r_w[i]['points'][cut] = t
cut = cut + 1
if cut >= POINTS_NUM:
break
len = cal_length(r_w[i]['points'])
# if len < temp_len:
# r_w[i]['length'] = len
# else:
# r_w[i]['points'] = r_g['points']
# r_w[i]['length'] = r_g['length']
frogs[i].sort(order='length')#trackbreak here!!
r_w[i]['points'] = frogs[i, 9]['points']
r_w[i]['length'] = frogs[i, 9]['length']
print "after"
print 'f_w', r_w[i]
print 'f_b', r_b[i]

最佳答案

查看“相关”列 - 有很多关于此 ValueError 的问题。

最可能的原因是您生成了一个 True/False 值列表或数组,然后尝试在 Python 中使用它 if/else上下文。

因此,我建议您查看那些问题行中的变量,并确定哪些生成多个值,而不是牵着您的手分析您的所有代码。实际上,因为只有 if声明,它必须是len < temp_len .这两个变量中的其他或其他是数组,而不是标量。

    # if len < temp_len:
# r_w[i]['length'] = len
# else:
# r_w[i]['points'] = r_g['points']
# r_w[i]['length'] = r_g['length']

关于python - numpy.sort() 错误 ValueError : The truth value of an array with more than one element is ambiguous. 使用 a.any() 或 a.all(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30069294/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com