Logistic回归中已超过最大迭代次数

时间:2019-05-16 10:03:09

标签: python statistics logistic-regression

我正在运行数据文件load_breast_cancer()对肿瘤进行分类。 运行statsmodels检查每个变量的p值后,出现错误:

Warning: Maximum number of iterations has been exceeded.
         Current function value: inf
         Iterations: 35 

LinAlgError: Singular matrix

希望您能帮助我! 谢谢! 我尝试过一些关于stackoverflow的解决方案来解决此问题,但是它不起作用!

这是我的代码:

from sklearn.datasets import load_breast_cancer

df = load_breast_cancer()
df_cancer = pd.DataFrame(np.c_[df['data'], df['target']], columns = np.append(df['feature_names'], ['target']))


import statsmodels.api as sm
import scipy.stats as st
from statsmodels.tools import add_constant as add_constant
df_constant = add_constant(df_cancer)
df_constant.head()

st.chisqprob = lambda chisq, df_cancer: st.chi2.sf(chisq, df_cancer)
cols=df_constant.columns[:-1]

model=sm.Logit(df_cancer['target'],df_constant[cols])
result = model.fit()

期望值是结果将显示Logit回归结果!

1 个答案:

答案 0 :(得分:1)

只需完成这一行

result = model.fit(method='bfgs')
print(result.summary())