我正在尝试建立逻辑回归模型。我首先使用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