在轴= 0的numpy 3d数组中计算零的出现次数

时间:2016-01-08 08:39:36

标签: python arrays numpy

我有一个numpy 3d数组,我想在其中找到零值出现的概率。

因此,首先需要计算轴= 0中存在多少零。

与arr.sum(axis = 0)类似,有任何方法可以返回我的3d数组中具有零点数的2D数组。

>>> print arr
[[[  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   7.43459761e-02 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  ..., 
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   4.58999968e+00
     1.50299997e+01   2.30100002e+01]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   1.86000001e+00
     5.51999998e+00   1.77899990e+01]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]]

 [[  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  ..., 
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   9.39900055e+01
     1.11450005e+02   1.15800003e+02]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   8.20799942e+01
     9.74399948e+01   1.06649994e+02]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]]

 [[  0.00000000e+00   3.74535918e-02   0.00000000e+00 ...,   3.89999986e-01
     9.89999950e-01   9.30000007e-01]
  [  9.29514784e-03   5.75268008e-02   0.00000000e+00 ...,   7.50000000e-01
     9.89999950e-01   1.28999996e+00]
  [  0.00000000e+00   7.26988986e-02   5.94767854e-02 ...,   1.71000004e+00
     1.43999994e+00   7.19999969e-01]
  ..., 
  [  4.54575920e+00   4.91925001e+00   1.09031944e+01 ...,   1.12470001e+02
     9.32400055e+01   6.66599884e+01]
  [  0.00000000e+00   6.33960581e+00   1.05395260e+01 ...,   1.37279984e+02
     1.22159996e+02   7.25400009e+01]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]]

 ..., 
 [[  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     8.99999961e-02   0.00000000e+00]
  ..., 
  [  2.09804267e-01   1.32204843e+00   6.83585852e-02 ...,   7.19999969e-01
     1.49999991e-01   0.00000000e+00]
  [  3.02928180e-01   6.30806535e-02   2.42170334e+00 ...,   4.86000013e+00
     3.98999977e+00   5.48999977e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]]

 [[  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  ..., 
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   5.39999962e-01
     5.99999987e-02   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   1.50000000e+00
     1.19999997e-01   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]]

 [[  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  ..., 
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]
  [  0.00000000e+00   0.00000000e+00   0.00000000e+00 ...,   0.00000000e+00
     0.00000000e+00   0.00000000e+00]]

1 个答案:

答案 0 :(得分:1)

only_z = numpy.copy(arr)
only_z[only_z==0]=1
only_z[only_z!=1]=0
only_z_sum = only_z.sum(axis=0)

prob_of_z = only_z_sum/31

这是我现在能找到的最简单的方法,我有所有出现零的概率。

