简体   繁体   English

Python - 如何按频率检查数字组合

[英]Python - How to check the combination of numbers by frequency

Let's have for example the following data.例如,让我们有以下数据。

 h: [Num1, Num2, Num3, Num4, Num5, Num6]
 a: [1,       2,    3,    4,    5,    6]
 b: [1,       2,    7,    8,    9,   10]
 c: [1,       2,    3,    6,    8,   10]

Now, let's say I want to see combinations of 2+ ordered by frequency.现在,假设我想查看按频率排序的 2+ 组合。

Let's take number:1 for example, it appears in all our three rows a, b, c.我们以数字:1 为例,它出现在我们所有的三行 a、b、c 中。

When 1 is "used", it's usually paired with 2 (3/3), followed by 3, 6, 8, 10 (2/3).当 1 被“使用”时,它通常与 2 (3/3) 配对,然后是 3, 6, 8, 10 (2/3)。 In other words, when 1 is "used" there is a chance it looks something like this:换句话说,当“使用” 1 时,它有可能看起来像这样:

 [1, 2, x, y, z, t]
 [1, 2, 3, x, y, z]
 [1, 2, 6, x, y, z]
 .
 .
 .
 [1, 8, x, y, z, t]
 [1, 10, x, y, z, t]
 [1, 2, 3, 6, 8, 10]

Order does not matter.顺序无所谓。 x, y, z, t could be any given number. x, y, z, t 可以是任何给定的数字。 Duplicates are not present/allowed.不存在/不允许重复。

I have a data frame with this format and want to see what other integers come in combination with, for example, 44.我有一个具有这种格式的数据框,想看看还有什么其他整数结合在一起,例如 44。

For example:例如:

 44 was paired with 11, 350 times out of 2000
 44 was paired with 27, 290 times out of 2000
 44 was paired with 35, 180 times out of 2000
 .
 .
 .
 44 was paired with 2, 5 times out of 2000

I have the frequency of which every number occurs in each column, I just can't figure out how to continue this.我有每列中每个数字出现的频率,我只是不知道如何继续这个。

Looking forward to ideas and questions.期待想法和问题。 Thank you!谢谢!

You could use Counter from the itertools module您可以使用 itertools 模块中的Counter

from itertools import combinations
from collections import Counter
data = [[1, 2, 3],[1, 2, 5],[1, 3, 8],[2, 5, 8]]
pairings = Counter(
    pair for row in data 
    for pair in combinations(sorted(row), 2)
)

The Counter object is dictionary like.计数器 object 类似于字典。

Counter({
    (1, 2): 2, 
    (1, 3): 2, 
    (2, 5): 2, 
    (2, 3): 1, 
    (1, 5): 1, 
    (1, 8): 1, 
    (3, 8): 1, 
    (2, 8): 1, 
    (5, 8): 1
})

You can get the count of a specific pair like this:您可以像这样获得特定对的计数:

>>> pairings[1,2] 
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM