我的概率向量中的负值

时间:2018-08-25 15:28:28

标签: python numpy vector probability

嗨,我想为我的2维数组创建一个概率向量。我自己编写了一个函数来遍历元素并计算每个值的概率。当我只输入正值时,一切都会起作用,但是一旦出现负数,我就会创建一个负概率,因为值必须为0 <= x <= 1。

def createProbabilityVector(inputArray):
    vector = inputArray
    probabilityVector = np.zeros(vector.shape)
    for x in range(vector.shape[0]):
        vectorSum = sum(vector[x])
        probabilityVector[[x]] = vector[[x]] / vectorSum
    return probabilityVector

是代码中的错误,还是我只是不明白我想做什么?

编辑:一些例子

input
[[ 1.62242568  1.27356428 -1.88008155  1.37183247]
 [-1.10638392  0.18420085 -1.68558966 -1.59951709]
 [ 1.79166467 -0.21911691 -1.29066019  0.4565108 ]
 [-0.20459109  1.59912774  0.47735207  1.6398782 ]]
output:
[[ 0.67948147  0.53337625 -0.78738927  0.57453155]
 [ 0.26296832 -0.04378136  0.4006355   0.38017754]
 [ 2.42642012 -0.2967462  -1.74791851  0.61824459]
 [-0.05825873  0.45536272  0.13592931  0.4669667 ]]
-----
input
[[ 1.50162225 -0.31502279 -1.40281248 -1.09221922]
 [ 1.93663826  1.31671237 -1.14334774  1.54792572]
 [ 1.21376416 -1.44547074  0.0045907   1.4099986 ]
 [ 0.51903455 -0.80046238 -1.69780354 -1.29893969]]
output:
[[-1.14764998  0.24076355  1.0721323   0.83475413]
 [ 0.52943577  0.3599612  -0.31256699  0.42317002]
 [ 1.02610693 -1.2219899   0.00388094  1.19200202]
 [-0.15833053  0.24417956  0.51791182  0.39623914]]
-----
input
[[-1.6333837  -0.50469549 -1.62305585 -1.43558978]
 [ 0.29636416 -0.22401163 -1.82816273  0.10676174]
 [-1.6599302  -0.2516563  -1.64843802 -0.86857615]
 [ 1.31762542  0.8690911   1.5888384  -1.83204102]]
output:
[[ 0.31431022  0.09711799  0.31232284  0.27624895]
 [-0.17971828  0.13584296  1.10861674 -0.06474142]
 [ 0.37482047  0.05682524  0.37222548  0.1961288 ]
 [ 0.67796038  0.44717514  0.81750812 -0.94264364]]
-----
input
[[ 0.15369025  1.05426071 -0.61295255  0.95033555]
 [ 0.04138761 -1.41072628  1.90319561 -1.2563338 ]
 [ 1.85131197 -1.24551221 -1.62731374  0.43129381]
 [ 0.21235188  1.21581691 -0.57470021 -0.58482563]]
output:
[[ 0.09945439  0.68222193 -0.3966473   0.61497099]
 [-0.05728572  1.95262488 -2.63426518  1.73892602]
 [-3.1366464   2.11025017  2.75713    -0.73073377]
 [ 0.79046139  4.52577253 -2.13927148 -2.17696245]]

1 个答案:

答案 0 :(得分:0)

您需要将输入数组的所有值转换为正值,有几种选择:

  • 将所有负数都转换为0,函数zeroed
  • 将所有值移动最小元素的绝对值,函数shifted
  • 将指数函数应用于值exponential

转换输入数组的值后,您可以照常使用函数,请遵循转换函数的定义:

def zeroed(arr):
    return arr.clip(min=0)

def shifted(arr):
    return arr + abs(np.min(arr))

def exponential(arr):
    return np.exp(arr)

在您的函数中,可以按以下方式使用转换:

def createProbabilityVector(inputArray):
    vector = inputArray
    probabilityVector = np.zeros(vector.shape)
    for x in range(vector.shape[0]):
        new_vector = zeroed(vector[x])
        vectorSum = sum(new_vector)
        probabilityVector[[x]] = new_vector / vectorSum
    return probabilityVector 

对于输入,功能zeroed可以用shiftedexponential代替:

array = np.array([[1.62242568, 1.27356428, -1.88008155, 1.37183247],
                  [-1.10638392, 0.18420085, -1.68558966, -1.59951709],
                  [1.79166467, -0.21911691, -1.29066019, 0.4565108],
                  [-0.20459109, 1.59912774, 0.47735207, 1.6398782]])

这些是函数zeroed的结果:

[[0.38015304 0.29841079 0.         0.32143616]
 [0.         1.         0.         0.        ]
 [0.79694165 0.         0.         0.20305835]
 [0.         0.43029432 0.1284462  0.44125948]]

shifted

[[0.35350056 0.31829072 0.         0.32820872]
 [0.22847732 0.73756992 0.         0.03395275]
 [0.52233595 0.18158552 0.         0.29607853]
 [0.         0.41655061 0.15748787 0.42596152]]

exponential

[[0.39778013 0.28063027 0.01198184 0.30960776]
 [0.17223667 0.62606504 0.09651165 0.10518664]
 [0.69307072 0.09279107 0.03177905 0.18235916]
 [0.06504215 0.39494808 0.12863496 0.41137482]]