为什么我使用statsmodels而不是sklearn LogisticRegression来获得“ LinAlgError:奇异矩阵”?

时间:2019-04-01 02:36:52

标签: python-3.x logistic-regression statsmodels

我正在尝试建立逻辑回归模型。我首先使用sklearn,但由于无法获取p值,因此切换到statsmodels,但出现“ LinAlgError:奇异矩阵”

这是我的代码:

clf = LogisticRegression(random_state=0, solver='lbfgs',
                            multi_class='multinomial').fit(train_x, train_y)
            coef = np.reshape(clf.coef_, [43])
            Coefficients[i-1, j-1, :] = coef

            model = sm.Logit(train_y, train_x)

            result = model.fit()

我得到的错误如下:

Warning: Maximum number of iterations has been exceeded.
         Current function value: 0.443088
         Iterations: 35
---------------------------------------------------------------------------
LinAlgError                               Traceback (most recent call last)
<ipython-input-121-f22ab1b93433> in <module>()
     48             model = sm.Logit(train_y, train_x)
     49 
---> 50             result = model.fit()

这是我的数据的形状:

train_x shape:(1481, 43)
train_y shape:(1481,)

目标变量是二进制

train_y sum:248.0

0 个答案:

没有答案