【问题标题】:Normalizing, returns divide by zero error归一化,返回除以零错误
【发布时间】:2021-06-01 09:50:22
【问题描述】:

我有一个传感器正在读取数据,我希望对其进行标准化。

f1020=[]
While True:
    cmnd = getMsrMnt(filter.1020) #command to get a sensor value
    f1020.append(cmnd.measurement) #write the sensor value to the above array
    norm_f1020 = [(float(i)-min(f1020))/(max(f1020)-min(f1020)) for i in f1020] #normalize the data

但是,此时,我收到以下错误

ZeroDivisionError:浮点除以零

这将始终发生在索引 0 处,因为此时数组中只有一个值!如果有的话,我如何使用现有的norm_f1020 技术来解决这个错误。

【问题讨论】:

    标签: arrays python-3.x normalization


    【解决方案1】:

    你可以通过两种方式解决这个问题:

    1. 添加到除法中的值真的很小
    2. 因为它应该是除以 0 的东西,你可以尝试 except 并添加 float('inf') 而不是除法的结果

    但我会选择第一种方式:

    f1020=[]
    While True:
        cmnd = getMsrMnt(filter.1020) #command to get a sensor value
        f1020.append(cmnd.measurement) #write the sensor value to the above array
        norm_f1020 = [(float(i)-min(f1020))/((max(f1020)-min(f1020) + 10**-100)) for i in f1020] #normalize the data
    

    只需确保您选择的值不会太小(例如 1/(0 + 10**-500) 我得到了ZeroDivisionError

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      • 2021-11-26
      • 2014-02-14
      • 1970-01-01
      相关资源
      最近更新 更多