当我尝试从numpy.ndarray获取最小值时,我收到类型错误。这是错误消息:
TypeError:ufunc'add'不包含签名匹配类型为dtype('S32')dtype('S32')dtype('S32')的循环
这是我从以下位置收到错误消息的代码块:
def laser_reading_callback(self, scan_data):
scan_ranges = scan_data.ranges
# np_array_scans = np.genfromtxt(np.array(scan_ranges))
np_array_scans = np.array(scan_ranges)
rospy.loginfo(type(np_array_scans)) # <-- prints out <type 'numpy.ndarray'>
rospy.loginfo("Min distance: "+np_array_scans.min()+" Array index: "+np_array_scans.argmin())
这是数组通常的样子:
[inf, inf, inf, inf, inf, 0.6639999747276306, inf, inf, inf, inf, inf, inf, inf,
inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf,
inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf,
inf, inf, inf, inf, inf, inf, inf, 0.3059999942779541, inf, 0.3109999895095825,
0.3089999854564667, 0.3059999942779541, 0.3070000112056732, 0.3050000071525574,
0.3050000071525574, 0.3050000071525574, 0.30399999022483826,
0.3019999861717224, 0.3019999861717224, 0.3009999990463257, 0.3009999990463257,
0.3009999990463257, inf, 0.3009999990463257, 0.3019999861717224,
0.3019999861717224, 0.3009999990463257, 0.30300000309944153,
0.3019999861717224, 0.3019999861717224, inf, inf, inf, inf, inf, inf, inf, inf,
inf, inf, inf, inf, inf, 6.3420000076293945, inf, inf, inf, inf, inf, inf, inf,
inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf,
3.427999973297119, inf, inf, 5.0970001220703125, 5.146999835968018,
5.22599983215332, 5.285999774932861, 5.320000171661377, 5.480000019073486,
5.568999767303467, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf,
inf, 6.293000221252441, 7.021999835968018, inf, inf, inf, inf, inf, inf, inf,
inf, inf, inf, inf, inf, inf, inf, 0.7070000171661377, inf, 0.7170000076293945,
inf, inf, inf, 0.4580000042915344, 0.43700000643730164, 0.42500001192092896,
0.4230000078678131, 0.421999990940094, 0.41499999165534973, inf,
1.180999994277954, 1.055999994277954, inf, inf, inf, inf, inf, inf,
1.7489999532699585, 1.7549999952316284, 1.7569999694824219, inf, inf,
4.76800012588501, 4.695000171661377, inf, inf, 4.7729997634887695,
4.7729997634887695, 4.770999908447266, 4.810999870300293, 4.794000148773193,
4.817999839782715, 4.835999965667725, inf, inf, 2.309999942779541,
2.2219998836517334, 2.1559998989105225, 2.1419999599456787, 2.1630001068115234,
2.2829999923706055, inf, inf, inf, inf, inf, 5.927000045776367,
5.660999774932861, inf, inf, inf, inf, inf, inf, inf, 0.6470000147819519, inf,
inf, inf, inf, inf, 2.9130001068115234, 2.9739999771118164, 2.99399995803833,
2.944000005722046, 2.9630000591278076, 3.0179998874664307, 3.0439999103546143,
3.0989999771118164, 3.177000045776367, 3.247999906539917, 3.2909998893737793,
inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf,
inf, inf, inf, inf, inf, inf, inf, inf, 9.274999618530273, 9.194000244140625,
9.170000076293945, inf, inf, inf, inf, inf, inf, inf, inf, inf,
9.038000106811523, inf, inf, inf, 1.812000036239624, 5.747000217437744,
5.8420000076293945, inf, 8.569999694824219, 8.522000312805176, inf, inf, inf,
inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf,
6.281000137329102, 6.132999897003174, 6.236999988555908, inf, 9.42300033569336,
9.114999771118164, inf, inf, inf, inf, inf, inf, inf, inf, inf,
7.169000148773193, 7.130000114440918, inf, inf, inf, inf, 6.449999809265137,
6.315000057220459, 6.308000087738037, inf, 2.5329999923706055, inf,
2.484999895095825, inf, inf, inf, inf, inf, 5.4070000648498535,
2.756999969482422, 2.6029999256134033, 2.563999891281128, 2.5480000972747803,
2.556999921798706, 2.63100004196167, 2.6670000553131104, 4.801000118255615,
inf, 1.3650000095367432, 1.3559999465942383, 1.3589999675750732,
1.3339999914169312, 1.3140000104904175, 1.309000015258789, 1.3040000200271606,
1.3289999961853027, 1.3769999742507935, 4.690999984741211, 4.614999771118164,
4.545000076293945, 4.303999900817871, 4.263000011444092, 4.270999908447266,
4.564000129699707, 4.565999984741211, 4.558000087738037, 4.552000045776367,
4.5329999923706055, 4.550000190734863, 4.534999847412109, 4.414999961853027,
4.427000045776367, inf, inf, inf]
问题:
argmin()
会返回什么?上下文:我正在从LiDAR系统获取扫描范围。上方的阵列是LiDAR发出的扫描范围。
答案 0 :(得分:0)
您不能将+
与字符串和numpy数组一起使用。只是改变
rospy.loginfo("Min distance: "+np_array_scans.min()+" Array index: "
+np_array_scans.argmin())
到
rospy.loginfo("Min distance: "+str(np_array_scans.min())+" Array index: "
+str(np_array_scans.argmin()))
它应该可以工作。
问题1:如何从数组中获取最小值?
Ans1:使用arr.min()
是arr
的{{1}}。
问题2:如果有2个或更多具有相同最小值的项目,argmin()将返回什么?
Ans2:如果numpy.ndarray
是一维numpy数组,即使存在多个最小值,arr
也只会返回一个索引。您可以使用arr.argmin
来获取所有最小值的索引。
问题3:为什么一维数组类型返回为ndarray?
Ans3:一维数组也是有效的np.where(arr==np.min(arr))[0]
。