从混合效应随机森林中获取特征重要性

Obtain feature importance from a mixed effects random forest

我是 R 用户 运行 第一次 python3.7 64bit Windows。 我试图使用包 eli5 中的 PermutationImportance 从混合效应随机森林中获取排列重要性。 可以找到再现性数据集 here.

适合:

merf = MERF(n_estimators= 500, max_iterations= 100)
np.random.seed(100)
merf.fit(X_train_merf, Z_train, clusters_train, y_train)

特征重要性:

perimp = PermutationImportance(merf, cv = None, refit = False, n_iter = 50).fit(X_train, Z_train, clusters_train, y_train)

上面的代码产生了这个错误

TypeError: fit() takes from 3 to 4 positional arguments but 5 were given

但是fit()只包含4个参数...

是否有可能从 merf 个对象中获得特征重要性?

我不知道 merf 或 eli5 模块,但我可以告诉您为什么会出现该异常。

如果您查看 PermutationImportance Module 及其 API 的文档,您可以看到 fit() 方法的以下定义:

    fit(X, y, groups=None, **fit_params)

最后一个参数前的两颗星表示它是一个 keyword argument。所以实际上这个方法可以接受 3 个位置参数和许多关键字参数。但这也意味着您需要命名第四个参数。在该方法中,您会获得该参数的字典,并且该方法需要知道如何处理它。

示例:

def my_fit(X, **fit_params):
    print(fit_params)

my_fit("positional argument", x=1,y=2,z=3)               
>>> {'x': 1, 'y': 2, 'z': 3}

我不使用 eli5 所以我不能告诉你使用什么关键字或者是否有可能从 merf 对象中获得特征重要性,但是只要给你的最后一个参数一个名字就可以解决这个错误喜欢:

perimp = PermutationImportance(merf, cv = None, refit = False, n_iter = 50).fit(X_train, Z_train, clusters_train, y_train=y_train)

希望该方法知道如何处理这样命名的参数。

我 运行 遇到了同样的问题,从源代码中我发现你可以从 运行domforest 模型对象中提取特征重要性,它是 MERF 的一部分。

mrf.fit(X_train, Z_train, clusters_train, y_train)
feat_importance=mrf.trained_fe_model.feature_importances_