使用map和map2在列表上进行映射以构建矩阵

时间:2019-10-28 18:32:13

标签: r purrr

我正在尝试在列表内的TrainTestData列表上映射许多其他列表-一个称为C Farolillo。在此列表中,我有我的训练和测试数据。

列表如下:

enter image description here

我试图映射列表以创建一种新型的矩阵。

dtrain <- map(TrainTestData, ~map2(
  X_train, Y_train, ~ xgb.DMatrix(data = .x, label = .y, missing = "NaN")
))

我收到以下错误:

  

map2(X_train,Y_train,〜xgb.DMatrix(data = .x,label = .y,   missing =“ NaN”)):找不到对象'X_train'

我要去哪里错了?我已经尝试了上述代码的一些变体,但无法正常工作。

以下给出了相同的错误:

dtrain <- map(TrainTestData$`C Farolillo`, ~map2(
  X_train, Y_train, ~ xgb.DMatrix(data = .x, label = .y, missing = "NaN")
))

这是因为我在X_train列表中有列表吗?

> class(TrainTestData)
[1] "list"
> class(TrainTestData$`C Farolillo`)
[1] "list"
> class(TrainTestData$`C Farolillo`$X_train)
[1] "list"
> class(TrainTestData$`C Farolillo`$X_train[[1]])
[1] "matrix"

注意:由于数据太大,我无法提供示例代码。

编辑:我可以使用以下方法创建矩阵:

xgb.DMatrix(data = TrainTestData$`C Farolillo`$X_train[[1]], label = TrainTestData$`C Farolillo`$Y_train[[1]], missing = "NaN")

对于X_train和Y_train中的3个矩阵,给出:

> xgb.DMatrix(data = TrainTestData$`C Farolillo`$X_train[[1]], label = TrainTestData$`C Farolillo`$Y_train[[1]], missing = "NaN")
xgb.DMatrix  dim: 2190 x 4  info: label  colnames: yes
> xgb.DMatrix(data = TrainTestData$`C Farolillo`$X_train[[2]], label = TrainTestData$`C Farolillo`$Y_train[[2]], missing = "NaN")
xgb.DMatrix  dim: 2190 x 4  info: label  colnames: yes
> xgb.DMatrix(data = TrainTestData$`C Farolillo`$X_train[[3]], label = TrainTestData$`C Farolillo`$Y_train[[3]], missing = "NaN")
xgb.DMatrix  dim: 2190 x 4  info: label  colnames: yes

编辑:

我有一些数据:

