LinAlgError:scipy中的奇异矩阵

时间:2019-10-09 15:35:01

标签: python-3.x machine-learning scikit-learn scipy statsmodels

我正在尝试如下运行自定义逻辑回归模型

from sklearn import linear_model
import scipy.stats as stat

class LogisticRegression_with_p_values:

    def __init__(self,*args,**kwargs):#,**kwargs):
        self.model = linear_model.LogisticRegression(*args,**kwargs)#,**args)

    def fit(self,X,y):
        self.model.fit(X,y)

        #### Get p-values for the fitted model ####
        denom = (2.0 * (1.0 + np.cosh(self.model.decision_function(X))))
        denom = np.tile(denom,(X.shape[1],1)).T
        F_ij = np.dot((X / denom).T,X) ## Fisher Information Matrix
        Cramer_Rao = np.linalg.inv(F_ij) ## Inverse Information Matrix
        sigma_estimates = np.sqrt(np.diagonal(Cramer_Rao))
        z_scores = self.model.coef_[0] / sigma_estimates # z-score for eaach model coefficient
        p_values = [stat.norm.sf(abs(x)) * 2 for x in z_scores] ### two tailed test for p-values

        self.coef_ = self.model.coef_
        self.intercept_ = self.mode

以及当我如下调用fit方法

reg = LogisticRegression_with_p_values()
reg.fit(inputs_train,loan_data_targets_train)

我收到错误

---> 97     raise LinAlgError("Singular matrix")
     98 
     99 def _raise_linalgerror_nonposdef(err, flag):

LinAlgError: Singular matrix

我已将所有数字变量编码为1和0的虚拟变量

但是,当我运行普通的scikit学习逻辑回归时,没有错误

from sklearn.linear_model import LogisticRegression
reg = LogisticRegression()
reg.fit(inputs_train,loan_data_targets_train)

0 个答案:

没有答案