ageron / handson-ml

⛔️ DEPRECATED – See https://github.com/ageron/handson-ml3 instead.
Apache License 2.0
25.18k stars 12.92k forks source link

ml2 end to end machine learining #591

Open d5423197 opened 4 years ago

d5423197 commented 4 years ago

Hello there,

I could not understand this piece of code.

def indices_of_top_k(arr, k): return np.sort(np.argpartition(np.array(arr), -k)[-k:])

Can't we just use argsort() instead?

Thanks for your help.

digitech-ai commented 4 years ago

argsort will work fine. argpartition just performs better. In case of you have 1million feature and if we want to identify first 500 important feature, using argpartition we dont need to sort the entire array. Algortithm will stop sorting, once it identified the last 500 (in case of negative indexing).

Hope it helps.