Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scikit python和R中的逻辑回归结果不同?_Python_R_Machine Learning_Regression_Logistic Regression - Fatal编程技术网

Scikit python和R中的逻辑回归结果不同?

Scikit python和R中的逻辑回归结果不同?,python,r,machine-learning,regression,logistic-regression,Python,R,Machine Learning,Regression,Logistic Regression,我在R和Python上对iris数据集进行逻辑回归,但两者给出的结果不同(系数、截距和分数) R码。 str(irisdf) “数据帧”:100 obs。共有5个变量: $萼片。长度:num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4 4.9。。。 $萼片。宽度:num 3.5 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1。。。 $Petal.长度:num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.4 1.5。。。 $P

我在R和Python上对iris数据集进行逻辑回归,但两者给出的结果不同(系数、截距和分数)

R码。 str(irisdf) “数据帧”:100 obs。共有5个变量: $萼片。长度:num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4 4.9。。。 $萼片。宽度:num 3.5 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1。。。 $Petal.长度:num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.4 1.5。。。 $Petal.Width:num0.20.20.20.20.20.40.40.30.20.20.1。。。 $Species:int 0 0 0 0 0 0 0 0。。。 >模型摘要(模型) 电话: glm(公式=物种,科=二项式,数据=irisdf) 偏差残差: 最小1季度中值3季度最大值 -1.681e-05-2.110e-08 0.000e+00 2.110e-08 2.006e-05 系数: 估计标准误差z值Pr(>z) (截距)6.556 601950.324 01 萼片长度-9.879 194223.245 0 1 萼片宽度-7.418 924.451 0 1 花瓣长度19.054 144515.981 0 1 花瓣宽度25.033 216058.936 0 1 (二项式族的离散参数取为1) 零偏差:99个自由度上的1.3863e+02 剩余偏差:95自由度上的1.3166e-09 AIC:10 Fisher评分迭代次数:25 由于收敛性问题,我增加了最大迭代次数,得到的ε为0.05

> model <- glm(Species ~ ., data = irisdf, family = binomial,control = glm.control(epsilon=0.01,trace=FALSE,maxit = 100))
> summary(model)

Call:
glm(formula = Species ~ ., family = binomial, data = irisdf, 
    control = glm.control(epsilon = 0.01, trace = FALSE, maxit = 100))

Deviance Residuals: 
       Min          1Q      Median          3Q         Max  
-0.0102793  -0.0005659  -0.0000052   0.0001438   0.0112531  

Coefficients:
             Estimate Std. Error z value Pr(>|z|)
(Intercept)     1.796    704.352   0.003    0.998
Sepal.Length   -3.426    215.912  -0.016    0.987
Sepal.Width    -4.208    123.513  -0.034    0.973
Petal.Length    7.615    159.478   0.048    0.962
Petal.Width    11.835    285.938   0.041    0.967

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1.3863e+02  on 99  degrees of freedom
Residual deviance: 5.3910e-04  on 95  degrees of freedom
AIC: 10.001

Number of Fisher Scoring iterations: 12

#R scores..
> scores = predict(model, newdata = irisdf, type = "response")
> head(scores,5)
           1            2            3            4            5 
2.844996e-08 4.627411e-07 1.848093e-07 1.818231e-06 2.631029e-08 
>模型摘要(模型)
电话:
glm(公式=物种,科=二项式,数据=irisdf,
control=glm.control(epsilon=0.01,trace=FALSE,maxit=100))
偏差残差:
最小1季度中值3季度最大值
-0.0102793  -0.0005659  -0.0000052   0.0001438   0.0112531  
系数:
估计标准误差z值Pr(>z)
(截距)1.796704.3520.003 0.998
萼片长度-3.426 215.912-0.016 0.987
萼片宽度-4.208 123.513-0.034 0.973
花瓣长度7.615159.4780.0480.962
花瓣宽度11.835285.9380.0410.967
(二项式族的离散参数取为1)
零偏差:99个自由度上的1.3863e+02
剩余偏差:95自由度上的5.3910e-04
AIC:10.001
Fisher评分迭代次数:12
#R分数。。
>分数=预测(模型,newdata=irisdf,type=“response”)
>总分(5分)
1            2            3            4            5 
2.844996e-08 4.627411e-07 1.848093e-07 1.818231e-06 2.631029e-08
在R和python中,分数、截距和系数都完全不同。哪一个是正确的,我想继续在python中进行。现在,我不知道哪一个结果是准确的。

