Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 保留分布的下采样numpy数组_Python_Python 3.x_Numpy_Random_Probability - Fatal编程技术网

Python 保留分布的下采样numpy数组

Python 保留分布的下采样numpy数组,python,python-3.x,numpy,random,probability,Python,Python 3.x,Numpy,Random,Probability,我正在尝试编写一个函数,它可以对具有浮点数的numpy.ndarray进行随机采样,同时保留数组中的数字分布。我现在有这个功能: import random from collections import Counter def sample(A, N): population = np.zeros(sum(A)) counter = 0 for i, x in enumerate(A): for j in range(x):

我正在尝试编写一个函数,它可以对具有浮点数的
numpy.ndarray
进行随机采样,同时保留数组中的数字分布。我现在有这个功能:

import random
from collections import Counter

def sample(A, N):
    population = np.zeros(sum(A))
    counter = 0
    for i, x in enumerate(A):
            for j in range(x):
                    population[counter] = i
                    counter += 1

    sampling = population[np.random.choice(0, len(population), N)]
    return np.histogram(sampling, bins = np.arange(len(A)+1))[0]
因此,我希望函数可以像这样工作(本例中不包括分配会计):

但是,当我将函数应用于这样的数组时,我得到:

TypeError                                 Traceback (most recent call last)
<ipython-input-74-07e3aa976da4> in <module>
----> 1 sample(a, 3)

<ipython-input-63-2d69398e2a22> in sample(A, N)
      3 
      4 def sample(A, N):
----> 5     population = np.zeros(sum(A))
      6     counter = 0
      7     for i, x in enumerate(A):

TypeError: 'numpy.float64' object cannot be interpreted as an integer
TypeError回溯(最近一次调用)
在里面
---->1个样本(a、3)
样品中(A,N)
3.
4 def样品(A,N):
---->5总体=np.零(和(A))
6计数器=0
7对于枚举(A)中的i,x:
TypeError:“numpy.float64”对象不能解释为整数
任何关于修改或创建一个函数的帮助都将非常感谢

In [67]: a = np.array([1.94, 5.68, 2.77, 7.39, 2.51])                                                  
In [68]: np.zeros(sum(a))                                                                              
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-68-263779bc977b> in <module>
----> 1 np.zeros(sum(a))

TypeError: 'numpy.float64' object cannot be interpreted as an integer
但您不需要使用sum:

In [70]: a.shape                                                                                       
Out[70]: (5,)
In [71]: np.zeros(a.shape)                                                                             
Out[71]: array([0., 0., 0., 0., 0.])
事实上,如果
a
是2d,并且您想要具有相同项数的1d数组,那么您想要的是形状的乘积,而不是总和


但是是否要返回与
A
大小完全相同的数组?我以为你想缩小尺寸。

np.zeros的参数应该是一个形状-一个整数,或整数的元组,例如
np.zeros((2,3))
。您的
a/a
是一个浮点数数组,因此
和也将是一个浮点数。那么如何解决这个问题?我尝试将参数改为
np.zeros(sum(A.shape))
,但仍然是相同的错误
In [69]: np.zeros(sum(a.shape))                                                                        
Out[69]: array([0., 0., 0., 0., 0.])
In [70]: a.shape                                                                                       
Out[70]: (5,)
In [71]: np.zeros(a.shape)                                                                             
Out[71]: array([0., 0., 0., 0., 0.])