使用stars包predict函数输出结果绘图时触发报错求助
解决stars包predict绘图报错问题
问题代码
library(stars) tif = system.file("tif/L7_ETMs.tif", package = "stars") i = read_stars(tif, proxy = TRUE) %>% split() nclus = 5 sam = st_sample(i, 1000) k = kmeans(na.omit(as.data.frame(sam)[, -c(1:2)]), nclus) out = predict(i, k) plot(out, col = sf.colors(nclus, categorical=TRUE))
报错信息
prediction on array(s) X1,X2,X3,X4,X5,X6' failed; will try to split() dimension y' over attributes
Error in split.stars(object) : length(x) == 1 is not TRUE
问题原因
- 读取数据时启用
proxy=TRUE创建代理对象,同时直接调用split()将多波段影像拆分为多个单波段对象,导致predict无法处理这种拆分后的结构 - 拆分后的stars对象与kmeans模型的输入维度不匹配,触发内部split操作的逻辑错误
修正方案
关闭代理模式加载数据,保留多波段影像的原始结构,确保predict处理的是完整的多维度输入:
修正后代码
library(stars) library(sf) tif = system.file("tif/L7_ETMs.tif", package = "stars") # 关闭proxy直接加载数据到内存,不预先执行split i = read_stars(tif, proxy = FALSE) nclus = 5 # 对完整多波段影像执行采样 sam = st_sample(i, 1000) # 提取采样点的光谱值,剔除坐标列 sam_values = na.omit(as.data.frame(sam)[, -c(1:2)]) k = kmeans(sam_values, nclus) # 基于完整多波段影像执行预测 out = predict(i, k) # 绘制分类结果 plot(out, col = sf.colors(nclus, categorical=TRUE))
关键修正点
- 取消
proxy=TRUE:代理对象适合大数据延迟加载,但此处小数据集直接加载到内存可避免拆分和预测的兼容性问题 - 移除
split():保留多波段影像的原始结构,确保predict能识别所有波段作为模型输入维度 - 保持采样与预测的输入维度一致:kmeans模型基于多波段值训练,预测时需传入相同维度的影像数据
Session信息
sessionInfo() R version 4.2.1 (2022-06-23 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19044) Matrix products: default locale: [1] LC_COLLATE=English_India.utf8 LC_CTYPE=English_India.utf8 [3] LC_MONETARY=English_India.utf8 LC_NUMERIC=C [5] LC_TIME=English_India.utf8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] caret_6.0-92 lattice_0.20-45 ggplot2_3.3.6 MASS_7.3-57 [5] openxlsx_4.2.5 stars_0.5-5 sf_1.0-7 abind_1.4-5 loaded via a namespace (and not attached): [1] nlme_3.1-157 bitops_1.0-7 bit64_4.0.5 [4] lubridate_1.8.0 doParallel_1.0.17 tools_4.2.1 [7] utf8_1.2.2 R6_2.5.1 rpart_4.1.16 [10] KernSmooth_2.23-20 DBI_1.1.3 colorspace_2.0-3 [13] nnet_7.3-17 withr_2.5.0 gbm_2.1.8 [16] tidyselect_1.1.2 gridExtra_2.3 bit_4.0.4 [19] compiler_4.2.1 cli_3.3.0 scales_1.2.0 [22] classInt_0.4-7 randomForest_4.7-1.1 proxy_0.4-27 [25] plotmo_3.6.2 stringr_1.4.0 digest_0.6.29 [28] rmarkdown_2.14 pkgconfig_2.0.3 htmltools_0.5.2 [31] parallelly_1.32.0 plotrix_3.8-2 fastmap_1.1.0 [34] rlang_1.0.3 rstudioapi_0.13 generics_0.1.3 [37] jsonlite_1.8.0 dplyr_1.0.9 ModelMetrics_1.2.2.2 [40] zip_2.2.0 RCurl_1.98-1.7 magrittr_2.0.3 [43] Formula_1.2-4 Matrix_1.4-1 Rcpp_1.0.8.3 [46] munsell_0.5.0 fansi_1.0.3 lifecycle_1.0.1 [49] terra_1.6-1 stringi_1.7.6 pROC_1.18.0 [52] yaml_2.3.5 plyr_1.8.7 recipes_0.2.0 [55] grid_4.2.1 earth_5.3.1 parallel_4.2.1 [58] listenv_0.8.0 crayon_1.5.1 splines_4.2.1 [61] knitr_1.39 pillar_1.7.0 ranger_0.13.1 [64] xgboost_1.6.0.1 future.apply_1.9.0 reshape2_1.4.4 [67] codetools_0.2-18 stats4_4.2.1 glue_1.6.2 [70] evaluate_0.15 data.table_1.14.2 BiocManager_1.30.18 [73] vctrs_0.4.1 foreach_1.5.2 gtable_0.3.0 [76] purrr_0.3.4 kernlab_0.9-31 future_1.26.1 [79] assertthat_0.2.1 TeachingDemos_2.12 xfun_0.31 [82] gower_1.0.0 prodlim_2019.11.13 h2o_3.36.1.2 [85] lwgeom_0.2-8 e1071_1.7-11 vip_0.3.2 [88] class_7.3-20 survival_3.3-1 timeDate_3043.102 [91] tibble_3.1.7 iterators_1.0.14 fastAdaboost_1.0.0 [94] hardhat_1.2.0 units_0.8-0 lava_1.6.10 [97] globals_0.15.1 ellipsis_0.3.2 ipred_0.9-12
内容的提问来源于stack exchange,提问作者UseR10085
相关产品推荐
相关产品推荐

