简体   繁体   English

Python / Scipy中的伯努利分布

[英]Bernoulli distribution in Python/Scipy

I'm trying to use the Bernoulli distribution to generate a matrix in which each line cell has a probability of line_id/total_lines to be 1.0 . 我正在尝试使用伯努利分布生成一个矩阵,其中每个行单元格的line_id/total_lines概率为1.0

That's my code: 那是我的代码:

from scipy.stats import bernoulli
import numpy

img_size = 100
img_number = 100

res = numpy.zeros((img_number+1, 6))

image_files = []
for i in range(1):
    image_base = Dt.Data(xd=img_size, yd=img_size)
    for p in numpy.arange(0.0, 1.0, 1.0/img_size):
        s = bernoulli.rvs(p, size=img_size)
        image_base.data[int(p * img_size), ...] = s
        if not s.any() == True:
            print int(p * img_size), s
    if i == 0:
        Dv.DataVisualization.plot_data(image_base, 'bin'+str(i))
    image_files.append(image_base)

from PIL import Image

def plot_data(data, file_path):
    output = Image.fromarray(numpy.uint8(data.data * 255))
    output.save(file_path + '.png', 'PNG')

However, for each image generated I'm getting a line (that's not the first one), fulfilled by zeros. 但是,对于生成的每个图像,我都会得到一行(不是第一行),并由零填充。 That's a least odd: 至少这很奇怪:

在此处输入图片说明在此处输入图片说明在此处输入图片说明

This: 这个:

if not s.any() == True:
    print int(p * img_size), s

printed just the first line. 仅打印第一行。 However, I still can see three lines (always the same lines) fulfilled by 0 over all images. 但是,我仍然可以看到在所有图像上三行(总是相同的行)被0满足。

I think that you may be misusing Numpy's all() and any() . 我认为您可能会滥用Numpy的all()any() The expression s.any() evaluates to a boolean. 表达式s.any()计算为布尔值。

If I want to determine whether I have a Numpy array whose elements are all zero, I should check the condition not s.any() == True . 如果要确定我是否有一个元素均为零的Numpy数组,则应检查条件not s.any() == True

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

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