Over a million developers have joined DZone.

Growing a Spam Tree

· Big Data Zone

Learn how you can maximize big data in the cloud with Apache Hadoop. Download this eBook now. Brought to you in partnership with Hortonworks.

Consider the following toy dataset, with some spam/ham information, and two words, “viagra” and “lottery”.

> load(spam.RData)
> head(db)
      Y viagra lottery
27 spam      0       1
37  ham      0       1
57 spam      0       0
89  ham      0       0
20 spam      1       0
86  ham      0       0

For the first node, compute Gini index for the two variables,

> gini=function(variable){
+ T=table(db$Y,db[,variable])
+ nx=apply(T,2,sum)
+ ProbCond=T/matrix(rep(nx,each=2),2,2)
+ ProbCond
+ Gini=-ProbCond*(1-ProbCond)
+ sum(matrix(rep(nx,each=2),2,2)/sum(nx)*Gini)}
> gini("viagra")
[1] -0.44
> gini("lottery")
[1] -0.487

Here the Gini index is maximal for “viagra”, so that will be the first node.

On the left node (emails without “viagra”), the component of Gini index is

> -75/100*(.4*.6+.6*.4)
[1] -0.36

If we decide to split (using the second word, “lottery”), at this node, the new Gini index would be

> idx=which(db$viagra==0)
> T=table(db[idx,"Y"],db[idx,"lottery"])
> nx=apply(T,2,sum)
> ProbCond=T/matrix(rep(nx,each=2),2,2)
> Gini=-ProbCond*(1-ProbCond)
> sum(matrix(rep(nx,each=2),2,2)/100*
+       Gini)
[1] -0.333

Since Gini index is larger, we decide to split (based on the second word) here. On the other node (emails with “viagra”), the component of Gini index is

> -25/100*(.8*.2+.2*.8)
[1] -0.08

and if decide to split (again, according to the second word), we get

> idx=which(db$viagra==1)
> T=table(db[idx,"Y"],db[idx,"lottery"])
> nx=apply(T,2,sum)
> ProbCond=T/matrix(rep(nx,each=2),2,2)
> Gini=-ProbCond*(1-ProbCond)
> sum(matrix(rep(nx,each=2),2,2)/100*
+       Gini)
[1] -0.0792

which is only slightly larger. Splitting would not be very interesting, here. To visualize the tree, use

> library(rpart)
> arbre = rpart(factor(Y)~.,data=db)
> library(rpart.plot)
> rpart.plot(arbre,type=4,extra=6)

Hortonworks DataFlow is an integrated platform that makes data ingestion fast, easy, and secure. Download the white paper now.  Brought to you in partnership with Hortonworks

Topics:
bigdata ,big data ,data visualization

Published at DZone with permission of Arthur Charpentier, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}