【问题标题】:preProcess function R预处理函数 R
【发布时间】:2021-05-09 23:07:30
【问题描述】:

我对 R 很陌生。

我正在尝试遵循以下程序:

imputedData <- preProcess( select(train, -SalePrice),
  method = c("center", "scale", "knnImpute", "nzv", 'YeoJohnson')
  )
#install.packages('RANN')
library(RANN)
trainTrans <- predict(imputedData, train)

我有这个错误

Must subset rows with a valid subscript vector. x Subscript nn$nn.idx must be a simple vector, not a matrix.

我已经安装了 Caret 包

火车数据集是我从 CSV 文件中导入的表格

【问题讨论】:

  • 我无法重现您的错误。你真的在 predict() 遇到错误?

标签: r preprocessor


【解决方案1】:

这是程序

加载包

knitr::opts_chunk$set(echo = TRUE, cache = TRUE, message = FALSE, warning = FALSE)

library(tidyverse)
library(caret)
library(GGally)
library(lattice)
library(corrplot)
library(factoextra)
library(FactoMineR)
library(magrittr)
theme_set(theme_bw())
set.seed(181019)

数据加载

train <- readr::read_csv("train.csv")
test  <- readr::read_csv("test.csv")

缺失值

missing_threshold <- .4
is_too_scarce <- lapply(select(train, -SalePrice), function(x) mean(is.na(x)) > missing_threshold)
is_too_scarce <- map_lgl(select(train, -SalePrice), ~mean(is.na(.x)) > missing_threshold)
not_too_scarce <- names(is_too_scarce)[!is_too_scarce]
train <- select(train, SalePrice, not_too_scarce)
train %<>% select(SalePrice, not_too_scarce)
test  %<>% select(not_too_scarce)

预处理

imputedData <- preProcess( select(train, -SalePrice),
  method = c("center", "scale", "knnImpute", "nzv", 'YeoJohnson')
  )
#install.packages('RANN')
library(RANN)
testTrans  <- predict(imputedData, test)
trainTrans <- predict(imputedData, train)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-06
    • 2023-03-27
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多