想象一下,我有一个包含 4 个类的数组或 Pandas 系列,但是每个步骤中的类都有很大的不同。这只是一个示例(如果需要,您可以生成更真实的示例):
import numpy as np
np.random.seed(seed=0) # to generate always the same numbers
x = np.random.randint(4, size=(100))
由于它们变化很大,我想通过多数投票技术或其他方法放大它们(或过滤或重新采样)。也许环顾最近的 5 个样本,然后该特定样本将具有最频繁的类别。最后我会得到一个更“紧凑”的数组。
这个方案应该代表我的意思:
有人可以帮我吗?
答案 0 :(得分:1)
我不会说这很漂亮,但它似乎可以解决问题。
import numpy as np
def majorityVoting(index, array, groupSize=5):
groupSizeFloor = groupSize // 2
arrayCount = len(array)
if index <= groupSizeFloor:
first = 0
last = groupSize
elif index >= arrayCount - (groupSize // 2):
first = arrayCount - groupSize
last = arrayCount
else:
first = index - groupSizeFloor
last = index + groupSizeFloor + 1
return np.bincount(array[first:last]).argmax()
np.random.seed(seed=0)
arr = np.random.randint(4, size=100)
majoritySorted = [majorityVoting(i, arr) for i in range(len(arr))]