TrainTestData <- list(Recoletos = list(X_train = list(structure(c(NA, NA, 1.3, 
NA, NA, 80, NA, NA, 225), .Dim = c(3L, 3L), .Dimnames = list(
    NULL, c("CO", "NO2", "NOx"))), structure(c(1.3, 1.2, 1.5, 
80, 76, 76, 225, 202, 276), .Dim = c(3L, 3L), .Dimnames = list(
    NULL, c("CO", "NO2", "NOx"))), structure(c(1.5, 1, 1, 76, 
65, 67, 276, 165, 181), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx"))), structure(c(1, 1.3, 1.1, 67, 93, 
85, 181, 319, 274), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx"))), structure(c(1.1, 1.5, 1.2, 85, 83, 
80, 274, 368, 253), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx"))), structure(c(1.2, 1.4, 2.2, 80, 84, 
92, 253, 287, 432), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx"))), structure(c(2.2, 2, 1.5, 92, 85, 
73, 432, 399, 298), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx"))), structure(c(1.5, 1.4, 1.3, 73, 70, 
73, 298, 237, 260), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx"))), structure(c(1.3, 1.4, 1.7, 73, 84, 
88, 260, 289, 314), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx"))), structure(c(1.7, 2.4, 2.8, 88, 101, 
100, 314, 530, 657), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx"))), structure(c(2.8, 2.3, 1.3, 100, 
101, 74, 657, 539, 233), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx"))), structure(c(1.3, 1.1, 1.1, 74, 71, 
42, 233, 205, 110), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx"))), structure(c(1.1, 1.3, 1.5, 42, NA, 
75, 110, NA, 255), .Dim = c(3L, 3L), .Dimnames = list(NULL, c("CO", 
"NO2", "NOx"))), structure(c(1.5, 0.8, 1.2, 75, 55, 77, 255, 
140, 238), .Dim = c(3L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
"NOx"))), structure(c(1.2, 2.1, 2.3, 77, 121, 109, 238, 515, 
507), .Dim = c(3L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
"NOx"))), structure(c(2.3, 1.8, 2.8, 109, 97, 120, 507, 446, 
689), .Dim = c(3L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
"NOx"))), structure(c(2.8, 1.8, 1.2, 120, 96, 68, 689, 396, 250
), .Dim = c(3L, 3L), .Dimnames = list(NULL, c("CO", "NO2", "NOx"
))), structure(c(1.2, 1.4, 1.1, 68, 87, 81, 250, 310, 256), .Dim = c(3L, 
3L), .Dimnames = list(NULL, c("CO", "NO2", "NOx"))), structure(c(1.1, 
1.1, 1.4, 81, 80, 83, 256, 225, 249), .Dim = c(3L, 3L), .Dimnames = list(
    NULL, c("CO", "NO2", "NOx"))), structure(c(1.4, 1.5, 1.4, 
83, 78, 74, 249, 254, 270), .Dim = c(3L, 3L), .Dimnames = list(
    NULL, c("CO", "NO2", "NOx"))), structure(c(1.4, 3.3, 1.9, 
74, 107, 110, 270, 662, 424), .Dim = c(3L, 3L), .Dimnames = list(
    NULL, c("CO", "NO2", "NOx"))), structure(c(1.9, 2, 1, 110, 
116, 73, 424, 508, 221), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx"))), structure(c(1, 0.9, 1.1, 73, 74, 
87, 221, 207, 240), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx"))), structure(c(1.1, 1.3, 1.2, 87, 104, 
76, 240, 299, 261), .Dim = c(3L, 3L), .Dimnames = list(NULL, 
    c("CO", "NO2", "NOx")))), Y_train = list(structure(c(NA, 
NA, 94), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(94, 
82, 130), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(130, 
65, 75), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(75, 
147, 123), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(123, 
186, 113), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(113, 
132, 222), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(222, 
205, 147), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(147, 
109, 122), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(122, 
134, 147), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(147, 
280, 364), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(364, 
286, 103), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(103, 
88, 45), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(45, 
NA, 117), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(117, 
55, 105), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(105, 
257, 260), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(260, 
228, 371), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(371, 
195, 119), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(119, 
145, 115), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(115, 
95, 109), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(109, 
115, 128), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(128, 
362, 205), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(205, 
256, 96), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(96, 
87, 100), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO")), structure(c(100, 
127, 121), .Dim = c(3L, 1L), .Dimnames = list(NULL, "NO"))), 
    X_test = list(structure(c(1.2, 76, 202), .Dim = c(1L, 3L), .Dimnames = list(
        NULL, c("CO", "NO2", "NOx"))), structure(c(1, 65, 165
    ), .Dim = c(1L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
    "NOx"))), structure(c(1.3, 93, 319), .Dim = c(1L, 3L), .Dimnames = list(
        NULL, c("CO", "NO2", "NOx"))), structure(c(1.5, 83, 368
    ), .Dim = c(1L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
    "NOx"))), structure(c(1.4, 84, 287), .Dim = c(1L, 3L), .Dimnames = list(
        NULL, c("CO", "NO2", "NOx"))), structure(c(2, 85, 399
    ), .Dim = c(1L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
    "NOx"))), structure(c(1.4, 70, 237), .Dim = c(1L, 3L), .Dimnames = list(
        NULL, c("CO", "NO2", "NOx"))), structure(c(1.4, 84, 289
    ), .Dim = c(1L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
    "NOx"))), structure(c(2.4, 101, 530), .Dim = c(1L, 3L), .Dimnames = list(
        NULL, c("CO", "NO2", "NOx"))), structure(c(2.3, 101, 
    539), .Dim = c(1L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
    "NOx"))), structure(c(1.1, 71, 205), .Dim = c(1L, 3L), .Dimnames = list(
        NULL, c("CO", "NO2", "NOx"))), structure(c(1.3, NA, NA
    ), .Dim = c(1L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
    "NOx"))), structure(c(0.8, 55, 140), .Dim = c(1L, 3L), .Dimnames = list(
        NULL, c("CO", "NO2", "NOx"))), structure(c(2.1, 121, 
    515), .Dim = c(1L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
    "NOx"))), structure(c(1.8, 97, 446), .Dim = c(1L, 3L), .Dimnames = list(
        NULL, c("CO", "NO2", "NOx"))), structure(c(1.8, 96, 396
    ), .Dim = c(1L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
    "NOx"))), structure(c(1.4, 87, 310), .Dim = c(1L, 3L), .Dimnames = list(
        NULL, c("CO", "NO2", "NOx"))), structure(c(1.1, 80, 225
    ), .Dim = c(1L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
    "NOx"))), structure(c(1.5, 78, 254), .Dim = c(1L, 3L), .Dimnames = list(
        NULL, c("CO", "NO2", "NOx"))), structure(c(3.3, 107, 
    662), .Dim = c(1L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
    "NOx"))), structure(c(2, 116, 508), .Dim = c(1L, 3L), .Dimnames = list(
        NULL, c("CO", "NO2", "NOx"))), structure(c(0.9, 74, 207
    ), .Dim = c(1L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
    "NOx"))), structure(c(1.3, 104, 299), .Dim = c(1L, 3L), .Dimnames = list(
        NULL, c("CO", "NO2", "NOx"))), structure(c(0.9, 76, 194
    ), .Dim = c(1L, 3L), .Dimnames = list(NULL, c("CO", "NO2", 
    "NOx")))), Y_test = list(structure(82, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(65, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(147, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(186, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(132, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(205, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(109, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(134, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(280, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(286, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(88, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(NA_real_, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(55, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(257, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(228, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(195, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(145, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(95, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(115, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(362, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(256, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(87, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(127, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")), structure(77, .Dim = c(1L, 1L), .Dimnames = list(
        NULL, "NO")))))

当我运行以下代码时:

dtrain <- map(TrainTestData, ~map2(
  .$X_train, .$Y_train, ~ xgb.DMatrix(data = .x, label = .y, missing = "NaN")))

dtest <- map(TrainTestData, ~map(
  .$X_test, ~ xgb.DMatrix(data = .x, missing = "NaN")))


watchlist <- list("train" = dtrain)
params <- list("eta" = 0.1, "max_depth" = 5, "objective"="reg:squarederror", "eval_metric"= "rmse",
               "set.seed" = 176)


xgb.model <- map(
  dtrain, ~ xgboost(params = params, data = .x, nrounds = 40, watchlist)
)

我收到此错误:

  

xgb.get.DMatrix(数据,标签,缺失,重量)错误:xgboost:   输入数据无效另外:警告消息:在   xgb.get.DMatrix(数据,标签,缺失,重量):xgboost:标签将   被忽略。

鼠标悬停了,我提供了label

编辑:

enter image description here

0 个答案:

没有答案