Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Did you know that 50- 80% of your enterprise business processes can be automated with AssistEdge?  Identify processes, deploy bots and scale effortlessly with AssistEdge.

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.

Setup

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

<dependency>
    <groupId>io.github.knolduslabs</groupId>
    <artifactId>ksai_2.12</artifactId>
    <version>0.0.4</version>
</dependency>

Now we are ready to code.

Implementation

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.

Thanks!

This article was first published on the Knoldus blog.

Consuming AI in byte sized applications is the best way to transform digitally. #BuiltOnAI, EdgeVerve’s business application, provides you with everything you need to plug & play AI into your enterprise.  Learn more.

Topics:
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 }}