更新 问题是花瓣宽度变量存在完全分离。换句话说,这个变量可以用来完美地预测给定数据集中的样本是setosa还是versicolor。这打破了R中logistic回归中使用的对数似然最大化估计。问题是,通过将花瓣宽度系数取为无穷大,对数似然可以被驱动得非常高

本文介绍了一些背景和策略

还有一个很好的讨论策略

那么,为什么sklearn逻辑回归有效呢?因为它采用了“正则化逻辑回归”。正则化会惩罚对参数的较大值的估计

在下面的示例中,我使用Firth的logistic回归包的偏差缩减方法logistf生成一个收敛模型

library(logistf)

iris = read.table("path_to _iris.txt", sep="\t", header=TRUE)
iris$Species <- as.factor(iris$Species)
sapply(iris, class)

model1 <- glm(Species ~ ., data = irisdf, family = binomial)
# Does not converge, throws warnings.

model2 <- logistf(Species ~ ., data = irisdf, family = binomial)
# Does converge.
库(logistf)
iris=read.table(“路径到iris.txt”,sep=“\t”,header=TRUE)

我能想到的一个可能的原因可能是R和Sklearn用于MLE的不同无约束优化方法;这可能会导致对数似然函数以不同的局部最优值结束。您可能还想关注我尝试将因变量转换为因子的类似问题的发展。但结果是相同的。奇怪。正在使用哪个Iris数据集?我今天可以看一下。顺便说一句,我应该在没有看到数据的情况下抓住它。提示出现在警告信息中:“拟合概率数值为0或1”。另一个线索是
花瓣宽度
系数估计值很大(逻辑回归中任何大于10的都表明可能发生了完全分离…)这两种方法产生不同的结果,因为它们最大化了惩罚似然函数的不同公式。如果这一术语“惩罚可能性”对您没有任何意义,您可以对回归收缩方法进行一些研究。ISLR的第6.2章很好地介绍了脊线和套索回归。它们不是为逻辑回归而提供的,但你应该明白这一点。
> model <- glm(Species ~ ., data = irisdf, family = binomial,control = glm.control(epsilon=0.01,trace=FALSE,maxit = 100))
> summary(model)

Call:
glm(formula = Species ~ ., family = binomial, data = irisdf, 
    control = glm.control(epsilon = 0.01, trace = FALSE, maxit = 100))

Deviance Residuals: 
       Min          1Q      Median          3Q         Max  
-0.0102793  -0.0005659  -0.0000052   0.0001438   0.0112531  

Coefficients:
             Estimate Std. Error z value Pr(>|z|)
(Intercept)     1.796    704.352   0.003    0.998
Sepal.Length   -3.426    215.912  -0.016    0.987
Sepal.Width    -4.208    123.513  -0.034    0.973
Petal.Length    7.615    159.478   0.048    0.962
Petal.Width    11.835    285.938   0.041    0.967

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1.3863e+02  on 99  degrees of freedom
Residual deviance: 5.3910e-04  on 95  degrees of freedom
AIC: 10.001

Number of Fisher Scoring iterations: 12

#R scores..
> scores = predict(model, newdata = irisdf, type = "response")
> head(scores,5)
           1            2            3            4            5 
2.844996e-08 4.627411e-07 1.848093e-07 1.818231e-06 2.631029e-08 
library(logistf)

iris = read.table("path_to _iris.txt", sep="\t", header=TRUE)
iris$Species <- as.factor(iris$Species)
sapply(iris, class)

model1 <- glm(Species ~ ., data = irisdf, family = binomial)
# Does not converge, throws warnings.

model2 <- logistf(Species ~ ., data = irisdf, family = binomial)
# Does converge.