python - 获取 sklearn 逻辑回归的边际效应
问题描述
我想从sklearn
模型中获得逻辑回归的边际效应
我知道你可以使用 '.get_margeff()' 为 statsmodel 逻辑回归得到这些。sklearn没有什么吗?我想避免自己进行计算,因为我觉得会有很大的错误空间。
import statsmodels.formula.api as sm
from statsmodels.tools.tools import add_constant
from sklearn.datasets import load_breast_cancer
import pandas as pd
import numpy as np
data = load_breast_cancer()
x = data.data
y= data.target
x=add_constant(x,has_constant='add')
model = sm.Logit(y, x).fit_regularized()
margeff = model.get_margeff(dummy=True,count=True)
##print the margal effect
print(margeff.margeff)
>> [ 6.73582136e-02 2.15779589e-04 1.28857837e-02 -1.06718136e-03
-1.96032750e+00 1.36137385e+00 -1.16303369e+00 -1.37422595e+00
8.14539021e-01 -1.95330095e+00 -4.86235558e-01 4.84260993e-02
7.16675627e-02 -2.89644712e-03 -5.18982198e+00 -5.93269894e-01
3.22934080e+00 -1.28363008e+01 3.07823155e+00 5.84122170e+00
1.92785670e-02 -9.86284081e-03 -7.53298463e-03 -3.52349287e-04
9.13527446e-01 1.69938656e-01 -2.89245493e-01 -4.65659522e-01
-8.32713335e-01 -1.15567833e+00]
# manual calculation, doing this as you can get the coef_ from a sklearn model and use in the function
def PDF(XB):
var1 = np.exp(XB)
var2 = np.power((1+np.exp(XB)),2)
var3 = (var1 / var2)
return var3
arrPDF = PDF(np.dot(x,model.params))
ME=pd.DataFrame(np.dot(arrPDF[:,None],model.params[None,:]))
print(ME.iloc[:,1:].mean().to_list())
>>
[0.06735821358791198, 0.0002157795887363032, 0.012885783711597246, -0.0010671813611730326, -1.9603274961356965, 1.361373851981879, -1.1630336876543224, -1.3742259536619654, 0.8145390210646809, -1.9533009514684947, -0.48623555805230195, 0.04842609927469917, 0.07166756271689229, -0.0028964471200298475, -5.189821981601878, -0.5932698935239838, 3.229340802910038, -12.836300822253634, 3.0782315528664834, 5.8412217033605245, 0.019278567008384557, -0.009862840813512401, -0.007532984627259091, -0.0003523492868714151, 0.9135274456151128, 0.16993865598225097, -0.2892454926120402, -0.46565952159093893, -0.8327133347971125, -1.1556783345783221]
自定义函数与“”相同,.get_margeff()
但在上面的自定义函数中使用 sklearn ceof_ 时可能会有很大的错误空间。
- sklearn 中是否有一些方法/功能/属性可以给我带来边际效应
- 如果没有,是否有另一个库从 ceof_ 和数据中获取边际效应
- 如果上述两个问题的答案是否定的,是否存在自定义函数不起作用的任何情况(例如,使用特定求解器或 sklearn 中的惩罚)
解决方案
几天前我刚刚达到了这个需求。
我的主管给了我这些我想分享的信息。希望这可以帮到你。
partial_dependence:这个方法可以得到你的partial dependence
意思marginal effects
。
plot_partial_dependence:此方法可以绘制partial dependence
.
以下是 API 参考中的示例代码。
scikit-learn version: 0.21.2
from sklearn.inspection import plot_partial_dependence, partial_dependence
from sklearn.datasets import make_friedman1
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import GradientBoostingRegressor
%matplotlib inline
X, y = make_friedman1()
# case1: linear model
lm = LinearRegression().fit(X, y)
# plot the partial dependence
plot_partial_dependence(lm, X, [0, (0, 1)])
# get the partial dependence
partial_dependence(lm, X, [0])
# case2: classifier
clf = GradientBoostingRegressor(n_estimators=10).fit(X, y)
# plot the partial dependence
plot_partial_dependence(clf, X, [0, (0, 1)])
# get the partial dependence
partial_dependence(clf, X, [0])
推荐阅读
- python - 如何用python分隔文本文件中的字符
- python - 画布中分层图像的事件处理
- asp.net-core - preLaunchTask 未在启动配置中的 ${command:pickRemoteProcess} 之前执行
- c# - 数据透视表聚合的 Excel 性能
- javascript - 如何调用rest api并将结果提供给后续的promise调用
- c++ - C++ char Arrays 与数组进行比较并分配单个值;
- java - 将项目添加到收藏夹数据库时更改图标颜色
- javascript - 如何在这个 JavaScript 中放置一个循环?
- r - RStudio 的 Anaconda Navigator 安装问题
- swift - 如何将应用程序的方向锁定为 iPhone *only* 的纵向?