对于csv读取的数据集,“类变量需要成为一个因素”错误

问题描述 投票:0回答:1

我希望离散化数据集中的连续特征,尤其是使用监督的。事实证明, [对此有一种包装/方法] 1,太好了!但是由于我不精通R,所以遇到了一些问题,如果您能提供帮助,我将不胜感激。

我收到错误

类变量必须是一个因素。

我在网上看了一个例子,他们似乎没有这个问题,但是我知道。请注意,除了应该是列名之外,我不太了解V2 ~ . V2

library(caret)
library(Rcpp)
library(arulesCBA)

filename <- "wine.data"
dataset <- read.csv(filename, header=FALSE)
dataset2 <- discretizeDF.supervised(V2 ~ ., dataset, method = "mdlp")

R报告以下错误:

。parseformula(公式,数据)中的错误:类变量需要为一个因素!

您可以在以下位置找到数据集wine.data:https://pastebin.com/hvDbEtMNdiscretizeDF.supervised的第一个参数是一个公式,这似乎是问题所在。

请帮助!预先谢谢你。

r syntax discretization
1个回答
0
投票

如小插图中所写,是为了实现:

几种将连续变量转换为变量的监督方法适用于关联规则挖掘的分类变量(因子)并建立关联的分类器。

如果您查看V2列,它是连续的:

test = read.csv("wine_dataset.txt",header=FALSE)
str(test)
'data.frame':   178 obs. of  14 variables:
 $ V1 : int  1 1 1 1 1 1 1 1 1 1 ...
 $ V2 : num  14.2 13.2 13.2 14.4 13.2 ...
 $ V3 : num  1.71 1.78 2.36 1.95 2.59 1.76 1.87 2.15 1.64 1.35 ...

您需要的是一个明确的目标,以便该算法可以找到合适的方法来离散化它以找到关联。例如:

#this cuts V2 into 4 categories according to where they fall in the range
test$V2 = factor(cut(test$V2,4,labels=1:4))
dataset2 <- discretizeDF.supervised(V2 ~ ., dataset, method = "mdlp")

以上是解决问题的一种方法,但是您需要找到很好地削减V2的方法。如果需要将目标用作连续目标,则可以使用规则中的discretizeDF,我也看到您的第一列仅是1,2,3:

test = read.csv("wine_dataset.txt",header=FALSE)
test2 = data.frame(test[,1:2],discretizeDF(test[,-c(1:2)]))
© www.soinside.com 2019 - 2024. All rights reserved.