1

我有以下代码:

#load required library
library(data.tree)
library(entropy)
library(RWeka)
library(partykit)
library(FSelector)
library(e1071)
library(caret)
library(RWekajars)


#Load dataset
rest_contries <- fromJSON("https://restcountries.eu/rest/v1/all")
View(rest_contries)
class(rest_contries)
dim(rest_contries)
cleaned_rest_countries <- rest_contries[,c(1,2,5,6)]
View(cleaned_rest_countries)
#================ J48 Algorithm =================================================
m <- J48(region~., data = cleaned_rest_countries)
plot(m)

这应该为我绘制来自 J48 算法的贝叶斯网络图。我怎么会收到以下错误:

> m <- J48(region~., data = cleaned_rest_countries)
Error in .jcall(o, "Ljava/lang/Class;", "getClass") : 
  weka.core.UnsupportedAttributeTypeException: weka.classifiers.trees.j48.C45PruneableClassifierTree: Cannot handle string attributes!

请协助解决问题。当我从 CSV 读取数据时,它运行良好,但从 json 中我得到一个字符串属性错误。

4

1 回答 1

1

我怀疑您需要将字符变量转换为因子。

> cleaned_rest_countries <- lapply(cleaned_rest_countries, as.factor)
> m <- J48(region~., data = cleaned_rest_countries)
> m
J48 pruned tree
------------------

subregion = :  (3.0)
subregion = Australia and New Zealand: Oceania (5.0)
subregion = Caribbean: Americas (27.0)
subregion = Central America: Americas (8.0)
subregion = Central Asia: Asia (5.0)
subregion = Eastern Africa: Africa (20.0)
subregion = Eastern Asia: Asia (8.0)
subregion = Eastern Europe: Europe (11.0)
subregion = Melanesia: Oceania (5.0)
subregion = Micronesia: Oceania (7.0)
subregion = Middle Africa: Africa (10.0)
subregion = Northern Africa: Africa (7.0)
subregion = Northern America: Americas (6.0)
subregion = Northern Europe: Europe (16.0)
subregion = Polynesia: Oceania (10.0)
subregion = South-Eastern Asia: Asia (11.0)
subregion = South America: Americas (15.0)
subregion = Southern Africa: Africa (5.0)
subregion = Southern Asia: Asia (9.0)
subregion = Southern Europe: Europe (16.0)
subregion = Western Africa: Africa (17.0)
subregion = Western Asia: Asia (17.0)
subregion = Western Europe: Europe (9.0)

Number of Leaves  :     23

Size of the tree :  24
于 2016-06-27T11:53:17.970 回答