【问题标题】:Thresholding a python list with multiple values对具有多个值的python列表进行阈值处理
【发布时间】:2020-02-29 06:57:59
【问题描述】:

好的,所以我有一个 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】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 2020-07-05
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多