获取LinAlgError:奇异矩阵错误 [英] Getting LinAlgError: Singular matrix Error
问题描述
我正在使用以下函数来计算p值,以建立拟合逻辑回归模型.但是我得到了LinAlgError:奇异矩阵错误.
I'm using the below function to calculate p-value to build fit logistic regression model. But I get LinAlgError: Singular matrix error.
from sklearn import linear_model
import scipy.stats as stat
class LogisticRegression_with_p_values:
def __init__(self, *args, **kwargs):
self.model = linear_model.LogisticRegression(*args, **kwargs)
def fit(self, X, y):
self.model.fit(X, y)
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)
Cramer_Rao = np.linalg.inv(F_ij)
sigma_estimates = np.sqrt(np.diagonal(Cramer_Rao))
z_scores = self.model.coef_[0] / sigma_estimates
p_values = [stat.norm.sf(abs(x)) * 2 for x in z_scores]
self.coef_ = self.model.coef_
self.intercept_ = self.model.intercept_
self.p_values = p_values
reg = LogisticRegression_with_p_values()
reg.fit(inputs_train, loan_data_targets_train)
推荐答案
错误:reg = LogisticRegression_with_p_values()LinAlgError:奇异矩阵P值函数抛出错误后拟合模型:LinAlgError:奇异矩阵
Error : reg = LogisticRegression_with_p_values() LinAlgError: Singular matrix Fitting the Model after the P-value function throws error: LinAlgError: Singular matrix
步骤1:运行以下代码,并观察绿线中的所有缺失值.
Step 1: run the below code and observe any missing values in the green line.
corr = inputs_train.corr()
kot = corr[corr>=.9]
plt.figure(figsize=(18,10))
sns.heatmap(kot, cmap="Greens")
我正在研究Lending Club分析并遇到此错误,因此使用了上述热图,并在绿线中找到了一个缺失值,因此为了进一步调查,我运行了以下代码,以检查输出是否为"nan" inputs_train ['term:36'].corr(inputs_train ['term:36'])
I am working on Lending Club analysis and encountered this error so used the above-mentioned heatmap and found a missing value in the green line so to further investigate I ran the below code to check if the output is 'nan' inputs_train['term:36'].corr(inputs_train['term:36'])
O/P ::南
下一步:
当我们创建变量'term_int'时,我们没有将其从字符串转换为数字.要验证此检查输出,如果type(df_inputs_prepr_train ['term_int'] [0])是否提供'STR'
When we create the variable 'term_int', we have not converted it to numerical from string. To verify this check output if type(df_inputs_prepr_train['term_int'][0]) gives 'STR'
所以当我们写的时候:
df_inputs_prepr_train[df_inputs_prepr_train['term_int']==36]['term_int']
它显示输出为零行,因为'term_int'仍然是str 36,而不是数字36.
it shows the output as zero rows since 'term_int' is still an str 36 and not a numerical 36.
所以当我们使用代码:::
so when we use the code ::
df_inputs_prepr_train['term:36']=np.where((df_inputs_prepr_train['term_int']==36),1,0)
it basically stores only zeroes as output.
要采取的措施::
`df_inputs_prepr_train['term_int']=pd.to_numeric(df_inputs_prepr_train['term_int'])`
交叉验证:如果再次运行热图,绿线中将看不到任何缺失的值
Cross-verify: if you run heatmap again you won't see any missing values in the green line
这篇关于获取LinAlgError:奇异矩阵错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!