数组中非零的概率乘以它自我反转

时间:2017-09-18 19:45:12

标签: probability python

我有一个70%的数组losses_vector。然后我将它乘以逆转的自我。

例如:

  [0 1 0 1 1 1]
X [1 1 1 0 1 0] = [0 1 0 0 1 0]

我是否正确地重新计算非零的百分比?

import numpy as np
probs = []
for r in range(100000):
    while True:
        losses_vector = np.random.binomial(1, p=.7, size=n_games)
        pr = (losses_vector.shape[0] - losses_vector[losses_vector==0].shape[0])/float(losses_vector.shape[0])
        if round(pr, 2)==round(.70,2):
            break
    e = losses_vector*losses_vector[::-1]
    non_zero_percentage = (e.shape[0] - e[e==0].shape[0])/float(e.shape[0])
    probs.append(non_zero_percentage)

谢谢。

1 个答案:

答案 0 :(得分:1)

您正在正确计算non_zero_percentage

但我宁愿更倾向于下面的片段;我需要一些时间来阅读你的所作所为。不过,每个人都有自己的风格。干杯!

n = 100000
p = 0.7
l = np.random.binomial(1, p=p, size=n)
non_zero_percentage = np.dot(l,l[::-1])/float(len(l))