6 Commits debcacff90 ... 40ead6f3ee

Author SHA1 Message Date
  Miron B. Kursa 40ead6f3ee Merge branch 'moreTests' into devel 1 year ago
  Miron B. Kursa 94d72e7879 Xgboost test operational 1 year ago
  Miron B. Kursa d337e1fa89 Merge branch 'devel' into moreTests 1 year ago
  Miron B. Kursa 6181cb16f1 Merge branch 'issue2' into devel 1 year ago
  Miron B. Kursa 7cef879af8 Testers for importance sources 1 year ago
  Miron B. Kursa dc857ea488 Fixed xgboost importance source for xgboost 71.1 1 year ago
2 changed files with 38 additions and 2 deletions
  1. 2 2
      R/importance.R
  2. 36 0
      tests/testthat/testSources.R

+ 2 - 2
R/importance.R

@@ -201,8 +201,8 @@ getImpXgboost<-function(x,y,nrounds=5,verbose=0,...){
    verbose=verbose
   )
  )->imp
- rep(0,ncol(x))->ans
- ans[as.numeric(imp$Feature)+1]<-imp$Gain
+ setNames(rep(0,ncol(x)),colnames(x))->ans
+ ans[imp$Feature]<-imp$Gain
  ans
 }
 comment(getImpXgboost)<-'xgboost gain importance'

+ 36 - 0
tests/testthat/testSources.R

@@ -0,0 +1,36 @@
+context("Importance source tests")
+
+#Set, as in https://mbq.me/blog/relevance-and-redundancy
+setNames(
+ do.call(expand.grid,rep(list(c(T,F)),5)),
+ c("A","B","N1","N2","N3")
+)->X
+cbind(X,AoB=with(X,A|B),AnB=with(X,A&B),nA=!X$A)->X
+factor(with(X,A!=B))->Y
+data.frame(lapply(X,factor))->X
+#Numeric value, for more fun
+set.seed(777)
+X$N4<-runif(nrow(X))
+
+impSources<-c(
+ "getImpExtraGini","getImpExtraRaw","getImpExtraZ","getImpFerns",
+ "getImpLegacyRfGini","getImpLegacyRfRaw","getImpLegacyRfZ",
+ "getImpRfGini","getImpRfRaw","getImpRfZ") #getImpXgboost is special
+
+for(e in impSources)
+ test_that(sprintf("Importance source %s works",e),{
+  set.seed(777)
+  #Run Boruta on this data
+  Boruta(X,Y)->ans
+  #Nonsense attributes should be rejected
+  expect_equal(
+   sort(getSelectedAttributes(ans)),
+   sort(c('A','B','AnB','AoB','nA'))
+  )
+ })
+
+test_that("Importance source getImpXgboost works",{
+ set.seed(777)
+ #Xgboost importance is generally poor, lower expectations
+ expect_silent(Boruta(Species~.,data=iris))
+})