Logistic Regression in statsmodels “LinAlgError: S

2019-06-02 17:51发布

问题:

Not sure why but I'm getting a "numpy.linalg.linalg.LinAlgError: Singular matrix" error when fitting a logistic regression model.

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
import statsmodels.api as sm

data = load_breast_cancer()
y = data.target
X = data.data

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, stratify=y, random_state=2)

X_train = sm.add_constant(X_train)
X_test = sm.add_constant(X_test)
model = sm.Logit(y_train, X_train)
fit = model.fit() # error appears on this line

fit.summary2()