- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 SKLearn 对相当大的数据集进行 LR,该数据集包含约 600 个虚拟变量和很少的区间变量(数据集中有 300 K 行),并且生成的混淆矩阵看起来很可疑。我想检查返回系数和方差分析的显着性,但我找不到如何访问它。有可能吗?对于包含大量虚拟变量的数据,最佳策略是什么?多谢!
最佳答案
Scikit-learn 故意不支持统计推断。如果您想要开箱即用的系数显着性检验(以及更多),您可以使用 Logit来自Statsmodels的估计器。该包模仿 R 中的接口(interface) glm
模型,因此您可能会发现它很熟悉。
如果您仍然想坚持使用 scikit-learn LogisticRegression,您可以使用渐近逼近来分布最大似然估计。准确地说,对于最大似然估计值 theta
的向量,其方差-协方差矩阵可以估计为 inverse(H)
,其中 H
是theta
处的对数似然 Hessian 矩阵。这正是下面函数的作用:
import numpy as np
from scipy.stats import norm
from sklearn.linear_model import LogisticRegression
def logit_pvalue(model, x):
""" Calculate z-scores for scikit-learn LogisticRegression.
parameters:
model: fitted sklearn.linear_model.LogisticRegression with intercept and large C
x: matrix on which the model was fit
This function uses asymtptics for maximum likelihood estimates.
"""
p = model.predict_proba(x)
n = len(p)
m = len(model.coef_[0]) + 1
coefs = np.concatenate([model.intercept_, model.coef_[0]])
x_full = np.matrix(np.insert(np.array(x), 0, 1, axis = 1))
ans = np.zeros((m, m))
for i in range(n):
ans = ans + np.dot(np.transpose(x_full[i, :]), x_full[i, :]) * p[i,1] * p[i, 0]
vcov = np.linalg.inv(np.matrix(ans))
se = np.sqrt(np.diag(vcov))
t = coefs/se
p = (1 - norm.cdf(abs(t))) * 2
return p
# test p-values
x = np.arange(10)[:, np.newaxis]
y = np.array([0,0,0,1,0,0,1,1,1,1])
model = LogisticRegression(C=1e30).fit(x, y)
print(logit_pvalue(model, x))
# compare with statsmodels
import statsmodels.api as sm
sm_model = sm.Logit(y, sm.add_constant(x)).fit(disp=0)
print(sm_model.pvalues)
sm_model.summary()
print()
的输出是相同的,而且它们恰好是系数 p 值。
[ 0.11413093 0.08779978]
[ 0.11413093 0.08779979]
sm_model.summary()
还会打印格式良好的 HTML 摘要。
关于scikit-learn - scikit学习: how to check coefficients significance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25122999/
我正在编写一个“简化”函数来简化多项式,以便 simplify("2xy-yx") 可以返回 "xy",simplify( "-a+5ab+3a-c-2a")可以返回"-c+5ab"等。 我正处于将多
我正在尝试使用nls函数在R中拟合此非线性模型。 这是我要拟合的数据: tab2 = data.frame(n = c(10,100,1000,10000,100000), Time = c(3.98
我正在做一些研究,我已经到了计算图的聚类系数的地步。 根据 this paper directly related to my research : The clustering coefficient
我使用相当标准的方法从三次贝塞尔曲线的控制点中检索一元多项式系数: std::array getBezierCubicCoefficients( const std::array& bezier )
我正在使用nnet包中的函数multinom来运行多项逻辑回归。 据我所知,在多项逻辑回归中,系数是响应概率与引用响应概率之比的对数变化(即ln(P(i)/P(r))= B1 + B2 * X ...
我尝试使用 SKLearn 对相当大的数据集进行 LR,该数据集包含约 600 个虚拟变量和很少的区间变量(数据集中有 300 K 行),并且生成的混淆矩阵看起来很可疑。我想检查返回系数和方差分析的显
我正在使用 sympy 生成矩阵的行列式: from sympy import * X1, Y1 = symbols ("X1 Y1") x1, x2, x3, y1, y2, y3 = symbol
问题:我有一组表现出周期性变化的测量值(时间、测量值、误差),我想用以下形式的傅立叶级数拟合它们 其中 A0 是我测量的平均值,t 是时间,t0 是(已知)引用时间,P 是(已知)周期。我想拟合系数
我知道可以使用以下方法获取多项式特征作为数字:polynomial_features.transform(X)。根据manual ,对于二度的特征是:[1, a, b, a^2, ab, b^2]。但
我正在尝试在 R 中运行联合显着性检验: library(car) data("mtcars") mylm <- lm(mpg ~ qsec + gear + am + am:qsec + am:hp
我正在为我的数据集使用 GAM(广义加性模型)。该数据集有 32 个观测值,具有 6 个预测变量和一个响应变量(即功率)。 我正在使用 gam() 包的 mgcv 函数来拟合模型。每当我尝试拟合模型时
我打算为 Android 实现一个说话者验证应用程序,并且想知道是否有办法从 Android 的语音识别模块获取语音功能(MEL 系数)? 请注意,说话人验证与语音识别略有不同,因此我不直接使用语音识
我正在尝试将 sympy 用于一些基本的代数和物理学。当定义一个等式时,例如 import sympy as sy g,h = sy.Symbols("g h") v = sy.sqrt(2 * g
我已经多次使用 1D c2c 变换,没有任何问题。 N个网格点变换的傅里叶系数顺序为:f_0, f_1, f_2, ..., f_N/2, f_-N/2+1, ...., f_-1。 我只是想不出 2
假设有一个 4 向交互,具有 2x2x2 因子设计和一个连续变量。 因子具有默认的对比度编码 ( contr.treatment )。下面是一个例子: set.seed(1) cat1 <- as.f
是否有计算 x**r 系数的 python 函数(可能来自 numpy 或 scipy)在扩展(1+x+x**2+x**3+...+x**(k-1))**n , 其中k>=1 , n>=0和 0<=r
我需要知道如何以我可以自己生成预测概率的方式返回逻辑回归系数。 我的代码如下所示: lr = LogisticRegression() lr.fit(training_data, binary_lab
我正在做一个时间序列问题。当我在做 AR 模型时,一切正常。 # Import the module for estimating an ARMA model from statsmodels.tsa
我编写了一个Python代码来估计(单变量)线性回归模型的参数,但是估计系数矩阵的维数不合适。向量“beta_estimated”(下面报告)的维度应该是 (1×1),但实际上是 (n×n)。有什么想
自定义对比在分析中被广泛使用,例如:“这个三水平因子的水平 1 和水平 3 的 DV 值是否有显着差异?” 直观地说,这种对比用单元均值表示为: c(1,0,-1) 这些对比度中的一个或多个,绑定(b
我是一名优秀的程序员,十分优秀!