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
<dependency> <groupId>io.github.knolduslabs</groupId> <artifactId>ksai_2.12</artifactId> <version>0.0.4</version> </dependency>
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 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)
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.