ITPub博客

首页 > > > R语言可解释机器学习:LIME简介

R语言可解释机器学习:LIME简介

作者:浪花小雨滴 时间:2020-05-10 09:53:06 0 删除 编辑

作者:黄天元,复旦大学博士在读,热爱数据科学与开源工具(R),致力于利用数据科学迅速积累行业经验优势和科学知识发现,涉猎内容包括但不限于信息计量、机器学习、数据可视化、应用统计建模、知识图谱等,著有《R语言数据高效处理指南》。知乎专栏:R语言数据挖掘。邮箱:huang.tian-yuan@qq.com.欢迎合作交流

要知道一个人羽毛球打得好不好,可以有很多指标,比如杀球速度、前场反应时间、勾对角球比网高多少等等。但是真的需要这么多指标才能够做出判断吗?也许不需要。对于一个业余选手,我们仅仅看看其高远球的质量就可以知道其处于什么段位。LIME(Local Interpretable Model-Agnostic Explanations)就采用了这种简化的思维。尽管我们的模型可以非常复杂,设计变量可以非常多,但是对于一个样本来说,构建一个局部简单的线性模型就能够对其进行有把握的评估。因此,这个方法能够对黑箱模型进行简化的局部解释,提供有价值的洞见。

LIME的基本实现过程如下(见Understanding lime):

  1. For each prediction to explain, permute the observation n times.
  2. Let the complex model predict the outcome of all permuted observations.
  3. Calculate the distance from all permutations to the original observation.
  4. Convert the distance to a similarity score.
  5. Select m features best describing the complex model outcome from the permuted data.
  6. Fit a simple model to the permuted data, explaining the complex model outcome with the m features from the permuted data weighted by its similarity to the original observation.
  7. Extract the feature weights from the simple model and use these as explanations for the complex models local behavior.

不妨来学习LIME包(CRAN - Package lime)GitHub中的一段代码(thomasp85/lime):

library(caret)
library(lime)

# Split up the data set
iris_test <- iris[1:5, 1:4]
iris_train <- iris[-(1:5), 1:4]
iris_lab <- iris[[5]][-(1:5)]

# Create Random Forest model on iris data
model <- train(iris_train, iris_lab, method = 'rf')

# Create an explainer object
explainer <- lime(iris_train, model)

# Explain new observation
explanation <- explain(iris_test, explainer, n_labels = 1, n_features = 2)

# The output is provided in a consistent tabular format and includes the
# output from the model.
explanation
#> # A tibble: 10 x 13
#>    model_type case  label label_prob model_r2 model_intercept
#>    <chr>      <chr> <chr>      <dbl>    <dbl>           <dbl>
#>  1 classific… 1     seto…          1    0.680           0.120
#>  2 classific… 1     seto…          1    0.680           0.120
#>  3 classific… 2     seto…          1    0.675           0.125
#>  4 classific… 2     seto…          1    0.675           0.125
#>  5 classific… 3     seto…          1    0.682           0.122
#>  6 classific… 3     seto…          1    0.682           0.122
#>  7 classific… 4     seto…          1    0.667           0.128
#>  8 classific… 4     seto…          1    0.667           0.128
#>  9 classific… 5     seto…          1    0.678           0.121
#> 10 classific… 5     seto…          1    0.678           0.121
#> # … with 7 more variables: model_prediction <dbl>, feature <chr>,
#> #   feature_value <dbl>, feature_weight <dbl>, feature_desc <chr>,
#> #   data <list>, prediction <list>

# And can be visualised directly
plot_features(explanation)

可以发现支持这5个样本判断为setosa物种的最大证据,就是因为它们的Petal.Width小于等于0.4且Petal.Length小于等于1.6。

LIME方法还可以对文本和图像进行解析,比如看到图中猫猫的面部和爪子,就可以判断它是猫;看到大象的鼻子(一个局部区域),就可以判断这个图中的动物是大象。

更多参考资料:pbiecek.github.io/ema/L

原文作者:;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2018-05-16

  • 博文量
    4259
  • 访问量
    39958