简体   繁体   English

绘制由Ranger函数计算的特征重要性

[英]Plot feature importance computed by Ranger function

I need to plot variable Importance using ranger function because I have a big data table and randomForest doesn't work in my case of study. 我需要使用游侠函数绘制变量重要性,因为我有一个大数据表,而randomForest在我的研究中不起作用。

This is my code : 这是我的代码:

library(ranger)
set.seed(42)
model_rf <- ranger(Sales ~ .,data = data[,-1],importance = "impurity")

Then I create new data frame DF which contains from the code above like this 然后我创建了新的数据帧DF ,它包含上面的代码,就像这样

> v<-as.vector(model_rf$variable.importance$Importance)
> w<-(as.vector((row.names(df))))
> DF<-cbind(w,v)
> DF<-as.data.frame(DF)
> DF
                           w                v
1                  DayOfWeek 376393213095.426
2                  Customers 1364058809531.96
3                       Open 634528877741.021
4                      Promo 261749509069.205
5               StateHoliday 5196666310.34041
6              SchoolHoliday  6522969049.3763
7                   DateYear  7035399071.0376
8                  DateMonth 20134820116.2625
9                    DateDay 37631766745.2306
10                  DateWeek 32834962167.9479
11                 StoreType 31568433413.5718
12                Assortment 20257406597.8358
13       CompetitionDistance  111847579772.77
14 CompetitionOpenSinceMonth 46332196019.0118
15  CompetitionOpenSinceYear 45548903472.6485
16                    Promo2                0
17           Promo2SinceWeek 50666744628.7906
18           Promo2SinceYear 40964066303.0407
19           CompetitionOpen 39927447341.0351
20                 PromoOpen  28319356095.063
21            IspromoinSales 2844220121.08598

But I need to plot a graph like this according to the result shown above: 但我需要根据上面显示的结果绘制这样的图形:

在此输入图像描述

EDIT 编辑

As @Sam proposed I tried to adapt this code: 正如@Sam建议我尝试调整此代码:

> ggplot(DF, aes(x=reorder(w,v), y=v,fill=v))+ 
+   geom_bar(stat="identity", position="dodge")+ coord_flip()+
+   ylab("Variable Importance")+
+   xlab("")+
+   ggtitle("Information Value Summary")+
+   guides(fill=F)+
+   scale_fill_gradient(low="red", high="blue")

But I get this error: 但我得到这个错误:

Error: Discrete value supplied to continuous scale In addition: There were 42 warnings (use warnings() to see them) > 错误:提供给连续刻度的离散值此外:有42个警告(使用警告()来查看它们)>

How can I do this, please? 我该怎么办? Thank you in advance! 先感谢您!

This is untested but I think this should give you what you are after: 这是未经测试但我认为这应该会给你你所追求的:

 ggplot(model_rf$variable.importance, aes(x=reorder(variable,importance), y=importance,fill=importance))+ 
      geom_bar(stat="identity", position="dodge")+ coord_flip()+
      ylab("Variable Importance")+
      xlab("")+
      ggtitle("Information Value Summary")+
      guides(fill=F)+
      scale_fill_gradient(low="red", high="blue")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM