numpy数组的分组平均值?

编程入门 行业动态 更新时间:2024-10-25 04:14:47
本文介绍了numpy数组的分组平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个大的numpy数组,尺寸为 [1] .我想找出一种组平均值".更具体地说,

I have a large numpy array, with dimensions [1]. I want to find out a sort of "group average". More specifically,

让我的数组为 [1,2,3,4,5,6,7,8,9,10] 并让我的 group_size 为 3 .因此,我将平均前三个元素,第 4 到第 6 个元素,第 7 到第 9 个元素,并对其余元素求平均(在这种情况下,只有 1 才能得到 - [2, 5, 8, 10].不用说,我需要一个向量化的实现.

Let my array be [1,2,3,4,5,6,7,8,9,10] and let my group_size be 3. Hence, I will average the first three elements, the 4th to 6th element, the 7th to 9th element, and average the remaining elements (only 1 in this case to get - [2, 5, 8, 10]. Needless to say, I need a vectorized implementation.

最后,我的目的是减少嘈杂图中的点数,以平滑具有大量振荡的一般模式.有没有正确的方法来做到这一点?如果两个问题的答案不同,我想回答两个问题.谢谢!

Finally, my purpose is reducing the number of points in a noisy graph to smoothen out a general pattern having a lot of oscillation. Is there a correct way to do this? I would like the answer to both questions, in case they have a different answer. Thanks!

推荐答案

一个好的平滑函数是 内核卷积.它的作用是将移动窗口中的小数组乘以较大的数组.

A good smoothing function is the kernel convolution. What it does is it multiplies a small array in a moving window over your larger array.

假设您选择的标准平滑内核 1/3 * [1,1,1] 并将其应用于数组(内核需要进行奇数编号和规范化).让我们把它应用到 [1,2,2,7,3,4,9,4,5,6]:

Say you chose a standard smoothing kernel of 1/3 * [1,1,1] and apply it to an array (a kernel needs to be odd-numbered and normalized). Lets apply it to [1,2,2,7,3,4,9,4,5,6]:

内核的中心位于第一个 2.然后,它对自己及其邻居进行平均,然后继续前进.结果是这样的:[1.67, 3.67, 4.0, 4.67, 5.33, 5.67, 6.0, 5.0]

The centre of the kernal to begin with is on the first 2. It then averages itself and its neighbours, then moves on. The result is this: [1.67, 3.67, 4.0, 4.67, 5.33, 5.67, 6.0, 5.0]

请注意,数组缺少第一个和最后一个元素.

Note that the array is missing the first and last element.

您可以使用 numpy.convolve,例如:

import numpy as np a = np.array([[1,2,2,7,3,4,9,4,5,6]]) k = np.array([1,1,1])/3 smoothed = np.convolve(x, k, 'valid')

这样做的结果是,您的中心价值被其邻居的价值所平滑.你可以通过增加卷积核的大小来改变卷积核,例如 [1,1,1,1,1]/5,或者给它一个高斯,这会给中心成员带来更多的压力外面的.阅读维基百科文章.

The effect of this is that your central value is smoothed with the values from its neighbours. You can change the convolution kernel by increasing it in size, 5 for example [1,1,1,1,1]/5, or give it a gaussian, which will stress the central members more than the outside ones. Read the wikipedia article.

编辑

这可以按照问题的要求获得块平均:

This works to get a block average as the question asks for:

import numpy as np a = [1,2,3,4,5,6,7,8,9,10] size = 3 new_a = [] i = 0 while i < len(a): val = np.mean(a[i:i+3]) new_a.append(val) i+=size print(new_a) [2.0, 5.0, 8.0, 10.0]

更多推荐

numpy数组的分组平均值?

本文发布于:2023-10-28 05:22:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平均值   数组   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!