Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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列表的阈值_Python_Arrays_List_Threshold - Fatal编程技术网

使用多个值设置python列表的阈值

使用多个值设置python列表的阈值,python,arrays,list,threshold,Python,Arrays,List,Threshold,我有一个1000×100的随机数数组。我想用多个数字的列表来设置这个列表的阈值;这些数字从[3到9]。如果它们高于阈值,我希望将行的总和附加到列表中 我试过很多方法,包括3次有条件的。现在,我已经找到了一种将数组与数字列表进行比较的方法,但每次发生这种情况时,我都会再次从该列表中获得随机数 xpatient=5 sd_healthy=2 xhealthy=7 sd_patient=2 thresholdvalue1=(xpatient-sd_healthy)*10 thresholdvalue2

我有一个1000×100的随机数数组。我想用多个数字的列表来设置这个列表的阈值;这些数字从[3到9]。如果它们高于阈值,我希望将行的总和附加到列表中

我试过很多方法,包括3次有条件的。现在,我已经找到了一种将数组与数字列表进行比较的方法,但每次发生这种情况时,我都会再次从该列表中获得随机数

xpatient=5
sd_healthy=2
xhealthy=7
sd_patient=2
thresholdvalue1=(xpatient-sd_healthy)*10
thresholdvalue2=(((xhealthy+sd_patient))*10)
thresholdlist=[]
x1=[]
Ahealthy=np.random.randint(10,size=(1000,100))
Apatient=np.random.randint(10,size=(1000,100))
TParray=np.random.randint(10,size=(1,61))
def thresholding(A,B): 
    for i in range(A,B):
        thresholdlist.append(i)
        i+=1
thresholding(thresholdvalue1,thresholdvalue2+1)
thresholdarray=np.asarray(thresholdlist)
thedivisor=10
newthreshold=(thresholdarray/thedivisor)
for x in range(61):
    Apatient=np.random.randint(10,size=(1000,100))
    Apatient=[Apatient>=newthreshold[x]]*Apatient
    x1.append([sum(x) for x in zip(*Apatient)])
所以,我的for循环包含一个随机整数,但如果我不这样做,我就看不到每一圈的阈值。我希望整个数组的阈值为3,3.1,3.2等等。
我希望我表达了我的观点。提前感谢

您可以使用以下方法解决您的问题:

import numpy as np

def get_sums_by_threshold(data, threshold, axis): # use axis=0 to sum values along rows, axis=1 - along columns
    result = list(np.where(data >= threshold, data, 0).sum(axis=axis))
    return result

xpatient=5
sd_healthy=2
xhealthy=7
sd_patient=2
thresholdvalue1=(xpatient-sd_healthy)*10
thresholdvalue2=(((xhealthy+sd_patient))*10)

np.random.seed(100) # to keep generated array reproducable
data = np.random.randint(10,size=(1000,100))
thresholds = [num / 10.0 for num in range(thresholdvalue1, thresholdvalue2+1)]

sums = list(map(lambda x: get_sums_by_threshold(data, x, axis=0), thresholds))
但您应该知道,初始数组仅包含整数值,对于具有相同整数部分的多个阈值,您将得到相同的结果(f.e.3.0、3.1、3.2、…、3.9)。如果要在具有指定形状的初始数组中存储0到9之间的浮点数,可以执行以下操作:

data = np.random.randint(90,size=(1000,100)) / 10.0