Skip to content

Commit

Permalink
Can label strings as labels without erroring out. Will be good for
Browse files Browse the repository at this point in the history
classification problems.  Also updated Readme to include an example
(hopefully nicely formated).
  • Loading branch information
dmcnelis committed Dec 16, 2011
1 parent 678550a commit f637e77
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 17 deletions.
43 changes: 42 additions & 1 deletion README.mkd
Expand Up @@ -9,4 +9,45 @@ existence as this project grows and is refined.

Everything in this framework should be thread safe, if you find something
that is not, please post an issue, a patch, or a unit test illustrating
the issue.
the issue.

Example of running clustering
```java

class MyRecordClass extends Record {
@Feature
double myFeature1
@Feature
double myFeature2
@Feature
double myFeature3

@Label
String myLabel;

Object nonFeatureItem;
String nonFeatureOrLabelString;

//Other stuff I want to do
}

class MyClusterRunner {
public static void main(String[] args) {
double epsilon = 4d; //Maximum distance between records in a cluster
int minClusterSize = 3; //Minimum number of records in a cluster
RecordList myRecords = new RecordList();
//populate your object

DBScan dbscan = new DBScan(epsilon, minClusterSize);
db.setSourceData(list);
ArrayList<Cluster> clusters = db.getClusters();

//do stuff with your clusters i.e. print record label and cluster
for(int i=0; i<clusters.size(); i++) {
for(MyRecordClass r : clusters.getElements()) {
System.out.println("Cluster " + i+ ": " + r.getLabel());
}
}
}
}
```
3 changes: 3 additions & 0 deletions src/main/java/me/mcnelis/rudder/data/ExampleRecord.java
Expand Up @@ -15,4 +15,7 @@ public class ExampleRecord extends Record {

@Label
protected double doubleLabel;

@Label
protected String stringLabel;
}
10 changes: 4 additions & 6 deletions src/main/java/me/mcnelis/rudder/data/Record.java
Expand Up @@ -155,10 +155,9 @@ public double[] getFeatureAndLabelArray() {
annotation = f.getAnnotation(Label.class);
if (annotation != null) {
try {
arr.addElement(f.getDouble(this));
arr.addElement(f.getDouble(this));
} catch (IllegalArgumentException e) {

e.printStackTrace();
//Field or label isn't a double, so ignore it
} catch (IllegalAccessException e) {

e.printStackTrace();
Expand All @@ -169,7 +168,7 @@ public double[] getFeatureAndLabelArray() {
return arr.getElements();
}

public double getLabel() {
public double getDoubleLabel() {

Field[] fields = this.getClass().getDeclaredFields();
for(Field f : fields) {
Expand All @@ -179,8 +178,7 @@ public double getLabel() {
try {
return (f.getDouble(this));
} catch (IllegalArgumentException e) {

e.printStackTrace();
//Not a double, so just move forward
} catch (IllegalAccessException e) {

e.printStackTrace();
Expand Down
Expand Up @@ -41,7 +41,7 @@ public double[] getSupervisedLabels() {
double[] d = new double[this.size()];
int cnt=0;
for (Record r : this) {
d[cnt] = r.getLabel();
d[cnt] = r.getDoubleLabel();
cnt++;
}

Expand Down
Expand Up @@ -12,7 +12,7 @@ protected DBScan(double epsilon, int minPts) {

@Override
protected ArrayList<Cluster> cluster() {
System.out.println("Size: " + this.sourceData.size());

for (Record r : this.sourceData) {
if (!r.isVisited()) {
r.setVisited();
Expand Down
Expand Up @@ -23,9 +23,13 @@ public void testGetClusters() {
r.setFeature("feature1",1*j + i*.0000001);
r.setFeature("feature2", 1*j+ i*.0000001);
r.setFeature("feature3", 1*j+ i*.0000001);
r.setLabel("stringLabel", "label");
} catch (FeatureNotFoundException e) {
e.printStackTrace();
fail("Should not have exception");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

list.add(r);
Expand Down
Expand Up @@ -32,14 +32,7 @@ public void testClustering() {

KMeans k = new KMeans(3, list);
ArrayList<Cluster> clusters = k.cluster();
for(Cluster c : clusters) {
System.out.println(
c.getCentroid()[0] + "\n"
+ c.getCentroid()[1] + "\n"
+ c.getCentroid()[2] + "\n"
);

}
assertTrue(true);
}

}

0 comments on commit f637e77

Please sign in to comment.