【问题标题】:Python normalise floats in a list of lists to range from 0.0 (smallest) to 1.0 (largest) in each sublistPython 将列表列表中的浮点数标准化为每个子列表中从 0.0(最小)到 1.0(最大)的范围
【发布时间】:2020-01-18 21:32:45
【问题描述】:

尝试规范化我在下面的列表:

[[7.460143566, 9.373718262, 9.540244102, 9.843519211, 9.034710884, 10.71182728], [0.490880072, 0.637698293, 0.806753874, 0.906699121, 0.697924912, 0.949957848], [52.33952713, 69.05165863, 65.69918823, 67.53870392, 65.12568665, 72.78334045]]

进入下面:

[[0.0, 0.3435355, 0.565656, 0.6576767, 1.0], [0.0, 0.232424, 0.465664, 0.76768, 1.0], [0.0, 0.24534535, 0.4564545, 0.576576, 1.0]]

我在尝试

normalized = (col_list_filter-min(col_list_filter))/(max(col_list_filter)-min(col_list_filter))
    print(normalized)

但不断收到 TypeError unsupported operand type(s) for -: 'list' and 'list'

【问题讨论】:

  • 请提供一个工作示例程序,而不是仅仅一行可能会在您的程序中产生错误,但它本身并不能真正说明问题。只能假设您作为示例提供的列表是col_list_filter 的值,这解释了错误,因为您要使用col_list_filter-min(<etc.> - 您不能在列表中使用-

标签: python python-3.x list


【解决方案1】:

假设col_list_filter 是列表列表,则col_list_filter-min(col_list_filter)max(col_list_filter)-min(col_list_filter) 都是list - list,正如错误消息中所述。

相反,您可以使用for 循环执行逐元素操作:

res = []
for i in l:
    max_, min_ = max(i), min(i)
    res.append([(j - min_)/(max_ - min_) for j in i])
res

或单线(但效率低得多):

[[(j - min(i))/(max(i) - min(i)) for j in i] for i in l]

输出:

[[0.0,
  0.5884873389626358,
  0.6396995276767564,
  0.7329666273317014,
  0.4842313879485761,
  1.0],
 [0.0,
  0.3198112142984678,
  0.688061628145554,
  0.9057703742992778,
  0.4510016620800218,
  1.0],
 [0.0,
  0.8174664500409363,
  0.6534818573661288,
  0.7434609459640676,
  0.625429283659689,
  1.0]]

【讨论】:

    【解决方案2】:

    您正在处理列表列表。所以你可以使用嵌套列表推导:

    a = [[7.460143566, 9.373718262, 9.540244102, 9.843519211, 9.034710884, 10.71182728], [0.490880072, 0.637698293, 0.806753874, 0.906699121, 0.697924912, 0.949957848], [52.33952713, 69.05165863, 65.69918823, 67.53870392, 65.12568665, 72.78334045]]
    
    b = [[(x-min(l))/(max(l)-min(l)) for x in l] for l in a]
    
    print (b)
    

    结果:

    [[0.0, 0.5884873389626358, 0.6396995276767564, 0.7329666273317014, 0.4842313879485761, 1.0], 
    [0.0, 0.3198112142984678, 0.688061628145554, 0.9057703742992778, 0.4510016620800218, 1.0],
    [0.0, 0.8174664500409363, 0.6534818573661288, 0.7434609459640676, 0.625429283659689, 1.0]]
    

    【讨论】:

    • 太好了,效果很好!没有涵盖理解对不起 Python 的新手
    【解决方案3】:

    这是一些规范化列表列表的代码:

    a = [2,4,10,6,8,4]
    amin, amax = min(a), max(a)
    for i, val in enumerate(a):
        a[i] = (val-amin) / (amax-amin)
    

    信用:https://scipython.com/book/chapter-2-the-core-python-language-i/questions/normalizing-a-list/

    试试看是否可以尝试将此逻辑应用于列表列表。

    自己试一试,如果遇到困难,请告诉我:)

    【讨论】:

      【解决方案4】:

      内置函数max只识别最外层。在这种情况下,它返回list,而不是数值。

      我认为使用 Numpy 数组更直接。

      import numpy as np
      
      your_original_list = [...]
      your_numpy_list = np.array(your_original_list)
      min_value = your_numpy_list.min()
      max_value = your_numpy_list.max()
      
      normalized = (your_numpy_list - min_value) / (max_value - min_value)
      
      

      如果你想用 row-wise 规范化列表,你可以指定axis 参数。

      batch = your_numpy_list.shape[0]
      min_list = your_numpy_list.min(axis=1).reshape(batch, 1)
      max_list = your_numpy_list.max(axis=1).reshape(batch, 1)
      normalized = (your_numpy_list - min_list) / (max_list - min_list)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-01
        相关资源
        最近更新 更多