如何生成具有不同稀疏度级别的二进制模式?

时间:2019-02-15 19:29:55

标签: python numpy

我想生成一组具有不同指定稀疏度的二进制模式。我遇到的问题是

  1. 每个稀疏度级别的模式数量相同,我希望它是随机的。
  2. 由于我正在使用int()函数,因此我对生成的模式数量没有太多控制。
import numpy as np
def rand_bin_array(p,N):
    arr = np.zeros(N)
    K=int(p*N)
    arr[:K] = 1
    #np.random.seed()
    np.random.shuffle(arr)
    return arr
perc1 = 0.1
perc2 = 0.6
perc = np.linspace(perc1,perc2,80) #perc1 and perc2 are the start and end of my sparsity range 
x = []
bits = 250
for i in perc:
    for j in range(int(1100/len(perc))):  #j is a counter for the loop. 1100 are the original number of patterns I want.
        x.append(rand_bin_array(i,bits))

预期结果: 1100种不同稀疏度的模式

实际结果: 1040种具有不同稀疏度级别的模式,我知道此问题的原因,但我想不出解决方法。

1 个答案:

答案 0 :(得分:1)

这应该可以帮助您-我添加了一些常量:

import numpy as np

def rand_bin_array(p,N):
    arr = np.zeros(N)
    K = int(p/100.0*N)            # create that many % ones
    arr[:K] = 1
    np.random.shuffle(arr)
    return arr

perc1 = 20                          # between 20%
perc2 = 70                          # and 70%
perc = np.linspace(perc1, perc2, 5) # create 5 values  
print(perc)

bits = 50                           # create arrays of 50 values
how_many = 2                        # two for each %
x = []
for percent_ones in perc:
    for _ in range(how_many):
        # create 10 different bit-arrays with percent ones
        x.append(rand_bin_array(percent_ones,bits))

print(x)

要获得以下输出:

[20.  32.5   45.  57.5  70. ]  # 5 percentages between 20 and 70%

# around 20%ish
[array([0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 1., 0., 0., 1., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 1., 0., 0., 0.,
       0., 1., 0., 0., 0., 0., 0., 0., 1., 1., 0., 0., 0., 0., 0., 0.]), 
 array([0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 1., 0., 1., 1., 1., 0.,
       0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0.]), 
 # around 30%ish
 array([0., 1., 0., 0., 1., 0., 0., 0., 0., 1., 1., 0., 0., 1., 0., 0., 1.,
       1., 1., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 1., 1., 0., 0., 0.,
       0., 1., 0., 0., 1., 0., 0., 0., 0., 0., 1., 1., 1., 0., 0., 0.]), 
 array([0., 1., 1., 0., 0., 0., 1., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0.,
       1., 0., 0., 1., 0., 0., 1., 1., 1., 0., 0., 0., 0., 1., 0., 1., 0.,
       0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 1., 0., 0., 1.]), 
 # around 45%ish
 array([0., 0., 1., 1., 1., 1., 1., 0., 1., 0., 0., 0., 1., 0., 0., 0., 1.,
       0., 0., 0., 0., 1., 1., 0., 0., 1., 1., 1., 1., 0., 0., 1., 0., 0.,
       0., 1., 0., 1., 0., 1., 0., 1., 1., 0., 0., 0., 0., 1., 0., 1.]), 
 array([0., 1., 0., 0., 1., 1., 0., 1., 0., 0., 1., 1., 0., 0., 1., 0., 1.,
        0., 1., 1., 1., 0., 0., 0., 1., 0., 0., 1., 1., 1., 0., 1., 0., 0.,
        0., 1., 0., 0., 0., 0., 1., 1., 0., 0., 1., 0., 0., 1., 1., 0.]), 
 # around 57%ish
 array([1., 1., 0., 0., 1., 1., 1., 0., 1., 0., 1., 0., 0., 0., 0., 1., 0.,
        1., 1., 1., 1., 1., 0., 0., 1., 0., 1., 0., 0., 1., 1., 1., 1., 0.,
        1., 0., 1., 0., 1., 1., 0., 0., 0., 1., 1., 1., 1., 0., 0., 1.]), 
 array([0., 1., 0., 1., 0., 0., 1., 1., 0., 1., 0., 0., 0., 1., 1., 0., 1.,
        0., 1., 1., 1., 0., 0., 0., 1., 1., 1., 1., 0., 0., 1., 1., 1., 0.,
        1., 0., 1., 0., 1., 1., 1., 0., 0., 1., 1., 0., 1., 1., 1., 0.]), 
 # around 70%ish
 array([1., 1., 1., 1., 1., 1., 1., 1., 1., 0.,  0., 1., 0., 1., 1., 0., 1.,
        1., 1., 1., 0., 0., 0., 1., 0., 1., 1., 0., 1., 1., 1., 1., 1., 1.,
        0., 1., 1., 1., 1., 0., 1., 0., 1., 0., 1., 1., 0., 0., 1., 1.]), 
 array([1., 1., 0., 1., 1., 1., 1., 1., 0., 0., 0., 1., 1., 1., 1., 1., 1.,
        1., 1., 1., 1., 0., 1., 0., 1., 1., 0., 1., 1., 0., 0., 1., 1., 1.,
        1., 1., 0., 1., 0., 0., 0., 1., 1., 1., 1., 1., 0., 0., 1., 1.])]

您可以使用与np.geomspace()相同大小的perc并修改每个百分比生成的金额:

import numpy as np

def rand_bin_array(p,N):
    arr = np.zeros(N)
    K = int(p/100.0*N)            # create that many % ones
    arr[:K] = 1
    np.random.shuffle(arr)
    return arr

perc1 = 20                          # between 20%
perc2 = 70                          # and 70%
perc = np.linspace(perc1, perc2, 5) # create 5 values 

bits = 50                           # create arrays of 50 values 
total = 100                         # total arrays

# initial amounts for total//2
how_many = np.geomspace(1,total//2,len(perc),True,int)
# add up on parts to reach total
how_many = [x + (total-sum(how_many)//len(how_many)) for x in how_many]
how_many[-1] += total-sum(how_many)

x = []
for count,percent_ones in zip(how_many,perc):
    for j in range(count):
        # create 10 different bit-arrays with percent ones
        x.append(rand_bin_array(percent_ones,bits))

print(x)