猿问

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

我正在尝试重写以下代码,


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] = 0

processed_feats[0, 7::feats+2] = 0

在哪里


feats = 6

current_feats is a (1,132) numpy array


and 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 insert


processed_feats = np.insert(current_feats,range(6,len(current_feats[0]),feats+2),0)

但这并没有考虑在数组末尾添加值,我必须使用两个插入命令,因为我需要在每个 feats+2 索引处添加两个 0。


慕的地6264312
浏览 196回答 1
1回答

尚方宝剑之说

将两个数组重新整形为 22x8 和 22x6,操作简单地变为将第二个数组写入第一个数组的前 6 列并将零写入其他列:reshaped = processed_feats.reshape((22, 8))reshaped[:, :6] = current_feats.reshape((22, 6))reshaped[:, 6:] = 0reshaped是 的视图processed_feats,因此将数据写入reshaped到processed_feats。
随时随地看视频慕课网APP

相关分类

Python
我要回答