为了账号安全,请及时绑定邮箱和手机立即绑定

将均匀间隔的值插入 numpy 数组

将均匀间隔的值插入 numpy 数组

慕的地6264312 2023-02-12 19:10:58
我正在尝试重写以下代码,processed_feats[0, 0::feats+2] = current_feats[0, 0::feats]processed_feats[0, 1::feats+2] = current_feats[0, 1::feats]processed_feats[0, 2::feats+2] = current_feats[0, 2::feats]processed_feats[0, 3::feats+2] = current_feats[0, 3::feats]processed_feats[0, 4::feats+2] = current_feats[0, 4::feats]processed_feats[0, 5::feats+2] = current_feats[0, 5::feats]processed_feats[0, 6::feats+2] = 0processed_feats[0, 7::feats+2] = 0在哪里feats = 6current_feats is a (1,132) numpy arrayand the size of processed_feats should be (1,176) and have the following format [feat1_1,feat2_1...feat6_1,0,0,feat1_2,feat2_2...]我正在尝试将它变成一行代码或更少的代码行(如果新解决方案的效率低于现有代码,那么我将回到旧方法)。到目前为止,我已经尝试使用 numpy insertprocessed_feats = np.insert(current_feats,range(6,len(current_feats[0]),feats+2),0)但这并没有考虑在数组末尾添加值,我必须使用两个插入命令,因为我需要在每个 feats+2 索引处添加两个 0。
查看完整描述

1 回答

?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

将两个数组重新整形为 22x8 和 22x6,操作简单地变为将第二个数组写入第一个数组的前 6 列并将零写入其他列:


reshaped = processed_feats.reshape((22, 8))

reshaped[:, :6] = current_feats.reshape((22, 6))

reshaped[:, 6:] = 0

reshaped是 的视图processed_feats,因此将数据写入reshaped到processed_feats。


查看完整回答
反对 回复 2023-02-12
  • 1 回答
  • 0 关注
  • 195 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号