python-3.x 我如何检查随机打印变量的概率并计算其频率?

46qrfjad  于 2023-04-08  发布在  Python
关注(0)|答案(3)|浏览(119)

我想检查/测试三个变量的打印概率(单独和单独),
例如,我想采取许多随机抽奖和计数的频率,每个值,或类似的东西。我怎么能?

import random

a = "Word A1", "Word A2", "Word A3", "Word A4"

b = "Word B1", "Word B2", "Word B3", "Word B4"

c = "Word C1", "Word C2", "Word C3", "Word C4"

a_random = random.choice(a)
b_random = random.choice(b)
c_random = random.choice(c)

sentence = a_random + ", " + b_random + ", " + c_random

print(sentence)
f2uvfpb9

f2uvfpb91#

例如,我想采取许多随机抽奖和计数的频率,每个值,或类似的东西,我怎么能?
最简单的方法是编写一个循环,计算每次随机抽奖的频率。例如:

import random
from collections import Counter

a = "Word A1", "Word A2", "Word A3", "Word A4"

b = "Word B1", "Word B2", "Word B3", "Word B4"

c = "Word C1", "Word C2", "Word C3", "Word C4"

a_counter = Counter()
for i in range(1000):
    a_random = random.choice(a)
    a_counter[a_random] += 1

b_counter = Counter()
for i in range(1000):
    b_random = random.choice(b)
    b_counter[b_random] += 1

c_counter = Counter()
for i in range(1000):
    c_random = random.choice(c)
    c_counter[c_random] += 1

print(a_counter)
print(b_counter)
print(c_counter)

输出显示每个单词被选中的次数。

Counter({'Word A1': 252, 'Word A4': 251, 'Word A2': 251, 'Word A3': 246})
Counter({'Word B1': 265, 'Word B4': 265, 'Word B3': 250, 'Word B2': 220})
Counter({'Word C1': 266, 'Word C3': 264, 'Word C4': 236, 'Word C2': 234})
n6lpvg4x

n6lpvg4x2#

如何检查 * 重复 * 随机性的属性

我想你很困惑,为什么对随机数生成器的调用序列没有系统地循环所有的选项,一个接一个?
如果是的话,那就不是随机的。
尝试这样的代码,它显示了单独调用一个随机 * 单词A* 的4个版本会发生什么。为了简洁,我调用了单词A的4个版本,“P”,“Q”,“R”,“S”,以避免重复“word_A”并避免使用数字(可能与频率混淆)。
它运行了数千次,并列出了单词A的每个序列的频率。

  • 哪种模式最常见?
  • PPPP比PQRS更常见吗?(尝试多次运行该程序)
  • 若然,原因为何;若否,原因为何?
  • 在多大比例的情况下“P”根本不出现。
  • 第二个符号与第一个符号相同的情况占多大比例?

对这些问题的答案的研究是概率论和统计学的基础。

import random

n_repetitions = 10000
n_options = 4
length = 4

histo = {}
for i in range(n_repetitions):
    string = ""
    for character in range(length):
      string += chr(ord("O")+random.randint(1,n_options))
    if string not in histo:
       histo[string] =0
    histo[string]+=1

keys = sorted(histo.keys())
print("Seq  Frequency")
for key in keys:
  print (key, histo[key])

输出示例

但每次跑步都不一样!

Seq  Frequency
PPPP 48
PPPQ 36
PPPR 39
PPPS 47
PPQP 34
PPQQ 43
PPQR 44
PPQS 39
PPRP 32
PPRQ 36
PPRR 42
PPRS 36
PPSP 36
PPSQ 33
PPSR 38
PPSS 29
PQPP 38
PQPQ 30
PQPR 36
... etc, to SSSS
cuxqih21

cuxqih213#

我会这样做(一个班轮!):

import numpy as np

# Change these as needed
groups = [["Word A1", "Word A2", "Word A3", "Word A4"], 
          ["Word B1", "Word B2", "Word B3", "Word B4"],
          ["Word C1", "Word C2", "Word C3", "Word C4"]]
m = 100

# Choose 1 word from each group of words (of which there are n), m times 
results = np.array([[np.random.randint(0, len(group)) for group in groups] for _ in range(m)])

这将产生一个m x n的选择数组。例如,您的示例的一次迭代可能会产生一个数组[1,2,0],它将对应于句子“Word A2,Word B3,Word C1”。要了解Word A2被选择了多少次,您可以简单地使用np.unique

import numpy as np

# keep in mind that word_a2_index = 1

unique, counts = np.unique(results[:,1], return_counts=True)

dict(zip(unique, counts)) # {0: 7, 1: 4, 2: 1, 3: 2, 4: 1}

或者重建句子:

def reconstruct(row):
    ", ".join([groups[i][row[i]] for i in range(len(row))])

np.apply_along_axis(reconstruct, axis=1, results)

相关问题