chen0040/java-naive-bayes-classifier


Package provides java implementation of naive bayes classifier

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.chen0040:java-naive-bayes-classifier:'
	}
	dependencies {
		implementation("com.github.chen0040:java-naive-bayes-classifier:")
	}
	<dependency>
	    <groupId>com.github.chen0040</groupId>
	    <artifactId>java-naive-bayes-classifier</artifactId>
	    <version></version>
	</dependency>

                            
    libraryDependencies += "com.github.chen0040" % "java-naive-bayes-classifier" % ""
        
        

                            
    :dependencies [[com.github.chen0040/java-naive-bayes-classifier ""]]
        
        

Readme


java-naive-bayes-classifier

Package provides java implementation of naive bayes classifier (NBC)

Build Status Coverage Status

Features

  • Handle both numerical and categorical inputs

Install

Add the following dependency to your POM file

<dependency>
  <groupId>com.github.chen0040</groupId>
  <artifactId>java-naive-bayes-classifier</artifactId>
  <version>1.0.1</version>
</dependency>

Usage

To train the NBC:

nbc.fit(trainingData);

To use NBC for classification:

String predicted = nbc.classify(dataRow);

The trainingData object is an instance of data frame consisting of data rows (Please refers to this link to find out how to store data into a data frame)

The sample code below shows how to use NBC to solves the classification problem "heart_scale".

InputStream inputStream = new FileInputStream("heart_scale");

DataFrame dataFrame = DataQuery.libsvm().from(inputStream).build();


dataFrame.unlock();
for(int i=0; i < dataFrame.rowCount(); ++i){
 DataRow row = dataFrame.row(i);
 row.setCategoricalTargetCell("category-label", "" + row.target());
}
dataFrame.lock();

NBC svc = new NBC();
svc.fit(dataFrame);

for(int i = 0; i < dataFrame.rowCount(); ++i){
 DataRow row = dataFrame.row(i);
 String predicted_label = svc.classify(row);
 System.out.println("predicted: "+predicted_label+"\texpected: "+row.categoricalTarget());
}