【发布时间】:2018-10-16 14:38:07
【问题描述】:
我有以下代码。我想尝试不同的特征选择算法而不重复两次相同的代码,所以我将函数名称放在列表中并编写了以下代码以查看它是否有效。确实如此。
我的问题是,列表如何将函数名称作为其项,以及它在 for 循环中的实际工作方式?
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')
*输出**
<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
【问题讨论】:
-
@bunbun 此代码有效...我已将代码的输出也放入了
-
哎呀是误读了你的问题
-
从问题中删除与
sklearn相关的所有内容可能是个好主意,因为很多人对此并不熟悉。无论如何,这个问题与sklearn无关。
标签: python python-3.x