Wednesday, June 28, 2017

Adaptive Resonance Theory in Java

java-adaptive-resonance-theory


Package provides java implementation of algorithms in the field of adaptive resonance theory (ART)
Build Status Coverage Status

Install

Add the following dependency to your POM file:
<dependency>
  <groupId>com.github.chen0040</groupId>
  <artifactId>java-adaptive-resonance-theory</artifactId>
  <version>1.0.5</version>
</dependency>

Features

Algorithms included:
  • ART1
  • FuzzyART
  • ARTMAP
Applications included:
  • Clustering (FuzzyART, ART1)
  • Multi-class Classification (ARTMAP)

Usage

Multi-class Classification using ARTMAP

To create and train a ARTMAP classifier:
ARTMAPClassifier classifier = new ARTMAPClassifier();
clasifier.fit(trainingData);
The "trainingData" is a data frame which holds data rows with labeled output (Please refers to this link to find out how to store data into a data frame)
To predict using the trained ARTMAP classifier:
String predicted_label = classifier.transform(dataRow);
The detail on how to use this can be found in the unit testing codes. Below is a complete sample codes of classifying on the libsvm-formatted heart-scale data:
InputStream inputStream = new FileInputStream("heart_scale");
DataFrame dataFrame = DataQuery.libsvm().from(inputStream).build();

// as the dataFrame obtained thus far has numeric output instead of labeled categorical output, the code below performs the categorical output conversion
dataFrame.unlock();
for(int i=0; i < dataFrame.rowCount(); ++i){
 DataRow row = dataFrame.row(i);
 row.setCategoricalTargetCell("category-label", "" + row.target());
}
dataFrame.lock();

double alpha = 9.89;
double beta = 0.3;
double rho = 0.01;
classifier.setAlpha(alpha);
classifier.setBeta(beta);
classifier.setRho0(rho);

classifier.fit(dataFrame);

for(int i = 0; i < dataFrame.rowCount(); ++i){
  DataRow tuple = dataFrame.row(i);
  String predicted_label = classifier.transform(tuple);
  System.out.println("predicted: "+predicted_label+"\tactual: "+tuple.categoricalTarget());
}

Spatial Segmentation (Clustering) using ART1

The following sample code shows how to do clustering using ART1:
DataQuery.DataFrameQueryBuilder schema = DataQuery.blank()
      .newInput("c1")
      .newInput("c2")
      .newOutput("designed")
      .end();

Sampler.DataSampleBuilder negativeSampler = new Sampler()
      .forColumn("c1").generate((name, index) -> randn() * 0.3 + (index % 2 == 0 ? 2 : 4))
      .forColumn("c2").generate((name, index) -> randn() * 0.3 + (index % 2 == 0 ? 2 : 4))
      .forColumn("designed").generate((name, index) -> 0.0)
      .end();

Sampler.DataSampleBuilder positiveSampler = new Sampler()
      .forColumn("c1").generate((name, index) -> rand(-4, -2))
      .forColumn("c2").generate((name, index) -> rand(-2, -4))
      .forColumn("designed").generate((name, index) -> 1.0)
      .end();

DataFrame data = schema.build();

data = negativeSampler.sample(data, 200);
data = positiveSampler.sample(data, 200);

System.out.println(data.head(10));

ART1Clustering algorithm = new ART1Clustering();

DataFrame learnedData = algorithm.fitAndTransform(data);

for(int i = 0; i < learnedData.rowCount(); ++i){
 DataRow tuple = learnedData.row(i);
 String clusterId = tuple.getCategoricalTargetCell("cluster");
 System.out.println("learned: " + clusterId +"\tknown: "+tuple.target());
}

Image Segmentation (Clustering) using FuzzyART

The following sample code shows how to use FuzzyART to perform image segmentation:
BufferedImage img= ImageIO.read(FileUtils.getResource("1.jpg"));

DataFrame dataFrame = ImageDataFrameFactory.dataFrame(img);

FuzzyARTClustering cluster = new FuzzyARTClustering();

DataFrame learnedData = cluster.fitAndTransform(dataFrame);

