2016-06-17 32 views
3

私はRとPythonの両方で虹彩データセットでロジスティック回帰を実行していました。しかし、両方とも異なる結果(係数、切片とスコア)を与えています。ScikitのpythonとRでロジスティック回帰の結果が異なる?

#Python codes. 
    In[23]: iris_df.head(5) 
    Out[23]: 
    Sepal.Length Sepal.Width Petal.Length Petal.Width Species 
    0   5.1   3.5   1.4   0.2  0 
    1   4.9   3.0   1.4   0.2  0 
    2   4.7   3.2   1.3   0.2  0 
    3   4.6   3.1   1.5   0.2  0 
    In[35]: iris_df.shape 
    Out[35]: (100, 5) 
    #looking at the levels of the Species dependent variable.. 

     In[25]: iris_df['Species'].unique() 
     Out[25]: array([0, 1], dtype=int64) 

    #creating dependent and independent variable datasets.. 

     x = iris_df.ix[:,0:4] 
     y = iris_df.ix[:,-1] 

    #modelling starts.. 
    y = np.ravel(y) 
    logistic = LogisticRegression() 
    model = logistic.fit(x,y) 
    #getting the model coefficients.. 
    model_coef= pd.DataFrame(list(zip(x.columns, np.transpose(model.coef_)))) 
    model_intercept = model.intercept_ 
    In[30]: model_coef 
    Out[36]: 
        0     1 
    0 Sepal.Length [-0.402473917528] 
    1 Sepal.Width [-1.46382924771] 
    2 Petal.Length [2.23785647964] 
    3 Petal.Width  [1.0000929404] 
    In[31]: model_intercept 
    Out[31]: array([-0.25906453]) 
    #scores... 
    In[34]: logistic.predict_proba(x) 
    Out[34]: 
    array([[ 0.9837306 , 0.0162694 ], 
      [ 0.96407227, 0.03592773], 
      [ 0.97647105, 0.02352895], 
      [ 0.95654126, 0.04345874], 
      [ 0.98534488, 0.01465512], 
      [ 0.98086592, 0.01913408], 

Rコード。収束問題へ

> str(irisdf) 
'data.frame': 100 obs. of 5 variables: 
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... 
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... 
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... 
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... 
$ Species  : int 0 0 0 0 0 0 0 0 0 0 ... 

> model <- glm(Species ~ ., data = irisdf, family = binomial) 
Warning messages: 
1: glm.fit: algorithm did not converge 
2: glm.fit: fitted probabilities numerically 0 or 1 occurred 
> summary(model) 

Call: 
glm(formula = Species ~ ., family = binomial, data = irisdf) 

Deviance Residuals: 
     Min   1Q  Median   3Q   Max 
-1.681e-05 -2.110e-08 0.000e+00 2.110e-08 2.006e-05 

Coefficients: 
       Estimate Std. Error z value Pr(>|z|) 
(Intercept)  6.556 601950.324  0  1 
Sepal.Length  -9.879 194223.245  0  1 
Sepal.Width  -7.418 92924.451  0  1 
Petal.Length  19.054 144515.981  0  1 
Petal.Width  25.033 216058.936  0  1 

(Dispersion parameter for binomial family taken to be 1) 

    Null deviance: 1.3863e+02 on 99 degrees of freedom 
Residual deviance: 1.3166e-09 on 95 degrees of freedom 
AIC: 10 

Number of Fisher Scoring iterations: 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 

スコア、切片及び係数の両方がRで全く異なっており、python.Whichの一つが正しいと私は結果が正確である混乱を有するpython.Nowで進行します。

+0

を使用してみてください。対数尤度関数が異なる局所最適値に終わる可能性がある。 – abhiieor

+0

あなたは、同様の質問で開発を続行することもできます。http://stackoverflow.com/questions/37872536/why-the-auc-is-so-different-from-logistic-regression-of-sklearn-and-r – abhiieor

答えて

3

更新 問題は、ペタルの幅の変数に沿って完全な分離が存在することです。つまり、この変数を使用して、指定されたデータセットのサンプルがsetosaかversicolorかを完全に予測することができます。これは、Rのロジスティック回帰で使用された対数尤度最大化推定を破る。問題は、対立遺伝子が花弁の幅の係数を無限大にすることによって、対数尤度が非常に高くなることである。

一部の背景と戦略はdiscussed hereです。

thread on CrossValidatedでも戦略が議論されています。

Sklearn LogisticRegressionはなぜ機能しますか?それは「正規化されたロジスティック回帰」を採用しているからです。正則化は、パラメータの大きな値の推定にペナルティを課します。

以下の例では、Firthのバイアス低減法ロジスティック回帰パッケージlogistfを使用して、統合モデルを作成します。 R溶液中のstd.errorとZ値に基づいて

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. 

ORIGINAL 、私はあなたが悪いモデルの仕様を持っていると思います。 0に近いz値は基本的に、モデルと従属変数の間に相関がないことを示します。これは無意味なモデルです。

私の最初の考えは、そのSpeciesフィールドをカテゴリ変数に変換する必要があるということです。あなたの例ではintのタイプです。私は考えることができる一つの可能​​な理由は、MLEのためのRとSklearnで使用されるさまざまな制約なし最適化の方法かもしれないas.factor

How to convert integer into categorical data in R?

+0

私は従属変数を要因に変換しようとしました。しかし、結果は同じです。 –

+0

奇妙な。どのIrisデータセットが使用していますか?私は今日それを見ることができます。 – andrew

+0

私はあなたにデータを送信します。あなたはメールを共有できますか?私はsetosaを0に、versicolorを1に、3番目をレベル2にするために削除しました。 –

関連する問題