Py学习  »  Python

python normalise在列表列表中浮动,在每个子列表中范围从0.0(最小)到1.0(最大)

Electric_Sheep01 • 5 年前 • 1456 次点击  

正在尝试规范化列表列表,我的列表如下:

[[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不支持的操作数类型-:“list”和“list”

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/41199
 
1456 次点击  
文章 [ 4 ]  |  最新文章 5 年前
Kai Sasaki
Reply   •   1 楼
Kai Sasaki    5 年前

内置函数 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)

如果要按行规范化列表,可以指定 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)
DoubleRainbowZ
Reply   •   2 楼
DoubleRainbowZ    5 年前

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

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/

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

你自己试试,如果你卡住了就告诉我:)

Chris
Reply   •   3 楼
Chris    5 年前

假设 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]]
Henry Yik
Reply   •   4 楼
Henry Yik    5 年前

您正在处理列表列表。因此,您可以使用嵌套列表理解:

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