如何消除可疑条形码(如123456)d

2024-10-08 19:30:08 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是来自pandas数据库的一些条形码数据

737318  Sikat Botol Pigeon          4902508045506   75170
737379  Natur Manual Breast Pump    8850851860016   75170
738753  Sunlight                    1232131321313   75261
739287  Bodymist bodyshop           1122334455667   75296
739677  Bodymist ale                1234567890123   75367

我想删除可疑的数据(即有太多重复或连续的数字),如123213132131311223344556671234567890123等。我非常容忍误报,但希望尽可能避免误报(坏条形码)。你知道吗


Tags: 数据数据库pandasmanual条形码误报pigeonsunlight
2条回答

作为第一步,我将使用条形码内置的验证机制,校验和。由于您的条形码似乎是GTIN条形码(特别是GTIN-13),因此可以使用this method

>>> import math
>>> def CheckBarcode(s):
        sum = 0
        for i in range(len(s[:-1])):
            sum += int(s[i]) * ((i%2)*2+1)
        return math.ceil(sum/10)*10-sum == int(s[-1])

>>> CheckBarcode("4902508045506")
True
>>> CheckBarcode("8850851860016")
True
>>> CheckBarcode("1232131321313")
True
>>> CheckBarcode("1122334455667")
False
>>> CheckBarcode("1234567890123")
False

如果您担心重复和连续的数字,您可以取这些数字的np.diff,然后使用Kolmogorov Smirnov test与三角形分布进行比较。随机数的连续数字之间的差值应遵循-1010之间的三角形分布,最大值在0

import scipy.stats as stat
t = stat.triang(.5, loc = -10, scale = 20)

将条形码转换为数组:

a = np.array(list(map(list, map(str, a))), dtype = int)  # however you get `a` out of your dataframe

然后用它做一个面具

np.array[stat.kstest(i, t.cdf).pvalue > .5 for i in np.diff(a, axis = 1)]

测试:

np.array([stat.kstest(j, t.cdf).pvalue > .5 for j in np.diff(np.random.randint(0, 10, (1000, 13)), axis = 1)]).sum()

Out: 720

您将有大约30%的假阴性率,但是p值阈值.5应该可以保证您保留的值没有太多连续或重复的数字。如果你真的想确定你已经消除了任何可疑的东西,你可能还想测试实际数字对stat.uniform(scale = 10)(消除1213141516171和类似的)。你知道吗

相关问题 更多 >

    热门问题