【问题标题】:Lottery analysis for learning学习抽签分析
【发布时间】:2023-01-29 22:16:47
【问题描述】:

我正在尝试学习如何使用 pandas 库。

对于数据源,我使用到目前为止的彩票组合抽奖。

我试图解决的众多任务之一是计算组合中数字对的出现频率。

我从这样的列表中创建一个数据框:

list = [
    [13, 14, 28, 30, 31, 37, 39],
    [7, 10, 12, 16, 21, 22, 33],
    ...,
    [1, 2, 7, 15, 25, 31, 33],
    [3, 6, 18, 21, 31, 34, 39]
]

df = pd.DataFrame(list)
print(df.head())

输出:

.   0   1   2   3   4   5   6
0   9  11  12  18  20  26  35
1  10  13  15  20  21  25  35
2   1   8  17  21  22  27  34
3  10  13  17  18  21  29  37
4   5   8  12  17  19  21  37

例如,结果我想得到两个或三个数字的元组在组合中出现的时间总和:

Pair  : Found n time in all combinations
9,23  : 33
11,32 : 26

你能给我一些指导或例子如何解决这个任务吗?

【问题讨论】:

    标签: python pandas combinations analytics analysis


    【解决方案1】:

    这是一个仅使用标准库中的模块的简单解决方案:

    from itertools import combinations
    from collections import Counter
    
    draws = [
        [13, 14, 28, 30, 31, 37, 39],
        [7, 10, 12, 16, 21, 22, 33],
        [1, 2, 7, 15, 25, 31, 33],
        [3, 6, 18, 21, 31, 34, 39]
    ]
    
    duos = Counter()
    trios = Counter()
    
    for draw in draws:
        duos.update(combinations(draw, 2))
        trios.update(combinations(draw, 3))
    
    print('Top 5 duos')
    for x in duos.most_common(5):
        print(f'{x[0]}: {x[1]}')
    
    print()
    
    print('Top 5 trios')
    for x in trios.most_common(5):
        print(f'{x[0]}: {x[1]}')
    

    上面的代码 sn-p 将产生以下输出:

    Top 5 duos
    (31, 39): 2
    (7, 33): 2
    (13, 14): 1
    (13, 28): 1
    (13, 30): 1
    
    Top 5 trios
    (13, 14, 28): 1
    (13, 14, 30): 1
    (13, 14, 31): 1
    (13, 14, 37): 1
    (13, 14, 39): 1
    
    

    这里还有一点优雅的版本:

    from itertools import combinations
    from collections import Counter
    
    draws = [
        [13, 14, 28, 30, 31, 37, 39],
        [7, 10, 12, 16, 21, 22, 33],
        [1, 2, 7, 15, 25, 31, 33],
        [3, 6, 18, 21, 31, 34, 39]
    ]
    
    counters = [Counter() for _ in range(3)]
    
    for n, counter in enumerate(counters, 2):
        for draw in draws:
            counter.update(combinations(draw, n))
    
        print(f'Top 10 combos of {n} numbers')
    
        for combo, count in counter.most_common(10):
            print(' '.join((f'{_:2d}' for _ in combo)), count, sep=': ')
    
        print()
    

    这将为我们提供以下输出:

    Top 10 combos of 2 numbers
    31 39: 2
     7 33: 2
    13 14: 1
    13 28: 1
    13 30: 1
    13 31: 1
    13 37: 1
    13 39: 1
    14 28: 1
    14 30: 1
    
    Top 10 combos of 3 numbers
    13 14 28: 1
    13 14 30: 1
    13 14 31: 1
    13 14 37: 1
    13 14 39: 1
    13 28 30: 1
    13 28 31: 1
    13 28 37: 1
    13 28 39: 1
    13 30 31: 1
    
    Top 10 combos of 4 numbers
    13 14 28 30: 1
    13 14 28 31: 1
    13 14 28 37: 1
    13 14 28 39: 1
    13 14 30 31: 1
    13 14 30 37: 1
    13 14 30 39: 1
    13 14 31 37: 1
    13 14 31 39: 1
    13 14 37 39: 1
    

    【讨论】:

      【解决方案2】:

      这是它的单行代码:

      from itertools import chain, combinations
      from collections import Counter
      
      lottery = [np.random.randint(1,100, size=6) for _ in range(1000)]
      
      def commmon_combs(matrix, n_common, combs_r):
          return Counter(chain(*[combinations(lottery[i], combs_r) for i in range(len(lottery))])).most_common(n_common)
      
      commmon_combs(lottery, 5, 2)
      
      Output:
      [((78, 21), 36),
       ((13, 67), 35),
       ((22, 86), 34),
       ((29, 61), 34),
       ((19, 99), 34)]
      

      【讨论】:

        【解决方案3】:

        IIUC,您可以找到每一行的所有组合(例如两个值的组合),然后简单地计算:

        from itertools import combinations
        
        (df.apply(lambda x: tuple(combinations(x, r=2)), axis=1)
           .explode()
           .value_counts()
           .sort_values(ascending=False))
        

        大熊猫系列的结果如下:

        (31, 39)    2
        (7, 33)     2
        (13, 28)    1
        (37, 39)    1
        (13, 30)    1
                   ..
        

        r=2 参数更改为 3 个等值的组合。

        【讨论】:

          【解决方案4】:

          我正在尝试获得二重奏和三重奏,但似乎我的列表构建方式略有不同。有解决办法吗。我已将彩票号码导出到 csv,经过一些修改后导入回这里的代码

          import csv
          import pandas as pd
          from itertools import combinations
          from collections import Counter
          
          
          
          results = []
          with open('numerot.csv', newline='') as inputfile:
          for row in csv.reader(inputfile):
              results.append(row[0])
          results=[i.replace(';',',') for i in results]
          
          print(results)
          
          duos = Counter()
          trios = Counter()
          
          for draw in results:
              duos.update(combinations(draw, 2))
              trios.update(combinations(draw, 3))
          
          print('Top 5 duos')
          for x in duos.most_common(5):
              print(f'{x[0]}: {x[1]}')
          
          print()
          
          print('Top 5 trios')
          for x in trios.most_common(5):
             print(f'{x[0]}: {x[1]}')enter code here`
          

          和结果。

          ['1;3;7;17;34;35;39;33', '2;4;6;30;32;33;35;11', '9;12;13;25;26;36;40;29', '2;7;11;22;30;33;35;23', '7;12;13;17;18;24;31;10', '2;5;8;11;15;19;29;38', '7;12;14;17;21;28;35;19', '19;20;23;27;29;32;34;8', '4;17;22;25;27;29;30;1', '1;7;15;19;20;35;38;32', '6;16;17;21;23;30;36;40', '1;6;8;9;15;30;40;17', '9;15;17;18;19;22;30;11', '2;6;13;14;29;30;38;40', '1;13;24;29;31;37;39;32', '8;10;15;19;32;38;40;34', '7;16;19;26;27;30;31;14', '4;5;7;9;19;24;37;40', '13;18;19;27;30;33;40;32', '2;7;12;17;27;38;39;25', '3;6;9;14;16;25;33;2', '3;9;24;25;30;32;34;13', '9;22;28;31;32;34;40;20', '3;4;14;19;20;28;33;7', '17;21;25;27;28;37;40;1', '1;7;16;23;25;32;33;9', '2;18;24;31;33;38;39;23', '15;18;22;25;29;32;34;23', '19;20;22;32;35;36;39;4', '2;8;22;28;32;34;39;14', '9;11;12;13;24;28;34;5', '8;22;27;28;31;34;39;30', '4;8;25;28;29;31;38;21', '4;15;18;23;35;36;40;22', '3;6;12;24;27;28;30;19', '2;7;12;24;26;37;39;29', '1;5;10;12;17;21;22;40', '4;5;10;14;25;26;29;19', '4;5;9;10;15;31;40;1', '1;16;24;27;28;36;40;38', '2;17;19;20;21;30;38;32', '10;12;13;20;24;33;36;8', '3;8;23;28;36;38;39;30', '1;10;13;30;34;35;39;6', '4;7;10;15;16;29;36;23', '7;12;15;22;27;28;31;20', '5;10;19;21;26;32;38;6', '4;13;16;17;18;21;26;20', '4;6;10;15;27;29;40;36', '4;15;26;31;33;34;38;19', '9;13;16;24;25;35;36;10', '1;13;19;28;30;33;35;7', '2;3;7;13;14;22;27;32', '5;6;7;14;27;37;38;40', '10;13;17;19;21;25;30;31']
          
          Top 5 duos
          (';', ';'): 1155
          (';', '3'): 732
          ('1', ';'): 603
          (';', '2'): 561
          ('2', ';'): 524
          
          Top 5 trios
          (';', ';', ';'): 1925
          (';', ';', '3'): 1621
          (';', '2', ';'): 1310
          ('1', ';', ';'): 1276
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-11-09
            • 1970-01-01
            • 2021-07-07
            • 1970-01-01
            • 2015-12-09
            • 2017-09-20
            • 2016-12-31
            相关资源
            最近更新 更多