for(int i=0; i <learnedData.rowCount(); ++i) {
 ImageDataRow row = (ImageDataRow)learnedData.row(i);
 int x = row.getPixelX();
 int y = row.getPixelY();
 String clusterId = row.getCategoricalTargetCell("cluster");
 System.out.println("cluster id for pixel (" + x + "," + y + ") is " + clusterId);
}
The segmented image can be generated using the trained KMeans from above as illustrated by the following sample code:
List<Integer> classColors = new ArrayList<Integer>();
for(int i=0; i < 5; ++i){
 for(int j=0; j < 5; ++j){
    classColors.add(ImageDataFrameFactory.get_rgb(255, rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)));
 }
}

BufferedImage segmented_image = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
for(int x=0; x < img.getWidth(); x++)
{
 for(int y=0; y < img.getHeight(); y++)
 {
    int rgb = img.getRGB(x, y);

    DataRow tuple = ImageDataFrameFactory.getPixelTuple(x, y, rgb);

    int clusterIndex = cluster.transform(tuple);

    rgb = classColors.get(clusterIndex % classColors.size());

    segmented_image.setRGB(x, y, rgb);
 }
}

Algorithms and Data Structures in Lua

lua-algorithms


This library was developed with the purpose of making general algorithms and data structures available in Java language also available in Lua.
The library covers commonly used data structures (ArrayList, Stack, Queue, Priority Queue, Balanced Search Tree, HashMap, Set, Tries) and algorithms (various sorting and search algorithms, shuffling, union find, etc)

Install

luarocks install lualgorithms

Usage

Common Data Structures

Stack

local stack = require 'lualgorithms.data.stack'

local s = stack.create()
s:push(1)
s:push(2)
s:push(3)
print(s:size())
print(s:isEmpty())
print(s:pop())
for index,val in pairs(s:enumerate()) do
    print(index, val)
end

List

The list behaves the same as the Java ArrayList API, and is zero-based indexing.
local list = require 'lualgorithms.data.list'

local s = list.create()
s:add(1) -- s becomes [1]
s:add(2) -- s becomes [1, 2]
s:add(3) -- s becomes [1, 2, 3]
s:set(2, 4) -- s becomes [1, 2, 4]

for index,val in pairs(s:enumerate()) do
    print(index, val)
end
for i = 0,s:size()-1 do
    print(s:get(i))
end
print(s:size())
print(s:isEmpty())
s:removeAt(0) -- s becomes [2, 4]
s:remove(2) -- s becomes [4]
s:removeAt(0) -- s is now empty

Queue

 local queue = require('lualgorithms.data.queue')
local s = queue.create()
s:enqueue(10)
s:enqueue(20)
s:enqueue(30)
print(s:size()) -- return 3
print(s:isEmpty()) -- return false
for key,value in pairs(s:enumerate()) do
    print(key, value)
end
print(s:dequeue()) -- return 10
print(s:dequeue()) -- return 20
print(s:dequeue()) -- return 30

MinPQ

local minpq = require('lualgorithms.data.minpq')
local comparer = function(a1, a2) return a1 - a2 end -- method that return negative value if if a1 < a2; 0 if a1 == a2; positive otherwise
local s = minpq.create(comparer)
s:enqueue(10)
s:enqueue(100)
s:enqueue(20)
s:enqueue(50)
print(s:size()) -- return 4
print(s:isEmpty()) -- return false

print(s:delMin()) -- return 10
print(s:delMin()) -- return 20
print(s:delMin()) -- return 50
print(s:delMin()) -- return 100

print(s:isEmpty()) -- return true

MaxPQ

local maxpq = require('lualgorithms.data.maxpq')
local comparer = function(a1, a2) return a1 - a2 end -- method that return negative value if if a1 < a2; 0 if a1 == a2; positive otherwise
local s = maxpq.create(comparer)
s:enqueue(10)
s:enqueue(100)
s:enqueue(20)
s:enqueue(50)
print(s:size()) -- return 4
print(s:isEmpty()) -- return false

print(s:delMax()) -- return 100
print(s:delMax()) -- return 50
print(s:delMax()) -- return 20
print(s:delMax()) -- return 10

print(s:isEmpty()) -- return true

HashSet

local hashset = require('lualgorithms.data.hashset')
local hash_func = function(x) return x % 1000 end
local s = hashset.create(hash_func)
s:add(100, 2)
s:add(200, 4)
s:add(450, 2)

print(s:contains(99)) -- return false
print(s:contains(100)) -- return true
print(s:size()) -- return 3
print(s:isEmpty()) -- return false
s:remove(100)
print(s:contains(100)) -- return false)

HashMap

