匹配R中两个稀疏矩阵的列名,以与Xgboost一起使用

时间:2018-08-21 13:33:04

标签: r matrix xgboost

我想使用xgboost创建一个预测模型:

model_xgboost <- xgboost(data = sparse_matrix_train,
                  label = as.numeric(mydata$RealPtot),
                  nrounds = nrounds,
                  objective = "reg:linear",
                  eta = 0.3,
                  depth = 6)

results_xgb <- predict(model_xgboost, sparse_matrix_test)

这是我得到的错误:

  

predict.xgb.Booster(model_xgboost,sparse_matrix_test)中的错误:
  存储在objectnewdata中的功能名称是不同的!

我认为发生此错误是因为训练和测试矩阵的顺序不同。 有什么方法可以对两个矩阵重新排序,以使列的顺序相同?

用于培训和测试的列名:

    #Test Set
      colnames(sparse_matrix_test)
      [1] "IsWeekdayweekend"       "IsHoliday1" "BreakEaster_Recess"    
          [4] "BreakMid_Term_Break"    "BreakNon_Break"         "IsWeekday_lag24weekend"
          [7] "IsHoliday_lag241"       "IsHoliday_lag1681"      "actTemp"               
         [10] "Temp_lag24"             "Ptot_lag24"             "Ptot_lag168"           
         [13] "Temp_lag168"            "Poly3_actTemp"          "Month02"               
         [16] "Month03"                "Month04"                "Month05"               
         [19] "Month06"                "Month07"                "Month08"               
         [22] "Month09"                "Month10"                "Month11"               
         [25] "Month12"                "Day02"                  "Day03"                 
         [28] "Day04"                  "Day05"                  "Day06"                 
         [31] "Day07"                  "Day08"                  "Day09"                 
         [34] "Day10"                  "Day11"                  "Day12"                 
         [37] "Day13"                  "Day14"                  "Day15"                 
         [40] "Day16"                  "Day17"                  "Day18"                 
         [43] "Day19"                  "Day20"                  "Day21"                 
         [46] "Day22"                  "Day23"                  "Day24"                 
         [49] "Day25"                  "Day26"                  "Day27"                 
         [52] "Day28"                  "Day29"                  "Day30"                 
         [55] "Day31"                  "hour01"                 "hour02"                
         [58] "hour03"                 "hour04"                 "hour05"                
         [61] "hour06"                 "hour07"                 "hour08"                
         [64] "hour09"                 "hour10"                 "hour11"                
         [67] "hour12"                 "hour13"                 "hour14"                
         [70] "hour15"                 "hour16"                 "hour17"                
         [73] "hour18"                 "hour19"                 "hour20"                
         [76] "hour21"                 "hour22"                 "hour23"

# Training set

         colnames(sparse_matrix_train)
          [1] "Ptot_lag24"             "Ptot_lag168"            "actTemp"               
          [4] "Poly3_actTemp"          "Temp_lag24"             "Temp_lag168"           
         [7] "IsHoliday1"             "IsWeekdayweekend"       "BreakEaster_Recess"    
         [10] "BreakMid_Term_Break"    "BreakNon_Break"         "IsWeekday_lag24weekend"
         [13] "IsHoliday_lag241"       "IsHoliday_lag1681"      "hour01"                
         [16] "hour02"                 "hour03"                 "hour04"                
         [19] "hour05"                 "hour06"                 "hour07"                
         [22] "hour08"                 "hour09"                 "hour10"                
         [25] "hour11"                 "hour12"                 "hour13"                
         [28] "hour14"                 "hour15"                 "hour16"                
         [31] "hour17"                 "hour18"                 "hour19"                
        [34] "hour20"                 "hour21"                 "hour22"                
         [37] "hour23"                 "Month02"                "Month03"               
         [40] "Month04"                "Month05"                "Month06"               
         [43] "Month07"                "Month08"                "Month09"               
         [46] "Month10"                "Month11"                "Month12"               
         [49] "Day02"                  "Day03"                  "Day04"                 
         [52] "Day05"                  "Day06"                  "Day07"                 
         [55] "Day08"                  "Day09"                  "Day10"                 
         [58] "Day11"                  "Day12"                  "Day13"                 
         [61] "Day14"                  "Day15"                  "Day16"                 
         [64] "Day17"                  "Day18"                  "Day19"                 
         [67] "Day20"                  "Day21"                  "Day22"                 
         [70] "Day23"                  "Day24"                  "Day25"                 
        [73] "Day26"                  "Day27"                  "Day28"                 
        [76] "Day29"                  "Day30"                  "Day31"

0 个答案:

没有答案