2017-10-08 57 views
0

在下面的代码中,“net_worths”,“predictions”是用于找出平方误差的两个numpy数组,即“错误”,然后将参数压缩到新变量cleared_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

让我们打破他们:

# 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