0

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
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
ubuntu_noob
  • 2,305
  • 6
  • 23
  • 64

1 Answers1

2

You are not putting the "name" of the function into the list as such. Functions are objects just like everything else in Python. When you create a function object with a def statement, you bind it to a name, as if with =, and also set the object's __name__ attribute. When you place those names in a list, the list contains references to function objects that you can call later.

The for loop binds the references to the name i one by one. You may want to print i.__name__ in addition to type(i) to see which function is being passed to SelectKBest with each iteration.

It is fairly common to see functions being processed this way in Python. Functions, and other callable objects, can be placed into lists, dictionaries, tuples, and just about any other data structure. Since functions are hashable, they can even be used as dictionary keys or placed in a set. You can also add attributes to a function object just by assigning them.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • then shouldnt the item type in the list be a pointer or memory address ?its returning function type – ubuntu_noob Oct 16 '18 at 06:22
  • @ubuntu_noob. Under the hood it's a pointer to an object. `type` also happens to accept a pointer. At that level everything is a pointer. For example, `3`is a pointer to an `int` object in Python. – Mad Physicist Oct 16 '18 at 06:25
  • and is this a correct way to iterate through multiple functions?..is there any chance of memory problems? – ubuntu_noob Oct 16 '18 at 06:27
  • @ubuntu_noob. Not much chance of error, barring a major but in your Python interpreter or some catastrophic failure. Functions are very much meant to be passed around like that in Python. That's one of the things that come from them being first class objects. There are lots of tools to help you do it. – Mad Physicist Oct 16 '18 at 06:30