【问题标题】:Error in UseMethod("accuracy") : no applicable method for 'accuracy' applied to an object of class "c('double', 'numeric')"UseMethod(“accuracy”)错误:没有适用于“accuracy”的适用方法应用于类“c('double','numeric')”的对象
【发布时间】:2021-07-21 01:22:15
【问题描述】:

我试图预测 r 中二手车数据的价格。我已经完成了所有的预处理并将数据分为训练集和测试集。这里我使用回归树。当我试图获得准确性时,我得到了这个错误。

library(rpart)
library(tidyverse)
library(dplyr)
dput(head(train.df, 5))

reg_tree <- rpart(price ~ ., 
            data = train.df,
            method = "anova", minbucket = 1, maxdepth = 30, cp = 0.001)

accuracy(predict(reg_tree, train.df), train.df$price)
structure(list(price = 33990, year = 2018L, manufacturer = structure(1L, .Label = c("acura", 
"alfa-romeo", "aston-martin", "audi", "bmw", "buick", "cadillac", 
"chevrolet", "chrysler", "datsun", "dodge", "ferrari", "fiat", 
"ford", "gmc", "harley-davidson", "honda", "hyundai", "infiniti", 
"jaguar", "jeep", "kia", "land rover", "lexus", "lincoln", "mazda", 
"mercedes-benz", "mercury", "mini", "mitsubishi", "nissan", "pontiac", 
"porsche", "ram", "rover", "saturn", "subaru", "tesla", "toyota", 
"volkswagen", "volvo"), class = "factor"), condition = structure(4L, .Label = c("excellent", 
"fair", "good", "like new", "new", "salvage"), class = "factor"), 
    cylinders = structure(6L, .Label = c("10 cylinders", "12 cylinders", 
    "3 cylinders", "4 cylinders", "5 cylinders", "6 cylinders", 
    "8 cylinders", "other"), class = "factor"), fuel = structure(3L, .Label = c("diesel", 
    "electric", "gas", "hybrid", "other"), class = "factor"), 
    odometer = 22267, title_status = structure(1L, .Label = c("clean", 
    "lien", "missing", "parts only", "rebuilt", "salvage"), class = "factor"), 
    transmission = structure(1L, .Label = c("automatic", "manual", 
    "other"), class = "factor"), drive = structure(2L, .Label = c("4wd", 
    "fwd", "rwd"), class = "factor"), size = structure(3L, .Label = c("compact", 
    "full-size", "mid-size", "sub-compact"), class = "factor"), 
    type = structure(4L, .Label = c("bus", "convertible", "coupe", 
    "hatchback", "mini-van", "offroad", "other", "pickup", "sedan", 
    "SUV", "truck", "van", "wagon"), class = "factor"), paint_color = structure(10L, .Label = c("black", 
    "blue", "brown", "custom", "green", "grey", "orange", "purple", 
    "red", "silver", "white", "yellow"), class = "factor")), row.names = 31113L, class = "data.frame")


Error in UseMethod("accuracy") : 
  no applicable method for 'accuracy' applied to an object of class "c('double', 'numeric')"

谁能帮帮我。

提前致谢。

【问题讨论】:

  • 您可以发布示例数据吗?请使用dput(train.df) 的输出编辑问题。或者,如果 dput(head(train.df, 20)) 的输出太大。请在脚本开头调用library() 加载您正在使用的包。

标签: r regression rpart


【解决方案1】:

假设我不知道 accuracy 函数来自哪个包(可能是 MLmetrics::Accuracy??),但是,错误是由于您使用的关于问题类型的度量:准确度是用于分类问题,其结果通常是 factor,只能具有某些值(离散变量)。由于您的结果类别(numeric),在这里您正在拟合回归模型。汽车的价格可以在一定范围内不断变化。因此,对于回归问题,评估模型性能最常用的指标之一是均方根误差 (RMSE)。 RMSE 函数在caret 包中实现。在这里,我发布了一个示例,其中包含来自包 `rpart:

的内置数据集 cars.test.frame
library(rpart)
library(tidyverse)
library(dplyr)
library(caret)

data("car.test.frame")
ind <- createDataPartition(car.test.frame$Price,p=.8,list=F)
train.df <- car.test.frame[ind,]
test.df <- car.test.frame[-ind,]

reg_tree <- rpart(Price ~ ., 
                  data = train.df,
                  method = "anova", minbucket = 1, maxdepth = 30, cp = 0.001)



rmse <- RMSE(predict(reg_tree, test.df), test.df$Price)
rmse_perc <- rmse/mean(test.df$Price)*100

RMSE 可以报告为平均汽车价格的百分比。您还可以实现自己的 rmse 函数,因为它易于计算:

rmse <- function (y_pred, y_true) 
{
  RMSE <- sqrt(mean((y_true - y_pred)^2))
  return(RMSE)
}

但是,上面的rmse 函数与caret 包的RMSE 相同

【讨论】:

    【解决方案2】:

    随便用

    library(forecast)
    

    它会起作用的!

    【讨论】:

      猜你喜欢
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 2021-07-23
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 2022-10-05
      相关资源
      最近更新 更多