2017-10-08 7 views
0

"net_worths"、 "predictions"は2つのnumpy配列で、2つのエラー、つまり "errors"を見つけるために使用され、パラメータは新しい変数cleaned_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 
関連する問題