>>> print prob_of_z
[[ 0.96774194  0.80645161  0.90322581  0.90322581  0.87096774  0.90322581
   0.87096774  0.90322581  0.90322581  0.83870968  0.83870968  0.83870968
   0.87096774  0.93548387  0.90322581  0.93548387  0.90322581  0.96774194]
 [ 0.93548387  0.77419355  0.90322581  0.90322581  0.90322581  0.90322581
   0.87096774  0.87096774  0.90322581  0.80645161  0.77419355  0.80645161
   0.90322581  0.93548387  0.93548387  0.93548387  0.90322581  0.93548387]
 [ 0.80645161  0.80645161  0.83870968  0.87096774  0.87096774  0.83870968
   0.87096774  0.83870968  0.90322581  0.83870968  0.87096774  0.90322581
   0.87096774  0.90322581  0.87096774  0.90322581  0.90322581  0.87096774]
 [ 0.83870968  0.74193548  0.80645161  0.87096774  0.83870968  0.80645161
   0.83870968  0.83870968  0.87096774  0.83870968  0.83870968  0.77419355
   0.77419355  0.77419355  0.77419355  0.83870968  0.80645161  0.80645161]
 [ 0.80645161  0.80645161  0.77419355  0.83870968  0.83870968  0.83870968
   0.83870968  0.83870968  0.80645161  0.77419355  0.77419355  0.74193548
   0.74193548  0.77419355  0.70967742  0.83870968  0.77419355  0.77419355]
 [ 0.77419355  0.77419355  0.74193548  0.77419355  0.80645161  0.77419355
   0.74193548  0.67741935  0.64516129  0.67741935  0.70967742  0.77419355
   0.70967742  0.70967742  0.80645161  0.80645161  0.70967742  0.67741935]
 [ 0.70967742  0.77419355  0.70967742  0.70967742  0.67741935  0.70967742
   0.74193548  0.58064516  0.5483871   0.61290323  0.74193548  0.64516129
   0.67741935  0.74193548  0.74193548  0.70967742  0.74193548  0.74193548]
 [ 0.67741935  0.67741935  0.64516129  0.64516129  0.64516129  0.67741935
   0.61290323  0.58064516  0.58064516  0.58064516  0.64516129  0.64516129
   0.67741935  0.67741935  0.67741935  0.74193548  0.67741935  0.70967742]
 [ 0.61290323  0.64516129  0.64516129  0.67741935  0.64516129  0.61290323
   0.51612903  0.48387097  0.5483871   0.61290323  0.70967742  0.64516129
   0.58064516  0.58064516  0.67741935  0.67741935  0.64516129  0.58064516]
 [ 0.58064516  0.64516129  0.64516129  0.58064516  0.61290323  0.48387097
   0.48387097  0.48387097  0.61290323  0.61290323  0.67741935  0.61290323
   0.58064516  0.61290323  0.64516129  0.67741935  0.74193548  0.64516129]
 [ 0.67741935  0.61290323  0.5483871   0.51612903  0.5483871   0.58064516
   0.51612903  0.58064516  0.58064516  0.61290323  0.58064516  0.5483871
   0.58064516  0.64516129  0.70967742  0.67741935  0.70967742  0.67741935]
 [ 0.74193548  0.70967742  0.48387097  0.48387097  0.48387097  0.51612903
   0.51612903  0.5483871   0.48387097  0.5483871   0.51612903  0.58064516
   0.58064516  0.61290323  0.70967742  0.64516129  0.67741935  0.61290323]
 [ 0.51612903  0.77419355  0.48387097  0.48387097  0.41935484  0.48387097
   0.48387097  0.51612903  0.48387097  0.41935484  0.41935484  0.51612903
   0.5483871   0.5483871   0.64516129  0.58064516  0.64516129  0.61290323]
 [ 0.67741935  0.74193548  0.74193548  0.61290323  0.5483871   0.48387097
   0.48387097  0.38709677  0.38709677  0.41935484  0.4516129   0.51612903
   0.51612903  0.58064516  0.5483871   0.64516129  0.58064516  0.58064516]
 [ 0.70967742  0.70967742  0.70967742  0.67741935  0.41935484  0.41935484
   0.48387097  0.48387097  0.48387097  0.58064516  0.58064516  0.61290323
   0.58064516  0.58064516  0.67741935  0.58064516  0.61290323  0.64516129]
 [ 0.74193548  0.74193548  0.64516129  0.61290323  0.58064516  0.32258065
   0.41935484  0.35483871  0.41935484  0.5483871   0.64516129  0.61290323
   0.61290323  0.51612903  0.51612903  0.5483871   0.51612903  0.64516129]
 [ 0.77419355  0.74193548  0.74193548  0.70967742  0.64516129  0.58064516
   0.35483871  0.38709677  0.48387097  0.5483871   0.61290323  0.58064516
   0.5483871   0.48387097  0.5483871   0.4516129   0.58064516  0.58064516]
 [ 1.          1.          1.          1.          1.          1.          1.
   1.          1.          1.          1.          1.          1.          1.
   1.          1.          1.          1.        ]]
>>>