R 包学习 -mlr()

作者: Thinkando | 来源:发表于2020-06-06 16:58 被阅读0次

    机器学习R包

    ########################
    # 1. 导入包
    ########################
    library('DALEX')
    
    library('mlr')
    
    library('breakDown')
    
    library("randomForest")
    
    data(apartments)
    
    head(apartments)
    
    
    ########################
    # 2. 建模与分析 
    ########################
    
    
    set.seed(2020)
    
    # id参数给这个任务做命名,data测存放数据,target参数用来告诉函数哪个变量是响应变量。
    regr_task <- makeRegrTask(id = "ap", data = apartments, target = "m2.price")
    
    # 随机森林模型
    regr_lrn_rf <- makeLearner("regr.randomForest")
    
    # 神经网络模型
    regr_lrn_nn <- makeLearner("regr.nnet")
    
    # 广义增强模型
    regr_lrn_gbm <- makeLearner("regr.gbm", par.vals = list(n.trees = 500))
    
    
    #对于神经网络而言,需要对超参数进行额外的调整(多少隐藏层、迭代次数),而且需要对数据进行预处理(中心化标准化)
    regr_lrn_nn <- setHyperPars(regr_lrn_nn, par.vals = list(maxit=500, size=2))
    
    regr_lrn_nn <- makePreprocWrapperCaret(regr_lrn_nn, ppc.scale=TRUE, ppc.center=TRUE)
    
    # 随后要进行的是模型的训练,任务和模型定义好之后,可以快速训练。
    regr_rf <- train(regr_lrn_rf, regr_task)
    
    regr_nn <- train(regr_lrn_nn, regr_task)
    
    regr_gbm <- train(regr_lrn_gbm, regr_task)
    
    # 接下来,要定义预测函数。也就是如果我们给一个测试集,那么模型怎么样才能够得到想要的预测值。
    
    
    data(apartmentsTest)
    
    custom_predict <- function(object, newdata) {pred <- predict(object, newdata=newdata)
    
    response <- pred$data$response
    
    return(response)}
    
    
    
    explainer_regr_rf <- DALEX::explain(regr_rf, data=apartmentsTest, y=apartmentsTest$m2.price, predict_function = custom_predict, label="rf")
    
    explainer_regr_nn <- DALEX::explain(regr_nn, data=apartmentsTest, y=apartmentsTest$m2.price,
                                        
                                        predict_function = custom_predict, label="nn")
    
    explainer_regr_gbm <- DALEX::explain(regr_gbm, data=apartmentsTest, y=apartmentsTest$m2.price,
                                         
                                         predict_function = custom_predict, label="gbm")
    
    #一般而言,函数的设计需要在第一个参数object中放入一个模型,而newdata代表测试数据集,
    #最后返回的是利用这个模型和测试数据集的解释变量所计算得到的预测数值。
    
    
    
    ##模型表现分析
    
    mp_regr_rf <- model_performance(explainer_regr_rf)
    
    mp_regr_gbm <- model_performance(explainer_regr_gbm)
    
    mp_regr_nn <- model_performance(explainer_regr_nn)
    
    
    
    mp_regr_rf
    
    
    
    plot(mp_regr_rf, mp_regr_nn, mp_regr_gbm)
    
    plot(mp_regr_rf, mp_regr_nn, mp_regr_gbm, geom = "boxplot")
    
    
    
    ##变量重要性分析
    
    vi_regr_rf <- variable_importance(explainer_regr_rf, loss_function = loss_root_mean_square)
    
    vi_regr_gbm <- variable_importance(explainer_regr_gbm, loss_function = loss_root_mean_square)
    
    vi_regr_nn <- variable_importance(explainer_regr_nn, loss_function = loss_root_mean_square)
    
    
    
    plot(vi_regr_rf, vi_regr_gbm, vi_regr_nn)
    
    # 不过,这里介绍一个新的参数设置,就是`type = "difference"`。
    # 在求变量重要性的时候,默认给出的是如果失去了这个变量,那么模型会发生的实质数值变化。
    # 如果把这个参数设置为`ratio`,那么就是缺失这个变量与缺失所有变量的变化之比,而设置为`difference`则为变化之差。
    # 这些参数能够让我们使用更加丰富的手段判断一个变量的重要程度。
    
    
    vi_regr_rf <- variable_importance(explainer_regr_rf, loss_function = loss_root_mean_square, type="difference")
    
    vi_regr_gbm <- variable_importance(explainer_regr_gbm, loss_function = loss_root_mean_square, type="difference")
    
    vi_regr_nn <- variable_importance(explainer_regr_nn, loss_function = loss_root_mean_square, type="difference")
    
    
    
    plot(vi_regr_rf, vi_regr_gbm, vi_regr_nn)
    
    
    # 下面做常规的单变量探索图:
    
    ## PDP图
    
    pdp_regr_rf <- variable_response(explainer_regr_rf, variable = "construction.year", type = "pdp")
    
    pdp_regr_gbm <- variable_response(explainer_regr_gbm, variable = "construction.year", type = "pdp")
    
    pdp_regr_nn <- variable_response(explainer_regr_nn, variable = "construction.year", type = "pdp")
    
    
    plot(pdp_regr_rf, pdp_regr_gbm, pdp_regr_nn)
    
    
    
    ## ALE图
    
    ale_regr_rf <- variable_response(explainer_regr_rf, variable = "construction.year", type = "ale")
    
    ale_regr_gbm <- variable_response(explainer_regr_gbm, variable = "construction.year", type = "ale")
    
    ale_regr_nn <- variable_response(explainer_regr_nn, variable = "construction.year", type = "ale")
    
    
    
    plot(ale_regr_rf, ale_regr_gbm, ale_regr_nn)
    
    mpp_regr_rf <- variable_response(explainer_regr_rf, variable = "district", type = "factor")
    
    mpp_regr_gbm <- variable_response(explainer_regr_gbm, variable = "district", type = "factor")
    
    mpp_regr_nn <- variable_response(explainer_regr_nn, variable = "district", type = "factor")
    
    
    
    plot(mpp_regr_rf, mpp_regr_gbm, mpp_regr_nn)
    
    
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png

    reddit.com/r/MachineLearning/comments/8aluhs/d_machine_learning_wayr_what_are_you_reading_week/

    相关文章

      网友评论

        本文标题:R 包学习 -mlr()

        本文链接:https://www.haomeiwen.com/subject/czrizhtx.html