MachineX: Association Rule Learning With KSAI
Let's 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.
Join the DZone community and get the full member experience.
Join For FreeIn many of my previous articles, I have posted about Association Rule Learning, what it's about, and how it is performed. In this article, 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"
Or, you can add KSAI in your Maven project by 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]]. The 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 check out my previous articles, which explain in depth about what they are and what their value should be.
To see all the rules that are generated, we can simply 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 article! You can find many more interesting algorithms in KSAI right here.
Thanks for reading, and let me know your thoughts in the comments section.
Published at DZone with permission of Akshansh Jain, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments