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
use ndarray::{s, Array, Array2, ArrayBase, Data, Ix1, Ix2};
use ndarray_rand::rand::Rng;
use ndarray_rand::rand_distr::StandardNormal;
use ndarray_rand::RandomExt;
pub fn generate_blobs(
blob_size: usize,
blob_centroids: &ArrayBase<impl Data<Elem = f64>, Ix2>,
rng: &mut impl Rng,
) -> Array2<f64> {
let (n_centroids, n_features) = blob_centroids.dim();
let mut blobs: Array2<f64> = Array2::zeros((n_centroids * blob_size, n_features));
for (blob_index, blob_centroid) in blob_centroids.genrows().into_iter().enumerate() {
let blob = generate_blob(blob_size, &blob_centroid, rng);
let indexes = s![blob_index * blob_size..(blob_index + 1) * blob_size, ..];
blobs.slice_mut(indexes).assign(&blob);
}
blobs
}
pub fn generate_blob(
blob_size: usize,
blob_centroid: &ArrayBase<impl Data<Elem = f64>, Ix1>,
rng: &mut impl Rng,
) -> Array2<f64> {
let shape = (blob_size, blob_centroid.len());
let origin_blob: Array2<f64> = Array::random_using(shape, StandardNormal, rng);
origin_blob + blob_centroid
}