使用列表

时间:2017-10-08 11:35:08

标签: python numpy

在下面的代码中,“net_worths”,“预测”是用于找出方差的两个numpy数组,即“错误”,然后参数压缩在一个新的变量cleaning_data中。

 cleaned_data = []

###  code goes here

errors = (net_worths - predictions)**2
cleaned_data = zip(ages, net_worths, errors)

cleaned_data = sorted(cleaned_data, key=lambda x: x[2], reverse=True)
limit = int(len(ages) * 0.1

return list(cleaned_data[limit:])

但是我很难理解以下3行,有人可以帮忙。

cleaned_data = sorted(cleaned_data, key=lambda x: x[2], reverse=True)

limit = int(len(ages) * 0.1

return list(cleaned_data[limit:])

1 个答案:

答案 0 :(得分:1)

让我们分解它们:

# sorted return a sorted list
# cleaned_data is the iterable it sorts
# key is the function used for comparison. When comparing two elements, 
#     it will compare them based on the value 3rd element in the list (x[2])

# reverse is self explantory; it returns the list in reversed order
cleaned_data = sorted(cleaned_data, key=lambda x: x[2], reverse=True)

# this check the length of ages, and multiple that by 0.1 (so it's 10% size)
limit = int(len(ages)) * 0.1

# return a new list from cleaned_data, but SKIP on 'limit' elements
return list(cleaned_data[limit:])

例如,

alist = [1, 2, 3, 4, 5]
alist[3:]  # --> same as cleaned_data[limit:]
[4, 5]

ages = [1, 2, 3, 4, 5]
limit = int(len(ages)) * 0.1  # --> 10% of the list size
limit
0.5