Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.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_Random_Numpy_Cumsum - Fatal编程技术网

Python 使用Numpy计算范围内的数字

Python 使用Numpy计算范围内的数字,python,random,numpy,cumsum,Python,Random,Numpy,Cumsum,我一直在尝试编写一些代码,将属于某个范围的数字相加,并将相应的数字添加到列表中。我还需要从cumsum范围中提取范围 numbers = [] i=0 z = np.random.rand(1000) arraypmf = np.array(pmf) summation = np.cumsum(z) while i < 6: index = i-1 a = np.extract[condition, z] # I can't figure out how to writ

我一直在尝试编写一些代码,将属于某个范围的数字相加,并将相应的数字添加到列表中。我还需要从cumsum范围中提取范围

numbers = []
i=0

z = np.random.rand(1000)
arraypmf = np.array(pmf)
summation = np.cumsum(z)

while i < 6:
   index = i-1

    a = np.extract[condition, z] # I can't figure out how to write the condition.
    length = len(a)
    length * numbers.append(i)
number=[]
i=0
z=np.rand.rand(1000)
arraypmf=np.array(pmf)
求和=np.cumsum(z)
而我<6:
指数=i-1
a=np.extract[condition,z]#我不知道如何编写条件。
长度=长度(a)
长度*数字。追加(i)

我不完全确定您想做什么,但在
numpy
中执行条件的最简单方法是将它们应用于整个阵列以获得掩码:

mask = (z >= 0.3) & (z < 0.6)
毕竟,如果你所做的只是对事情进行总结,
0
与不存在的情况相同,你可以用
count\u non-zero
替换
len

例如:

In [588]: z=np.random.rand(10)
In [589]: z
Out[589]: 
array([ 0.33335522,  0.66155206,  0.60602815,  0.05755882,  0.03596728,
        0.85610536,  0.06657973,  0.43287193,  0.22596789,  0.62220608])
In [590]: mask = (z >= 0.3) & (z < 0.6)
In [591]: mask
Out[591]: array([ True, False, False, False, False, False, False,  True, False, False], dtype=bool)
In [592]: z * mask
Out[592]: 
array([ 0.33335522,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.43287193,  0.        ,  0.        ])
In [593]: np.count_nonzero(z * mask)
Out[593]: 2
In [594]: np.extract(mask, z)
Out[594]: array([ 0.33335522,  0.43287193])
In [595]: len(np.extract(mask, z))
Out[595]: 2
[588]中的
z=np.rand.rand(10)
In[589]:z
出[589]:
阵列([0.33335522,0.66155206,0.60602815,0.05755882,0.03596728,
0.85610536,  0.06657973,  0.43287193,  0.22596789,  0.62220608])
在[590]中:掩码=(z>=0.3)和(z<0.6)
在[591]中:掩码
Out[591]:数组([True,False,False,False,False,False,True,False,False],dtype=bool)
In[592]:z*掩模
Out[592]:
数组([0.33335522,0,0,0,0,0,0,,
0.        ,  0.        ,  0.43287193,  0.        ,  0.        ])
在[593]中:np.count_非零(z*掩码)
Out[593]:2
在[594]中:np.提取(掩码,z)
Out[594]:数组([0.33335522,0.43287193])
在[595]中:len(np.extract(mask,z))
Out[595]:2

以下是另一种方法(我认为)您正在尝试做的事情:

import numpy as np
z = np.random.rand(1000)
bins = np.asarray([0, .1, .15, 1.])

# This will give the number of values in each range
counts, _ = np.histogram(z, bins)

# This will give the sum of all values in each range
sums, _ = np.histogram(z, bins, weights=z)

不清楚你想在这里做什么。为什么要创建从未使用过的
索引
变量?
cumsum
与任何事情都有什么关系?什么是
length*number.append(i)
应该做什么?(
list.append
返回
None
,即使它返回了您期望的结果,您也不会将
length*
的结果分配给任何内容。)循环应该循环什么?(因为你从不修改
i
i<6
将永远正确,你只需在
0
后面添加
0
数字
)也许你可以用伪代码解释你不知道怎么写?我放弃了猜测OP的意图,认为没有希望,但这似乎是一个很好的猜测…
import numpy as np
z = np.random.rand(1000)
bins = np.asarray([0, .1, .15, 1.])

# This will give the number of values in each range
counts, _ = np.histogram(z, bins)

# This will give the sum of all values in each range
sums, _ = np.histogram(z, bins, weights=z)