报错信息:VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. Y_new = [np.squeeze(np.array(Y_new)).transpose()] # the aggregate output of 24 single outputs# 24个单独输出的聚合输出
解释原因:
警告信息是VisibleDeprecationWarning,它与从不规则嵌套序列(即长度或形状不同的列表、元组或ndarray的列表或元组)创建NumPy数组有关。警告建议在创建ndarray时,如果你打算这样做,必须指定dtype=object。
试图使用np.array(Y_new)将Y_new转换为NumPy数组。然而,由于Y_new中的元素具有不同的长度或形状,这触发了这个警告。
为了解决这个警告并创建ndarray,在调用np.array()时通过指定dtype参数为'object'来进行设置。
找到对应的行
Y_new = [np.squeeze(np.array(Y_new).transpose()]
改成
Y_new = [np.squeeze(np.array(Y_new, dtype=object)).transpose()]
文章讲述了在Python中遇到VisibleDeprecationWarning警告的原因,该警告出现在尝试从不同长度或形状的序列创建NumPy数组时。解决方案是通过指定dtype=object来创建数组。示例代码展示了如何修正这个问题,以避免警告并正确转换数据。
1万+

被折叠的 条评论
为什么被折叠?