local hashmap = require('lualgorithms.data.hashmap')
local hash_func = function(x) return x % 1000 end
local s = hashmap.create(hash_func)
s:put(100, 2)
s:put(200, 4)
s:put(450, 2)
print(s:get(100)) -- return  2
print(s:get(200)) -- return  4
print(s:get(450)) -- return  2
print(s:get(99)) -- return  nil
print(s:containsKey(99)) -- return  false
print(s:containsKey(100)) -- return  true
print(s:size()) -- return  3
print(s:isEmpty()) -- return  false
print(s:remove(100)) -- return  2
print(s:containsKey(100)) -- return  false
print(s:size()) -- return  2
s:remove(200)
s:remove(450)
print(s:isEmpty()) -- return  true

SortedMap (Left-Leaning Red Black Tree)

local map = require('lualgorithms.data.redblacktree')
local comparator = function(a1, a2) return a1 - a2 end
local s = map.create(comparator)
s:put(100, 2)
s:put(200, 4)
s:put(450, 2)
print(s:minKey()) -- return 100
print(s:maxKey()) -- return 450
print(s:get(100)) -- return  2
print(s:get(200)) -- return  4
print(s:get(450)) -- return  2
print(s:get(99)) -- return  nil
print(s:containsKey(99)) -- return  false
print(s:containsKey(100)) -- return  true
print(s:size()) -- return  3
print(s:isEmpty()) -- return  false
print(s:remove(100)) -- return  2
print(s:containsKey(100)) -- return  false
print(s:size()) -- return  2
s:remove(200)
s:remove(450)
print(s:isEmpty()) -- return  true

Sorting

Note that the default is to sort ascendingly, which can be reversed via the comparator function pass in as the second parameter.

Sorting (Selection Sort)

local list = require("lualgorithms.data.list")
local a = list.create()
a:add(100)
a:add(200)
a:add(300)
a:add(600)
a:add(200)
a:add(400)
a:add(340)
a:add(120)
a:add(10)

local selection = require("lualgorithms.sorting.selection")
selection.sort(a, function(a1, a2) return a1 - a2 end)

for i=0,(a:size()-1) do
    print(a:get(i))
end

Sorting (Insertion Sort)

local list = require("lualgorithms.data.list")
local a = list.create()
a:add(100)
a:add(200)
a:add(300)
a:add(600)
a:add(200)
a:add(400)
a:add(340)
a:add(120)
a:add(10)

local insertion = require("lualgorithms.sorting.insertion")
insertion.sort(a, function(a1, a2) return a1 - a2 end)

for i=0,(a:size()-1) do
    print(a:get(i))
end

Sorting (Shell Sort)

local list = require("lualgorithms.data.list")
local a = list.create()
a:add(100)
a:add(200)
a:add(300)
a:add(600)
a:add(200)
a:add(400)
a:add(340)
a:add(120)
a:add(10)

local shellsort = require("lualgorithms.sorting.shellsort")
shellsort.sort(a, function(a1, a2) return a1 - a2 end)

for i=0,(a:size()-1) do
    print(a:get(i))
end

Sorting (Merge Sort)

local list = require("lualgorithms.data.list")
local a = list.create()
a:add(100)
a:add(200)
a:add(300)
a:add(600)
a:add(200)
a:add(400)
a:add(340)
a:add(120)
a:add(10)

local mergesort = require("lualgorithms.sorting.mergesort")
mergesort.sort(a, function(a1, a2) return a1 - a2 end)

for i=0,(a:size()-1) do
    print(a:get(i))
end

Sorting (Quick Sort)

local list = require("lualgorithms.data.list")
local a = list.create()
a:add(100)
a:add(200)
a:add(300)
a:add(600)
a:add(200)
a:add(400)
a:add(340)
a:add(120)
a:add(10)

local quicksort = require("lualgorithms.sorting.quicksort")
quicksort.sort(a, function(a1, a2) return a1 - a2 end)

for i=0,(a:size()-1) do
    print(a:get(i))
end

Sorting (3-ways Quick Sort)

local list = require("lualgorithms.data.list")
local a = list.create()
a:add(100)
a:add(200)
a:add(300)
a:add(600)
a:add(200)
a:add(400)
a:add(340)
a:add(120)
a:add(10)

local quicksort3ways = require("lualgorithms.sorting.quicksort3ways")
quicksort3ways.sort(a, function(a1, a2) return a1 - a2 end)

for i=0,(a:size()-1) do
    print(a:get(i))
end