2012-07-25 86 views
2

概率阵的我有一个包含像下面的例子中一个物种的出现 概率值的numpy的数组(从GIS栅格地图实际进口):离散化在Python

a = random.randint(1.0,20.0,1200).reshape(40,30) 
b = (a*1.0)/sum(a) 

现在我希望得到一个该阵列的离散版本再次。就像我有 例如位于该阵列区域的100个人(1200个细胞)他们如何分发 ?当然,它们应该根据它们的概率分布,意味着较低的值表示较低的发生概率。但是,由于所有数据都是统计数据,因此个人的机会仍然很小,可能性不大。它应该是可能的,多个人可以对细胞占据...

它就像再次转化连续分布曲线为直方图。像许多不同的直方图可能会导致一定的分布曲线,它也应该是相反的。因此,应用我所寻找的算法将会每次产生不同的离散值。

...是否有任何算法在python中可以做到这一点?由于我不熟悉离散化,也许有人可以提供帮助。

+0

“*还有一个单独的位于低概率细胞的机会。*” - 这意味着你要另一个随机函数。它应该是多么随意?没有随机函数,对于相同的给定输入,总是得到相同的结果。 – eumiro 2012-07-25 13:07:46

回答

3

使用random.choicebincount

np.bincount(np.random.choice(b.size, 100, p=b.flat), 
      minlength=b.size).reshape(b.shape) 

如果你没有NumPy的1.7,你可以替换random.choice有:

np.searchsorted(np.cumsum(b), np.random.random(100)) 

,并提供:

np.bincount(np.searchsorted(np.cumsum(b), np.random.random(100)), 
      minlength=b.size).reshape(b.shape) 
+0

我不是那么熟悉Python,但我无法得到模块(random.choice)工作:>>> import numpy >>> numpy.version。版本 '1.6.2' >>> numpy.random.choice() 回溯(最近通话最后一个): 文件 “”,1号线,在 numpy.random.choice() AttributeError的: '模块'对象没有任何属性'选择' – Johannes 2012-07-25 13:38:46

+0

@Johannes'random.choice'在NumPy 1.7中添加;你有哪个版本?试试我的替代解决方案,如果你有1.6(检查'np.version.version')。 – ecatmur 2012-07-25 13:46:57

1

这类似于到我本月早些时候的question

import random 
def RandFloats(Size): 
    Scalar = 1.0 
    VectorSize = Size 
    RandomVector = [random.random() for i in range(VectorSize)] 
    RandomVectorSum = sum(RandomVector) 
    RandomVector = [Scalar*i/RandomVectorSum for i in RandomVector] 
    return RandomVector 

from numpy.random import multinomial 
import math 
def RandIntVec(ListSize, ListSumValue, Distribution='Normal'): 
    """ 
    Inputs: 
    ListSize = the size of the list to return 
    ListSumValue = The sum of list values 
    Distribution = can be 'uniform' for uniform distribution, 'normal' for a normal distribution ~ N(0,1) with +/- 5 sigma (default), or a list of size 'ListSize' or 'ListSize - 1' for an empirical (arbitrary) distribution. Probabilities of each of the p different outcomes. These should sum to 1 (however, the last element is always assumed to account for the remaining probability, as long as sum(pvals[:-1]) <= 1). 
    Output: 
    A list of random integers of length 'ListSize' whose sum is 'ListSumValue'. 
    """ 
    if type(Distribution) == list: 
     DistributionSize = len(Distribution) 
     if ListSize == DistributionSize or (ListSize-1) == DistributionSize: 
      Values = multinomial(ListSumValue,Distribution,size=1) 
      OutputValue = Values[0] 
    elif Distribution.lower() == 'uniform': #I do not recommend this!!!! I see that it is not as random (at least on my computer) as I had hoped 
     UniformDistro = [1/ListSize for i in range(ListSize)] 
     Values = multinomial(ListSumValue,UniformDistro,size=1) 
     OutputValue = Values[0] 
    elif Distribution.lower() == 'normal': 
     """ 
      Normal Distribution Construction....It's very flexible and hideous 
      Assume a +-3 sigma range. Warning, this may or may not be a suitable range for your implementation! 
      If one wishes to explore a different range, then changes the LowSigma and HighSigma values 
      """ 
      LowSigma = -3#-3 sigma 
      HighSigma = 3#+3 sigma 
      StepSize = 1/(float(ListSize) - 1) 
      ZValues  = [(LowSigma * (1-i*StepSize) +(i*StepSize)*HighSigma) for i in range(int(ListSize))] 
      #Construction parameters for N(Mean,Variance) - Default is N(0,1) 
      Mean  = 0 
      Var   = 1 
      #NormalDistro= [self.NormalDistributionFunction(Mean, Var, x) for x in ZValues] 
      NormalDistro= list() 
      for i in range(len(ZValues)): 
       if i==0: 
        ERFCVAL = 0.5 * math.erfc(-ZValues[i]/math.sqrt(2)) 
        NormalDistro.append(ERFCVAL) 
       elif i == len(ZValues) - 1: 
        ERFCVAL = NormalDistro[0] 
        NormalDistro.append(ERFCVAL) 
       else: 
        ERFCVAL1 = 0.5 * math.erfc(-ZValues[i]/math.sqrt(2)) 
        ERFCVAL2 = 0.5 * math.erfc(-ZValues[i-1]/math.sqrt(2)) 
        ERFCVAL = ERFCVAL1 - ERFCVAL2 
        NormalDistro.append(ERFCVAL) 
      #print "Normal Distribution sum = %f"%sum(NormalDistro) 
      Values = multinomial(ListSumValue,NormalDistro,size=1) 
      OutputValue = Values[0] 
     else: 
      raise ValueError ('Cannot create desired vector') 
     return OutputValue 
    else: 
     raise ValueError ('Cannot create desired vector') 
    return OutputValue 

ProbabilityDistibution = RandFloats(1200)#This is your probability distribution for your 1200 cell array 
SizeDistribution = RandIntVec(1200,100,Distribution=ProbabilityDistribution)#for a 1200 cell array, whose sum is 100 with given probability distribution 

是重要的两条主线是在代码的最后两行以上

2

到目前为止,我认为ecatmur的答案似乎很合理,简单。

我只是想添加一个更“应用”的例子。考虑一个骰子 与6面(6个数字)。每个数字/结果的概率为1/6。 显示在阵列的形式中,骰子可能看起来像:

b = np.array([[1,1,1],[1,1,1]])/6.0 

因此滚动的骰子100倍(n=100)结果在下面的仿真:

np.bincount(np.searchsorted(np.cumsum(b), np.random.random(n)),minlength=b.size).reshape(b.shape) 

我认为可以是这样的一种合适的方法应用。 因此,谢谢你ecatmur的帮助!

/约翰内斯

+0

我仍然不确定是否使用该程序来模拟例如100个骰子? – Johannes 2012-08-08 11:01:26