In many of my previous blogs, I have posted about Association Rule Learning, what it’s about and how it is performed. In this blog, we are going to use Association Rule Learning to actually see it in action, and for this purpose, we are going to use KSAI, a machine learning library purely written in Scala. So, let’s begin.
Adding KSAI to your project
You can add KSAI in your SBT project using the following import line –
libraryDependencies += "io.github.knolduslabs" %% "ksai" % "0.0.4"
You can add KSAI in your maven project using the following –
<dependency> <groupId>io.github.knolduslabs</groupId> <artifactId>ksai_2.12</artifactId> <version>0.0.4</version> </dependency>
Using KSAI for Association Rule Learning
I’ll be using the data file kosarak.dat for demonstrating the application. I will also be including this file in the github repository that will be provided below so that you guys can also play around with it.
To use the algorithm, you will first need to create an ARM object. For that, you need to parse your data into an Array[Array[Int]]. Below given code is what I use to perform the same.
val data: Array[Array[Int]] = Source.fromFile(getClass.getResource("/kosarak.dat").getPath) .getLines() .map(_.split(" ").map(_.toInt)) .toArray
Awesome! Now we can create the ARM object. We do this as follows –
val arm = ARM(data, 0.003)
Now we gotta generate the rules, for which we can simply call the learn method of the ARM. We get something like below –
val eventualResults = arm.learn(0.5)
What’s 0.003 and 0.5? That’s the minimum support and confidence that we are going to use. Don’t know about them? Then you must checkout my previous blogs which explain in depth about what they are and what their value should be.
To see all the rules that are generated, we can simple print them out as below –
eventualResults.map(array => array.foreach(rule => println(rule)))
And we will be able to see all the rules, with the support and confidence, on the console.
You can get the code and the data file used in this example here.
That’s it for this blog. You can find many more interesting algorithms in KSAI right here.
Thanks for reading!