Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thorongil committed May 28, 2020
1 parent e015054 commit 93e44b3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
4 changes: 2 additions & 2 deletions k-means/src/main/java/it/unipi/hadoop/model/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public float distance(Point p, int h){
if (h == 0)
return -1;

if (h == Float.POSITIVE_INFINITY) {
if (h == Integer.MAX_VALUE) {
// Chebyshev distance
float max = -1f;
float diff = 0.0f;
Expand All @@ -104,7 +104,7 @@ public float distance(Point p, int h){
for (int i = 0; i < dim; i++) {
dist += Math.pow(Math.abs(this.components.getValue(i) - p.components.getValue(i)), h);
}
dist = (float)Math.pow(dist, 1/h);
dist = (float) Math.pow(dist, 1f/h);
return dist;
}
}
Expand Down
52 changes: 39 additions & 13 deletions k-means/src/test/java/it/unipi/hadoop/PointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,48 +37,74 @@ protected void tearDown() throws Exception {

public void testManhattan1() {
float distance = p1.distance(p2, 1);
System.out.println("Mario Distance: " + distance);
assertTrue(true);
System.out.println("Manhattan Test 1 Distance: " + distance);
assertTrue(Math.abs(distance - 2.0) < 0.0001f);
}

public void testManhattan2() {
float distance = t1.distance(t2, 1);
System.out.println("Distance: " + distance);
assertTrue(distance - 0 < 0.0001f );
System.out.println("Manhattan Test 2 Distance: " + distance);
assertTrue(Math.abs(distance - 5.0) < 0.0001f);
}

public void testManhattan3() {
float distance = z1.distance(z2, 1);
System.out.println("Distance: " + distance);
assertTrue(distance - 0 < 0.0001f );
System.out.println("Manhattan Test 3 Distance: " + distance);
assertTrue(Math.abs(distance - 19.0) < 0.0001f );
}

public void testEuclidean1() {

float distance = p1.distance(p2, 2);
System.out.println("Euclidean Test 1 Distance: " + distance);
assertTrue(Math.abs(distance - 1.4142) < 0.0001f );
}

public void testEuclidean2() {

float distance = t1.distance(t2, 2);
System.out.println("Euclidean Test 2 Distance: " + distance);
assertTrue(Math.abs(distance - 3.3166) < 0.0001f );
}

public void testMinkowsky1() {
public void testEuclidean3() {
float distance = z1.distance(z2, 2);
System.out.println("Euclidean Test 3 Distance: " + distance);
assertTrue(Math.abs(distance - 7.9372) < 0.0001f);
}

public void testMinkowsky1() {
float distance = p1.distance(p2, 5);
System.out.println("Minkowsky Test 1 Distance: " + distance);
assertTrue(Math.abs(distance - 1.1486) < 0.0001f );
}

public void testMinkowsky2() {

float distance = t1.distance(t2, 5);
System.out.println("Minkowsky Test 2 Distance: " + distance);
assertTrue(Math.abs(distance - 3.0049) < 0.0001f );
}

public void testMinkowsky3() {

float distance = z1.distance(z2, 5);
System.out.println("Minkowsky Test 3 Distance: " + distance);
assertTrue(Math.abs(distance - 5.2788) < 0.0001f);
}

public void testInfinity1() {

float distance = p1.distance(p2, Integer.MAX_VALUE);
System.out.println("Infinity Test 1 Distance: " + distance);
assertTrue(Math.abs(distance - 1.0) < 0.0001f);
}

public void testInfinity() {
public void testInfinity2() {
float distance = t1.distance(t2, Integer.MAX_VALUE);
System.out.println("Infinity Test 2 Distance: " + distance);
assertTrue(Math.abs(distance - 3.0) < 0.0001f);
}

public void testInfinity3() {
float distance = z1.distance(z2, Integer.MAX_VALUE);
System.out.println("Infinity Test 3 Distance: " + distance);
assertTrue(Math.abs(distance - 5.0) < 0.0001f);
}

}
Binary file modified k-means/target/classes/it/unipi/hadoop/model/Point.class
Binary file not shown.
Binary file not shown.
Binary file modified k-means/target/test-classes/it/unipi/hadoop/PointTest.class
Binary file not shown.
7 changes: 5 additions & 2 deletions scripts/distancecalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

#[(1, 2, 3.3, -4, 5.6, 6.88, 7)]

XA = [(1, 2, 3, -4, 5, 6, 7)]
XB = [(2, 1, 0, 1, 2, 3, 4)]
# XA = [(1, 2, 3, -4, 5, 6, 7)]
# XB = [(2, 1, 0, 1, 2, 3, 4)]

XA = [(1, 2)]
XB = [(2, 1)]


eu = distance.cdist(XA, XB, 'euclidean')
Expand Down

0 comments on commit 93e44b3

Please sign in to comment.