问题
I have the following code :
#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)
Which is supposed to plot for me a bayesian network diagram from the J48 algorithm . How ever I get the following error :
> 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!
Please assist with solving the problem. When I read data from a CSV , it works well but from a json I get an string attribute error .
回答1:
I suspect you need to transform your character variables to factors.
> 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
来源:https://stackoverflow.com/questions/38052572/weka-core-unsupportedattributetypeexception-weka-classifiers-trees-j48-c45prune