Over a million developers have joined DZone.

MachineX: Logistic Regression With KSAI

DZone 's Guide to

MachineX: Logistic Regression With KSAI

Let's take a look at this brief article, which includes a tutorial on using the KSAI library to build a logistic regression model.

· AI Zone ·
Free Resource

This article was first published on the Knoldus blog.

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 article. 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
import ksai.data.parser.ARFFParser

Now use the following code in your application:

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

val trainingInstances: Array[Array[Double]] = arffFile.data.toArray
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)

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.


This article was first published on the Knoldus blog.

artificial intelligence ,logistic regression ,ksai ,tutorial ,machinex

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}