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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use learning::{LearningResult, UnSupModel};
use learning::error::{Error, ErrorKind};
use linalg::{Matrix, Vector, BaseMatrix};
use rulinalg::utils;
use rulinalg::matrix::Row;
#[derive(Debug)]
pub struct DBSCAN {
eps: f64,
min_points: usize,
clusters: Option<Vector<Option<usize>>>,
predictive: bool,
_visited: Vec<bool>,
_cluster_data: Option<Matrix<f64>>,
}
impl Default for DBSCAN {
fn default() -> DBSCAN {
DBSCAN {
eps: 0.5,
min_points: 5,
clusters: None,
predictive: false,
_visited: Vec::new(),
_cluster_data: None,
}
}
}
impl UnSupModel<Matrix<f64>, Vector<Option<usize>>> for DBSCAN {
fn train(&mut self, inputs: &Matrix<f64>) -> LearningResult<()> {
self.init_params(inputs.rows());
let mut cluster = 0;
for (idx, point) in inputs.row_iter().enumerate() {
let visited = self._visited[idx];
if !visited {
self._visited[idx] = true;
let neighbours = self.region_query(point, inputs);
if neighbours.len() >= self.min_points {
self.expand_cluster(inputs, idx, neighbours, cluster);
cluster += 1;
}
}
}
if self.predictive {
self._cluster_data = Some(inputs.clone());
}
Ok(())
}
fn predict(&self, inputs: &Matrix<f64>) -> LearningResult<Vector<Option<usize>>> {
if self.predictive {
if let (&Some(ref cluster_data), &Some(ref clusters)) = (&self._cluster_data,
&self.clusters) {
let mut classes = Vec::with_capacity(inputs.rows());
for input_point in inputs.row_iter() {
let mut distances = Vec::with_capacity(cluster_data.rows());
for cluster_point in cluster_data.row_iter() {
let point_distance =
utils::vec_bin_op(input_point.raw_slice(), cluster_point.raw_slice(), |x, y| x - y);
distances.push(utils::dot(&point_distance, &point_distance).sqrt());
}
let (closest_idx, closest_dist) = utils::argmin(&distances);
if closest_dist < self.eps {
classes.push(clusters[closest_idx]);
} else {
classes.push(None);
}
}
Ok(Vector::new(classes))
} else {
Err(Error::new_untrained())
}
} else {
Err(Error::new(ErrorKind::InvalidState,
"Model must be set to predictive. Use `self.set_predictive(true)`."))
}
}
}
impl DBSCAN {
pub fn new(eps: f64, min_points: usize) -> DBSCAN {
assert!(eps > 0f64, "The model epsilon must be positive.");
DBSCAN {
eps: eps,
min_points: min_points,
clusters: None,
predictive: false,
_visited: Vec::new(),
_cluster_data: None,
}
}
pub fn set_predictive(&mut self, predictive: bool) {
self.predictive = predictive;
}
pub fn clusters(&self) -> Option<&Vector<Option<usize>>> {
self.clusters.as_ref()
}
fn expand_cluster(&mut self,
inputs: &Matrix<f64>,
point_idx: usize,
neighbour_pts: Vec<usize>,
cluster: usize) {
debug_assert!(point_idx < inputs.rows(),
"Point index too large for inputs");
debug_assert!(neighbour_pts.iter().all(|x| *x < inputs.rows()),
"Neighbour indices too large for inputs");
self.clusters.as_mut().map(|x| x.mut_data()[point_idx] = Some(cluster));
for data_point_idx in &neighbour_pts {
let visited = self._visited[*data_point_idx];
if !visited {
self._visited[*data_point_idx] = true;
let data_point_row = unsafe { inputs.row_unchecked(*data_point_idx) };
let sub_neighbours = self.region_query(data_point_row, inputs);
if sub_neighbours.len() >= self.min_points {
self.expand_cluster(inputs, *data_point_idx, sub_neighbours, cluster);
}
}
}
}
fn region_query(&self, point: Row<f64>, inputs: &Matrix<f64>) -> Vec<usize> {
debug_assert!(point.cols() == inputs.cols(),
"point must be of same dimension as inputs");
let mut in_neighbourhood = Vec::new();
for (idx, data_point) in inputs.row_iter().enumerate() {
let point_distance = utils::vec_bin_op(data_point.raw_slice(), point.raw_slice(), |x, y| x - y);
let dist = utils::dot(&point_distance, &point_distance).sqrt();
if dist < self.eps {
in_neighbourhood.push(idx);
}
}
in_neighbourhood
}
fn init_params(&mut self, total_points: usize) {
unsafe {
self._visited.reserve(total_points);
self._visited.set_len(total_points);
}
for i in 0..total_points {
self._visited[i] = false;
}
self.clusters = Some(Vector::new(vec![None; total_points]));
}
}
#[cfg(test)]
mod tests {
use super::DBSCAN;
use linalg::{Matrix, BaseMatrix};
#[test]
fn test_region_query() {
let model = DBSCAN::new(1.0, 3);
let inputs = Matrix::new(3, 2, vec![1.0, 1.0, 1.1, 1.9, 3.0, 3.0]);
let m = matrix![1.0, 1.0];
let row = m.row(0);
let neighbours = model.region_query(row, &inputs);
assert!(neighbours.len() == 2);
}
#[test]
fn test_region_query_small_eps() {
let model = DBSCAN::new(0.01, 3);
let inputs = Matrix::new(3, 2, vec![1.0, 1.0, 1.1, 1.9, 1.1, 1.1]);
let m = matrix![1.0, 1.0];
let row = m.row(0);
let neighbours = model.region_query(row, &inputs);
assert!(neighbours.len() == 1);
}
}