python - 如何使用numpy对每2个连续向量求和
问题描述
如何使用 numpy 对每 2 个连续向量求和。或每 2 个连续向量的平均值。列表列表(可以有偶数或奇数个向量。)示例:
[[2,2], [1,2], [1,1], [2,2]] --> [[3,4], [3,3]]
也许像这样,但使用 numpy 和实际适用于向量数组而不是整数数组的东西。或者如果存在的话,可能是某种数组理解。
def pairwiseSum(lst, n):
sum = 0;
for i in range(len(lst)-1):
# adding the alternate numbers
sum = lst[i] + lst[i + 1]
解决方案
def mean_consecutive_vectors(lst, step):
idx_list = list(range(step, len(lst), step))
new_lst = np.split(lst, idx_list)
return np.mean(new_lst, axis=1)
也可以用np.sum()
代替来完成np.mean()
。
推荐阅读
- javascript - 我想将 LearnMoreButton 文本与 h4 文本对齐。与现有的,了解更多显示在下一行
- python - 如何从 python 中的日期中删除 00:00:00
- c++ - 将字符数组作为输入时,VS Code 退出而不显示任何输出
- java - 当我向新保护的 SpringBoot REST API 发送 POST 请求时,我收到 403 错误
- excel - Excel IF 包含文本且不包含文本 THEN 公式
- azure-cosmosdb - 最终用户的 Cosmos DB 简单 ID 字段
- nullpointerexception - 绘制处理结束时出现空指针异常3
- database - 查找产品销售额总和最高的 10 个用户
- angular - 如何从服务中封装的流中获取已检查的用户?
- java - 在android BLE中读取超过20个字节