I have the following code. I wanted to try different feature selection algorithms without repeating the same code twice so, I put the function names in the list and wrote the following code to see if it works. It did.
My question is, how can a list have function names as its items, and how is it actually working in the for loop?
from sklearn.datasets import load_digits
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.feature_selection import mutual_info_classif
X, y = load_digits(return_X_y=True)
list=[mutual_info_classif,chi2]
for i in list:
print(type(i))
X_new = SelectKBest(i, k=20).fit_transform(X, y)
print(X_new)
print('hello')
*Output**
<class 'function'>
[[ 5. 13. 15. ... 6. 0. 0.]
[ 0. 0. 9. ... 0. 10. 0.]
[ 0. 3. 14. ... 0. 16. 9.]
...
[ 1. 13. 2. ... 2. 6. 0.]
[ 2. 14. 15. ... 5. 12. 0.]
[10. 16. 1. ... 8. 12. 1.]]
hello
<class 'function'>
[[ 1. 0. 15. ... 6. 0. 0.]
[ 5. 0. 9. ... 0. 10. 0.]
[12. 0. 14. ... 0. 16. 9.]
...
[ 1. 0. 2. ... 2. 6. 0.]
[ 0. 0. 15. ... 5. 12. 0.]
[ 1. 0. 1. ... 8. 12. 1.]]
hello