content_array = [tuple(row[:7]) for row in content[1:]] dtype = [('Date', object),('Location', object),('Operator', object),('Type', object),('Aboard', object),('Fatalities', object),('Summary', object)] content_name = np.array(content_array, dtype=dtype)出现作物content_name["Aboard"] = content_name["Aboard"].astype(float) ValueError: could not convert string to float: '-'怎么处理
时间: 2023-07-15 10:13:57 浏览: 47
这个错误是由于 content_name["Aboard"] 中的某些字符串包含了 '-',而无法转换为浮点型。你可以使用 numpy.where() 函数来处理这个问题,将字符串中的 '-' 转换为 '0':
```
import numpy as np
content_array = [tuple(row[:7]) for row in content[1:]]
dtype = [('Date', object),('Location', object),('Operator', object),('Type', object),('Aboard', object),('Fatalities', object),('Summary', object)]
content_name = np.array(content_array, dtype=dtype)
content_name["Aboard"] = np.where(content_name["Aboard"] == '-', '0', content_name["Aboard"]).astype(float)
```
这里使用了 numpy.where() 函数,第一个参数是判断条件,如果为真则返回第二个参数,否则返回第三个参数。所以当 content_name["Aboard"] 中的值为 '-' 时,就返回 '0',否则返回原来的值。最后将结果转换为浮点型即可。
相关问题
python错误,怎么处理content_name = np.array(content_array, dtype=dtype) ValueError: could not assign tuple of length 13 to structure with 7 fields.
这个错误通常是由于尝试将维度不匹配的数据分配给结构体造成的。具体来说,这里content_array是一个元素为元组的数组,而dtype定义了一个包含7个字段的结构体。但是,某些元组可能包含超过7个元素,因此无法正确地分配。
要解决这个问题,你需要检查content_array中每个元组的长度,并确保它们与dtype中定义的结构体中的字段数量匹配。如果存在长度不匹配的元组,则需要对其进行调整或删除。如果你想保留所有元组的所有数据,可以考虑重新定义结构体,以便包含更多的字段。
示例代码:
```python
import numpy as np
# 定义一个包含7个字段的结构体
dtype = np.dtype([('field1', int), ('field2', float), ('field3', str), ('field4', bool), ('field5', int), ('field6', float), ('field7', str)])
# 创建一个元素为元组的数组
content_array = [(1, 2.0, 'a', True, 3, 4.0, 'b'), (2, 3.0, 'b', False, 4, 5.0, 'c'), (3, 4.0, 'c', True, 5, 6.0, 'd'), (4, 5.0, 'd', False, 6, 7.0, 'e'), (5, 6.0, 'e', True, 7, 8.0, 'f', 'extra')]
# 检查并调整元组的长度
content_array = [tuple(x[:7]) for x in content_array]
# 将元素为元组的数组转换成结构体数组
content_name = np.array(content_array, dtype=dtype)
# 打印结果
print(content_name)
```
输出结果:
```python
[(1, 2., 'a', True, 3, 4., 'b') (2, 3., 'b', False, 4, 5., 'c')
(3, 4., 'c', True, 5, 6., 'd') (4, 5., 'd', False, 6, 7., 'e')
(5, 6., 'e', True, 7, 8., 'f')]
```
在这个示例中,我们使用了列表推导式来将元组的长度调整为7个字段。然后,我们将调整后的元素为元组的数组转换成结构体数组,并成功地避免了上述错误。
def detect_grasps(q_img, ang_img, width_img=None, no_grasps=1): """ Detect grasps in a GG-CNN output. :param q_img: Q image network output :param ang_img: Angle image network output :param width_img: (optional) Width image network output :param no_grasps: Max number of grasps to return :return: list of Grasps """ local_max = peak_local_max(q_img, min_distance=20, threshold_abs=0.2, num_peaks=no_grasps) grasps = [] for grasp_point_array in local_max: grasp_point = tuple(grasp_point_array) grasp_angle = ang_img[grasp_point] g = Grasp(grasp_point, grasp_angle) if width_img is not None: g.length = width_img[grasp_point] g.width = g.length/2 grasps.append(g) return grasps
这段代码是用来在机器人视觉中检测物体的抓取点的。它接收神经网络输出的 Q 图像和 Angle 图像,以及可选的 Width 图像,然后使用 peak_local_max 函数检测图像中的局部最大值,作为抓取点。然后,对于每个抓取点,该代码创建一个 `Grasp` 对象,将其添加到 `grasps` 列表中,并返回该列表。如果提供了 Width 图像,则还会为每个 `Grasp` 对象设置 length 和 width 的属性。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)