[英]Python normalise floats in a list of lists to range from 0.0 (smallest) to 1.0 (largest) in each sublist
Trying to normalize a list of lists I have below: 试图规范我下面的列表列表:
[[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]]
into below: 到下面:
[[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]]
I was trying 我在尝试
normalized = (col_list_filter-min(col_list_filter))/(max(col_list_filter)-min(col_list_filter))
print(normalized)
But keep getting TypeError unsupported operand type(s) for -: 'list' and 'list' 但是请继续获取TypeError不受支持的-('list'和'list')操作数类型
Assuming col_list_filter
is the list of lists, both col_list_filter-min(col_list_filter)
and max(col_list_filter)-min(col_list_filter)
are list - list
just as stated in the error message. 假设
col_list_filter
是列表列表,则col_list_filter-min(col_list_filter)
和max(col_list_filter)-min(col_list_filter)
都是list - list
,如错误消息中所述。
Instead, you can do an element-wise operation using for
loop: 相反,您可以使用
for
循环进行按元素操作:
res = []
for i in l:
max_, min_ = max(i), min(i)
res.append([(j - min_)/(max_ - min_) for j in i])
res
Or one-liner (but much less efficient): 或单线(但效率低得多):
[[(j - min(i))/(max(i) - min(i)) for j in i] for i in l]
Output: 输出:
[[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]]
This is some code to normalize just a list of lists: 这是一些代码,用于仅规范列表列表:
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)
credit: https://scipython.com/book/chapter-2-the-core-python-language-i/questions/normalizing-a-list/ 信用: https : //scipython.com/book/chapter-2-the-core-python-language-i/questions/normalizing-a-list/
Try see if you can try apply this logic to a list of lists. 尝试看看是否可以尝试将此逻辑应用于列表列表。
Give it a go yourself and let me know if you get stuck :) 自己试一试,让我知道是否卡住了:)
You are working on list of lists. 您正在处理列表列表。 So you can use a nested list comprehension:
因此,您可以使用嵌套列表理解:
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)
Result: 结果:
[[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]]
Built-in function max
only recognizes the outermost layer. 内置函数
max
仅识别最外层。 In this case, it returns the list
, not numerical value. 在这种情况下,它将返回
list
而不是数值。
I think using Numpy array is much more straight forward. 我认为使用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)
If you want to normalize the list with row-wise, you can specify axis
parameter. 如果要按行规范化列表,则可以指定
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.