MachineX: Logistic Regression with KSAI

Reading Time: 2 minutes

Logistic Regression, a predictive analysis, is mostly used with binary variables for classification and can be extended to use with multiple classes as results also. We have already studied the algorithm in deep with this blog. Today we will be using KSAI library to build our logistic regression model.


First, we need to add the dependency in build.sbt in case of an sbt project

libraryDependencies += "io.github.knolduslabs.ksai" %% "ksai" % "0.0.4"

If you are using a Maven project, use the following in pom.xml


Now we are ready to code.


To use Logistic Regression, we need to import the following in our code:

import ksai.core.classification.LogisticRegression

Now use following code in your application:

private val arffFile = ARFFParser.parse("path/trainFile.arff")

val trainingInstances: Array[Array[Double]] =
val respones: Array[Int] = arffFile.getNumericTargets.toArray

val logisticRegression = LogisticRegression(trainingInstances, respones)

In the above code, ARFFParser needs a sample ARFF file containing training data. Using that we extract the set of input variables called trainingInstances and the output labels called responses. With this code, we get an instance of LogisticRegression as logisticRegression. We can then use predict() method to get output for an input set.

val response: Int = logisticRegression.predict(instance)

Where instance is the input set of type Array[Double] and we get the response for this input set as output.

There are some other parameters as well while creating the object of LogisticRegression class like tolerance which signifies the maximum error that is acceptable. Here is the link to the sample code. Please explore it for more details.

Explore the library here.

Thanks. 🙂


Written by 

Anuj Saxena is a software consultant having 6+ years of experience. He is currently working with functional programming languages like Scala and functional Java with the tech stack of Big Data technologies( Spark, Kafka . . .) and Reactive technologies( Akka, Lagom, Cassandra . . .). He has also worked on DevOps tools like DC/OS and Mesos for Deployments. His hobbies include watching movies, anime and he also loves travelling a lot.