首页 > 解决方案 > 在 R 中解释 NeuralNet 时出现 LIME 错误

问题描述

我正在使用 LIME 在信用资格数据集上解释 R 中的神经网络。当我使用随机森林(不缩放数据)时,一切正常。当我构建神经网络时,LIME 不起作用,解释函数失败并出现以下错误:

elnet(xd, is.sparse, ix, jx, y, weights, offset, type.gaussian, : y 是常数;高斯 glmnet 在标准化步骤中失败

R代码片段

#SCALE the date. use this for NeuralNets
minvalue <-  apply(pdata[,2:15], 2, min)
maxvalue <-  apply(pdata[,2:15], 2, max)

#First column is the ID's, so ignoring that    
scaledata <- data.frame(scale(pdata[,2:15], center = minvalue, scale = maxvalue-minvalue))


#split the dataset
split <- createDataPartition(scaledata$Eligible, p=0.80, list=FALSE)

train <- scaledata[split,]
test <- scaledata[-split,]



#install.packages("neuralnet")
set.seed(123)
nn <- neuralnet(Eligible ~ Gender_Male + Married+ Self_Employed+ApplicantIncome+CoapplicantIncome+LoanAmount+
                  Loan_Amount_Term+Credit_History+LoanAmount_log+Property_Area_Rural+Property_Area_Semiurban+Property_Area_Urban, 
                data=train, hidden=c(5),act.fct = "logistic", linear.output = FALSE)



install.packages("lime")
library(lime)
nn <- as_classifier(nn, labels = NULL)

explainer <- lime(train, nn)


explaination <- explain(
  x = train[2,],
  explainer,
  n_features = 15,
  n_labels = 2,
)

这是变量的标准差

            Gender_Male                 Married                Graduate           Self_Employed 
             0.38362661              0.47535856              0.39979545              0.35380796 
        ApplicantIncome       CoapplicantIncome              LoanAmount        Loan_Amount_Term 
             0.06682824              0.07530975              0.11665333              0.13549645 
         Credit_History                Eligible          LoanAmount_log     Property_Area_Rural 
             0.35176038              0.46580982              0.11370806              0.45262633 
Property_Area_Semiurban     Property_Area_Urban 
             0.48272099              0.47602427 

有关火车数据的更多信息

> str(train) 'data.frame':  492 obs. of  14 variables:  
$ Gender_Male            : num  1 1 1 1 1 1 1 1 1 1 ...  
$ Married                : num  0 1 1 1 0 1 1 1 1 1 ...  
$ Graduate               : num  1 1 1 0 1 1 0 1 1 1 ...  
$ Self_Employed          : num  0 0 1 0 0 1 0 0 0 0 ...  
$ ApplicantIncome        : num  0.0705 0.0548 0.0353 0.0301 0.0724 ...  
$ CoapplicantIncome      : num  0 0.0362 0 0.0566 0 ...  
$ LoanAmount             : num  0.1722 0.1722 0.0825 0.1606 0.191 ...  
$ Loan_Amount_Term       : num  0.744 0.744 0.744 0.744 0.744 ...  
$ Credit_History         : num  1 1 1 1 1 1 1 0 1 1 ...  
$ Eligible               : num  1 0 1 1 1 1 1 0 1 1 ...  
$ LoanAmount_log         : num  0.61 0.61 0.458 0.5950.632 ...  
$ Property_Area_Rural    : num  0 1 0 0 0 0 0 0 0 0 ...  
$ Property_Area_Semiurban: num  0 0 0 0 0 0 0 1 0 0 ...  
$ Property_Area_Urban    : num  1 0 1 1 1 1 1 0 1 1 ...

如果不对数据进行缩放,神经网络显然会给出不令人满意的结果。有很多与此错误相关的查询,我尝试了之前建议的一些解决方案,但没有帮助。请帮忙!提前致谢。

标签: rmachine-learningneural-networkdata-science

解决方案


推荐阅读