Skip to content

Commit

Permalink
First version - it makes R-1 Classification
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Jaszewski authored and jjroman committed Jan 29, 2013
0 parents commit 0e3d1e9
Show file tree
Hide file tree
Showing 11 changed files with 404 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
55 changes: 55 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ztbd</groupId>
<artifactId>IWI</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>IWI</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.swinglabs</groupId>
<artifactId>swing-layout</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

</dependencies>
</project>
51 changes: 51 additions & 0 deletions src/main/java/ztbd/iwi/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ztbd.iwi;

import java.util.HashMap;
import java.util.Scanner;

/**
* Hello world!
*
*/
public class App {

public static void main(String[] args) {
try {
NaiveClassifier nc;
try (Scanner scan = new Scanner(Thread.currentThread().getContextClassLoader().getResourceAsStream("input2_test.tab"))) {
int noOfOutputClasses = scan.nextInt();
HashMap<String, String> hm = new HashMap<>();
while (hm.size() < noOfOutputClasses) {
hm.put(scan.next(), scan.next());
}
int noOfLines = scan.nextInt();
int noOfIntInLine = scan.nextInt();
int noOfAttrInLine = noOfIntInLine - 1;
String expectedValue = null;
Integer[] buffor = new Integer[noOfAttrInLine];
nc = new NaiveClassifier(noOfAttrInLine, hm);
while (noOfLines-- > 0) {
for (int i = 0; i < noOfAttrInLine; i++) {
buffor[i] = scan.nextInt();
}
expectedValue = scan.next();

for (int i = 0; i < noOfAttrInLine; i++) {
nc.getAttributeForIndex(i).incrementValue(new NBAttributeValue(buffor[i]), nc.getOutputValueForString(expectedValue));
}
}
}

//nc.getVectorProbabilityForAnswer(NBVector nbVec, NBOutputValue ov);
//nc.klasifyVector();
System.out.println("Probability of getting answer 1 for R-1 classification with attribute number 1 of value 0");
System.out.println(nc.getAttributeForIndex(1).getProbablity(new NBAttributeValue(0), nc.getOutputValueForString("1")));

System.out.println("Probability of getting answer 1 for R-1 classification with attribute number 2 of value 3");
System.out.println(nc.getAttributeForIndex(2).getProbablity(new NBAttributeValue(3), nc.getOutputValueForString("1")));

} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/ztbd/iwi/MainPanel.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>

<Form version="1.3" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">

<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="400" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="300" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
</Form>
43 changes: 43 additions & 0 deletions src/main/java/ztbd/iwi/MainPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ztbd.iwi;

/**
*
* @author jjroman
*/
public class MainPanel extends javax.swing.JPanel {

/**
* Creates new form MainPanel
*/
public MainPanel() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}
// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}
54 changes: 54 additions & 0 deletions src/main/java/ztbd/iwi/NBAttribute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ztbd.iwi;

import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
*
* @author jjroman
*/
class NBAttribute {
private int summarisedNoOfEntities = 0;
HashMap <NBAttributeValue, HashMap<NBOutputValue, AtomicInteger>> probabilityMap = new HashMap<>();

/**
* Metoda używana do inkrementalnego uczenia
* @param attrVal
* @param outVal
*/
public void incrementValue(NBAttributeValue attrVal, NBOutputValue outVal) {
// sprawdzenie istnienia
if(probabilityMap.containsKey(attrVal)){
if(probabilityMap.get(attrVal).containsKey(outVal)){
probabilityMap.get(attrVal).get(outVal).incrementAndGet();
}else{
probabilityMap.get(attrVal).put(outVal, new AtomicInteger(1));
}
}else{
HashMap<NBOutputValue, AtomicInteger> hm = new HashMap<>();
hm.put(outVal, new AtomicInteger(1));
probabilityMap.put(attrVal,hm);
}
summarisedNoOfEntities++;
}

public double getProbablity(NBAttributeValue attrVal, NBOutputValue outVal){
if(summarisedNoOfEntities > 0
&& probabilityMap.containsKey(attrVal)
&& probabilityMap.get(attrVal).containsKey(outVal)
){

return ((double)(probabilityMap.get(attrVal).get(outVal).get() ))/((double) probabilityMap.get(attrVal).size());
}

return almostZero();
}

private double almostZero(){
return 1.0 / (summarisedNoOfEntities+1);
}
}
40 changes: 40 additions & 0 deletions src/main/java/ztbd/iwi/NBAttributeValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ztbd.iwi;

/**
*
* @author jjroman
*/
class NBAttributeValue implements Comparable<NBAttributeValue> {

private int value;
public NBAttributeValue(int value ) {
this.value = value;
}

public int getValue(){
return value;
}

@Override
public boolean equals(Object o){
return (o instanceof NBAttributeValue) && ((NBAttributeValue)o).getValue() == this.getValue();
}

@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + this.value;
return hash;
}
@Override
public int compareTo(NBAttributeValue o) {
return (this.getValue() - o.getValue());
}



}
38 changes: 38 additions & 0 deletions src/main/java/ztbd/iwi/NBOutputValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ztbd.iwi;

import java.util.Objects;

/**
*
* @author jjroman
*/
class NBOutputValue {
int key = 0;
String name = null;

public NBOutputValue(String key, String name) {
this(Integer.parseInt(key), name);
}

public NBOutputValue(int key, String name) {
this.key = key;
this.name = name;
}

@Override
public boolean equals(Object o){
return (o instanceof NBOutputValue) && ((NBAttributeValue)o).hashCode() == this.hashCode();
}

@Override
public int hashCode() {
int hash = 3;
hash = 19 * hash + this.key;
hash = 19 * hash + Objects.hashCode(this.name);
return hash;
}
}
58 changes: 58 additions & 0 deletions src/main/java/ztbd/iwi/NaiveClassifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ztbd.iwi;

import java.util.HashMap;
import java.util.Map;

/**
*
* @author jjroman
*/
public class NaiveClassifier {

private NBAttribute[] attributes = null;
private HashMap<String,NBOutputValue> ovMap = null;

/**
*
* @param noOfAttributes
* @param ovMap
*/
public NaiveClassifier(int noOfAttributes, Map<String,String> ovMap) {
initAttributes(noOfAttributes);
initOutputValues(ovMap);
}



private void initAttributes(int noOfAttributes) {
this.attributes = new NBAttribute[noOfAttributes];
for (int i = 0; i < noOfAttributes; i++) {
this.attributes[i] = new NBAttribute();
}
}

private void initOutputValues(Map<String,String> ovMap) {
this.ovMap = new HashMap<>();

for(Map.Entry<String, String> me : ovMap.entrySet()){
this.ovMap.put(me.getKey(), new NBOutputValue(me.getKey(), me.getValue()));
}
}

public NBAttribute getAttributeForIndex(int index) {
return this.attributes[index];
}

public NBOutputValue getOutputValueForString(String ov) {
return this.ovMap.get(ov);
}

public void train(NBAttribute attr, NBAttributeValue attrVal, NBOutputValue outVal) {
attr.incrementValue(attrVal, outVal);
}

}
9 changes: 9 additions & 0 deletions src/main/resources/input2_test.tab
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
2
1 green
2 red
5 5
1 1 2 0 0
0 2 1 0 1
0 1 3 0 1
1 1 3 1 0
1 2 1 1 0
Loading

0 comments on commit 0e3d1e9

Please sign in to comment.