设置具有多个值的python列表的阈值

问题描述 投票:0回答:1

好,所以我有一个1000x100的数组,其中包含随机数。我想用多个数字来限制这个列表。这些数字从[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等,等等。我希望我能表达我的意思。在此先感谢

python arrays list threshold
1个回答
0
投票

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

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))

但是您应该知道您的初始数组仅包含整数值,并且对于具有相同整数部分(例如3.0、3.1、3.2,...,3.9)的多个阈值,您将获得相同的结果。如果要以指定的形状在初始数组中存储从0到9的浮点数,则可以执行以下操作:

data = np.random.randint(90,size=(1000,100)) / 10.0
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.