-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClusterCenter.java
148 lines (122 loc) · 3.35 KB
/
ClusterCenter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*=============================================================================
| Assignment: Final Project - Multiple Document Summarization
| Author: Group7 - (Sampath, Ajay, Visesh)
| Grader: Walid Shalaby
|
| Course: ITCS 6190
| Instructor: Srinivas Akella
|
| Language: Java
| Version : 1.8.0_101
|
| Deficiencies: No logical errors.
*===========================================================================*/
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.WritableComparable;
/*
* Model to represent center of the KMeans cluster
* */
public final class ClusterCenter implements WritableComparable<ClusterCenter> {
private DoubleVector center;
private int kTimesIncremented = 1;
private int clusterIndex;
public ClusterCenter() {
super();
}
public ClusterCenter(DoubleVector center) {
super();
this.center = center.deepCopy();
}
public ClusterCenter(ClusterCenter center) {
super();
this.center = center.center.deepCopy();
this.kTimesIncremented = center.kTimesIncremented;
}
public ClusterCenter(VectorWritable center) {
super();
this.center = center.getVector().deepCopy();
}
public final void plus(VectorWritable c) {
plus(c.getVector());
}
public final void plus(DoubleVector c) {
center = center.add(c);
kTimesIncremented++;
}
public final void plus(ClusterCenter c) {
kTimesIncremented += c.kTimesIncremented;
center = center.add(c.getCenterVector());
}
public final void divideByK() {
center = center.divide(kTimesIncremented);
}
public final boolean converged(ClusterCenter c) {
return calculateError(c.getCenterVector()) > 0;
}
public final boolean converged(ClusterCenter c, double error) {
return calculateError(c.getCenterVector()) > error;
}
public final double calculateError(DoubleVector v) {
return Math.sqrt(center.subtract(v).abs().sum());
}
@Override
public final void write(DataOutput out) throws IOException {
VectorWritable.writeVector(center, out);
out.writeInt(kTimesIncremented);
out.writeInt(clusterIndex);
}
@Override
public final void readFields(DataInput in) throws IOException {
this.center = VectorWritable.readVector(in);
kTimesIncremented = in.readInt();
clusterIndex = in.readInt();
}
@Override
public final int compareTo(ClusterCenter o) {
return Integer.compare(clusterIndex, o.clusterIndex);
}
/**
* @return the center
*/
public final DoubleVector getCenterVector() {
return center;
}
/**
* @return the index of the cluster in a datastructure.
*/
public int getClusterIndex() {
return clusterIndex;
}
public void setClusterIndex(int clusterIndex) {
this.clusterIndex = clusterIndex;
}
@Override
public final int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((center == null) ? 0 : center.hashCode());
return result;
}
@Override
public final boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ClusterCenter other = (ClusterCenter) obj;
if (center == null) {
if (other.center != null)
return false;
} else if (!center.equals(other.center))
return false;
return true;
}
@Override
public final String toString() {
return "ClusterCenter [center=" + center + "]";
}
}