3

我正在尝试使用 Python 在 csv 文件的行上实现滑动/移动窗口方法。每行可以有一个带有二进制值的列yesno. 基本上,我想要罕见的yes噪音。这意味着如果说我们在 5 个窗口(最多 5 个)中有3 yes行,请保留它们。但如果有 1 或 2,让我们将它们更改为. 我怎样才能做到这一点?no

例如,以下内容yes都应变为no.

...
1,a1,b1,no,0.75
2,a2,b2,no,0.45
3,a3,b3,yes,0.98
4,a4,b4,yes,0.22
5,a5,b5,no,0.46
6,a6,b6,no,0.20
...

但在下面,我们保持原样(可以有 5 个窗口,其中 3 个是yes):

...
1,a1,b1,no,0.75
2,a2,b2,no,0.45
3,a3,b3,yes,0.98
4,a4,b4,yes,0.22
5,a5,b5,no,0.46
6,a6,b6,yes,0.20
...

我尝试写一些东西,窗口为 5,但卡住了(它不完整):

        window_size = 5 
        filename='C:\\Users\\username\\v3\\And-'+v3file.split("\\")[5]
        with open(filename) as fin:
            with open('C:\\Users\\username\\v4\\And2-'+v3file.split("\\")[5],'w') as finalout:
                line= fin.readline()
                index = 0
                sequence= []
                accs=[]
                while line:
                    print(line)
                    for i in range(window_size):
                        line = fin.readline()
                        sequence.append(line)
                    index = index + 1
                    fin.seek(index)
4

2 回答 2

4

您可以使用设置为所需窗口大小collections.dequemaxlen参数来实现一个滑动窗口,该窗口跟踪最近 5 行的是/否标志。保持对 yeses 的计数,而不是在每次迭代中计算滑动窗口中 yeses 的总和,以提高效率。当您有一个全尺寸的滑动窗口并且 yeses 的计数大于 2 时,将这些 yeses 的行索引添加到应该保持 yeses 原样的集合中。并且在重置输入的文件指针后的第二遍中,如果行索引不在集合中,则将 yeses 更改为 noes:

from collections import deque

window_size = 5
with open(filename) as fin, open(output_filename, 'w') as finalout:
    yeses = 0
    window = deque(maxlen=5)
    preserved = set()
    for index, line in enumerate(fin):
        window.append('yes' in line)
        if window[-1]:
            yeses += 1
        if len(window) == window_size:
            if yeses > 2:
                preserved.update(i for i, f in enumerate(window, index - window_size + 1) if f)
            if window[0]:
                yeses -= 1
    fin.seek(0)
    for index, line in enumerate(fin):
        if index not in preserved:
            line = line.replace('yes', 'no')
        finalout.write(line)

演示:https ://repl.it/@blhsing/StripedCleanCopyrightinfringement

于 2019-12-17T21:25:58.947 回答
0

这是一个基于构建连续列表推导的 5 行解决方案:

lines = [
'1,a1,b1,no,0.75',
'2,a2,b2,yes,0.45',
'3,a3,b3,yes,0.98',
'4,a4,b4,yes,0.22',
'5,a5,b5,no,0.46',
'6,a6,b6,no,0.98',
'7,a7,b7,yes,0.22',
'8,a8,b8,no,0.46',
'9,a9,b9,no,0.20']

n = len(lines)

# flag all lines containing 'yes' (add 2 empty lines at boundaries to avoid pbs)
flags = [line.count('yes') for line in ['', '']+lines+['', '']]
# count number of flags in sliding window [p-2,p+2]
counts = [sum(flags[p-2:p+3]) for p in range(2,n+2)]
# tag lines that need to be changed
tags = [flag > 0 and count < 3 for (flag,count) in zip(flags[2:],counts)]
# change tagged lines
for n in range(n):
  if tags[n]: lines[n] = lines[n].replace('yes','no')

print(lines)

结果:

['1,a1,b1,no,0.75',
 '2,a2,b2,yes,0.45',
 '3,a3,b3,yes,0.98',
 '4,a4,b4,yes,0.22',
 '5,a5,b5,no,0.46',
 '6,a6,b6,no,0.98',
 '7,a7,b7,no,0.22',
 '8,a8,b8,no,0.46',
 '9,a9,b9,no,0.20']

编辑:当您从标准文本文件中读取数据时,您所要做的就是:

with file(filename,'r') as f:
  lines = f.read().strip().split('\n')

(strip 删除文件顶部或底部的潜在空行, split(\n) 将文件内容转换为行列表)然后使用上面的代码...

于 2019-12-17T20:13:21.870 回答