Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump ndarray to 0.15.0 #156

Merged
merged 9 commits into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ num-traits = "0.2"
rand = { version = "0.8", features = ["small_rng"] }
approx = "0.4"

ndarray = { version = "0.14", default-features = false, features = ["approx"] }
ndarray-linalg = { version = "0.13", optional = true }
ndarray = { version = "0.15", default-features = false, features = ["approx"] }
ndarray-linalg = { version = "0.14", optional = true }

thiserror = "=1.0.25"

Expand Down Expand Up @@ -69,7 +69,7 @@ default-features = false
features = ["cblas"]

[dev-dependencies]
ndarray-rand = "0.13"
ndarray-rand = "0.14"
linfa-datasets = { path = "datasets", features = ["winequality", "iris", "diabetes"] }

[workspace]
Expand Down
4 changes: 2 additions & 2 deletions algorithms/linfa-bayes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ keywords = ["factorization", "machine-learning", "linfa", "unsupervised"]
categories = ["algorithms", "mathematics", "science"]

[dependencies]
ndarray = { version = "0.14" , features = ["blas", "approx"]}
ndarray-stats = "0.4"
ndarray = { version = "0.15" , features = ["blas", "approx"]}
ndarray-stats = "0.5"
thiserror = "=1.0.25"

linfa = { version = "0.4.0", path = "../.." }
Expand Down
12 changes: 6 additions & 6 deletions algorithms/linfa-clustering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ features = ["std", "derive"]

[dependencies]
hnsw = "0.8"
ndarray = { version = "0.14", features = ["rayon", "approx"]}
ndarray-linalg = "0.13"
ndarray-rand = "0.13"
ndarray-stats = "0.4"
ndarray = { version = "0.15", features = ["rayon", "approx"]}
ndarray-linalg = "0.14"
ndarray-rand = "0.14"
ndarray-stats = "0.5"
num-traits = "0.2"
rand_isaac = "0.3"
space = "0.12"
Expand All @@ -44,11 +44,11 @@ rand_pcg = "0.3.1"
noisy_float = "0.2.0"

[dev-dependencies]
ndarray-npy = { version = "0.7", default-features = false }
ndarray-npy = { version = "0.8", default-features = false }
criterion = "0.3.5"
serde_json = "1"
approx = "0.4"
lax = "0.1.0"
lax = "0.2.0"

[[bench]]
name = "k_means"
Expand Down
10 changes: 5 additions & 5 deletions algorithms/linfa-clustering/src/gaussian_mixture/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ impl<F: Float> GaussianMixtureModel<F> {
// det(precision_chol) is half of det(precision)
let log_det = Self::compute_log_det_cholesky_full(&self.precisions_chol, n_features);
let mut log_prob: Array2<F> = Array::zeros((n_samples, n_clusters));
Zip::indexed(means.genrows())
Zip::indexed(means.rows())
.and(self.precisions_chol.outer_iter())
.apply(|k, mu, prec_chol| {
.for_each(|k, mu, prec_chol| {
let diff = (&observations.to_owned() - &mu).dot(&prec_chol);
log_prob
.slice_mut(s![.., k])
Expand Down Expand Up @@ -527,7 +527,7 @@ mod tests {

let n = 500;
let mut observations = Array2::zeros((2 * n, means.ncols()));
for (i, mut row) in observations.genrows_mut().into_iter().enumerate() {
for (i, mut row) in observations.rows_mut().into_iter().enumerate() {
let sample = if i < n {
mvn1.sample(&mut rng)
} else {
Expand Down Expand Up @@ -560,7 +560,7 @@ mod tests {

fn function_test_1d(x: &Array2<f64>) -> Array2<f64> {
let mut y = Array2::zeros(x.dim());
Zip::from(&mut y).and(x).apply(|yi, &xi| {
Zip::from(&mut y).and(x).for_each(|yi, &xi| {
if xi < 0.4 {
*yi = xi * xi;
} else if (0.4..0.8).contains(&xi) {
Expand Down Expand Up @@ -650,7 +650,7 @@ mod tests {
let closest_c = gmm_centroids.index_axis(Axis(0), memberships[i]);
Zip::from(&closest_c)
.and(&expected_c)
.apply(|a, b| assert_abs_diff_eq!(a, b, epsilon = 1.))
.for_each(|a, b| assert_abs_diff_eq!(a, b, epsilon = 1.))
}
}

Expand Down
24 changes: 12 additions & 12 deletions algorithms/linfa-clustering/src/k_means/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,19 +483,19 @@ fn compute_centroids<F: Float>(
let mut counts: Array1<usize> = Array1::ones(n_clusters);
let mut centroids = Array2::zeros((n_clusters, observations.ncols()));

Zip::from(observations.genrows())
Zip::from(observations.rows())
.and(cluster_memberships)
.apply(|observation, &cluster_membership| {
.for_each(|observation, &cluster_membership| {
let mut centroid = centroids.row_mut(cluster_membership);
centroid += &observation;
counts[cluster_membership] += 1;
});
// m_k-means: Treat the old centroid like another point in the cluster
centroids += old_centroids;

Zip::from(centroids.genrows_mut())
Zip::from(centroids.rows_mut())
.and(&counts)
.apply(|mut centroid, &cnt| centroid /= F::cast(cnt));
.for_each(|mut centroid, &cnt| centroid /= F::cast(cnt));
centroids
}

Expand All @@ -510,9 +510,9 @@ fn compute_centroids_incremental<F: Float>(
) -> Array2<F> {
let mut centroids = old_centroids.to_owned();
// We can parallelize this
Zip::from(observations.genrows())
Zip::from(observations.rows())
.and(cluster_memberships)
.apply(|obs, &c| {
.for_each(|obs, &c| {
// Computes centroids[c] += (observation - centroids[c]) / counts[c]
// If cluster is empty for this batch, then this wouldn't even be called, so no
// chance of getting NaN.
Expand All @@ -533,7 +533,7 @@ pub(crate) fn update_cluster_memberships<F: Float, D: Distance<F>>(
) {
Zip::from(observations.axis_iter(Axis(0)))
.and(cluster_memberships)
.par_apply(|observation, cluster_membership| {
.par_for_each(|observation, cluster_membership| {
*cluster_membership = closest_centroid(dist_fn, centroids, &observation).0
});
}
Expand All @@ -547,7 +547,7 @@ pub(crate) fn update_min_dists<F: Float, D: Distance<F>>(
) {
Zip::from(observations.axis_iter(Axis(0)))
.and(dists)
.par_apply(|observation, dist| {
.par_for_each(|observation, dist| {
*dist = closest_centroid(dist_fn, centroids, &observation).1
});
}
Expand All @@ -563,7 +563,7 @@ pub(crate) fn update_memberships_and_dists<F: Float, D: Distance<F>>(
Zip::from(observations.axis_iter(Axis(0)))
.and(cluster_memberships)
.and(dists)
.par_apply(|observation, cluster_membership, dist| {
.par_for_each(|observation, cluster_membership, dist| {
let (m, d) = closest_centroid(dist_fn, centroids, &observation);
*cluster_membership = m;
*dist = d;
Expand All @@ -579,7 +579,7 @@ pub(crate) fn closest_centroid<F: Float, D: Distance<F>>(
// (n_features)
observation: &ArrayBase<impl Data<Elem = F>, Ix1>,
) -> (usize, F) {
let iterator = centroids.genrows().into_iter();
let iterator = centroids.rows().into_iter();

let first_centroid = centroids.row(0);
let (mut closest_index, mut minimum_distance) = (
Expand Down Expand Up @@ -610,7 +610,7 @@ mod tests {

fn function_test_1d(x: &Array2<f64>) -> Array2<f64> {
let mut y = Array2::zeros(x.dim());
Zip::from(&mut y).and(x).apply(|yi, &xi| {
Zip::from(&mut y).and(x).for_each(|yi, &xi| {
if xi < 0.4 {
*yi = xi * xi;
} else if (0.4..0.8).contains(&xi) {
Expand All @@ -624,7 +624,7 @@ mod tests {

macro_rules! calc_inertia {
($dist:expr, $centroids:expr, $obs:expr, $memberships:expr) => {
$obs.genrows()
$obs.rows()
.into_iter()
.zip($memberships.iter())
.map(|(row, &c)| $dist.rdistance(row.view(), $centroids.row(c).view()))
Expand Down
4 changes: 2 additions & 2 deletions algorithms/linfa-clustering/src/k_means/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,15 @@ mod tests {
// Look for the right number of centroids
let out = init.run(&dist_fn, centroids.len(), obs.view(), &mut rng);
let mut cluster_ids = HashSet::new();
for row in out.genrows() {
for row in out.rows() {
// Centroid should not be 0
assert_abs_diff_ne!(row, Array1::zeros(row.len()), epsilon = 1e-1);
// Find the resultant centroid in 1 of the 3 clusters
let found = clusters
.iter()
.enumerate()
.find_map(|(i, c)| {
if c.genrows().into_iter().any(|cl| abs_diff_eq!(row, cl)) {
if c.rows().into_iter().any(|cl| abs_diff_eq!(row, cl)) {
Some(i)
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion algorithms/linfa-clustering/src/optics/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ fn find_neighbors<F: Float>(
let mut hnsw: Hnsw<Euclidean<F>, Pcg64, 12, 24> = Hnsw::new_params(params);

// insert all rows as data points into HNSW graph
for feature in observations.genrows().into_iter() {
for feature in observations.rows().into_iter() {
hnsw.insert(Euclidean(feature), &mut searcher);
}
let mut neighbours = vec![space::Neighbor::invalid(); observations.nrows()];
Expand Down
2 changes: 1 addition & 1 deletion algorithms/linfa-clustering/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn generate_blobs(
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() {
for (blob_index, blob_centroid) in blob_centroids.rows().into_iter().enumerate() {
let blob = generate_blob(blob_size, &blob_centroid, rng);

let indexes = s![blob_index * blob_size..(blob_index + 1) * blob_size, ..];
Expand Down
6 changes: 3 additions & 3 deletions algorithms/linfa-elasticnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ default-features = false
features = ["std", "derive"]

[dependencies]
ndarray = { version = "0.14", features = ["blas", "approx"]}
ndarray-linalg = "0.13"
ndarray = { version = "0.15", features = ["blas", "approx"]}
ndarray-linalg = "0.14"

num-traits = "0.2"
approx = "0.4"
Expand All @@ -39,5 +39,5 @@ linfa = { version = "0.4.0", path = "../.." }

[dev-dependencies]
linfa-datasets = { version = "0.4.0", path = "../../datasets", features = ["diabetes"] }
ndarray-rand = "0.13"
ndarray-rand = "0.14"
rand_isaac = "0.3"
4 changes: 2 additions & 2 deletions algorithms/linfa-hierarchical/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ keywords = ["hierachical", "agglomerative", "clustering", "machine-learning", "l
categories = ["algorithms", "mathematics", "science"]

[dependencies]
ndarray = { version = "0.14", default-features = false }
ndarray = { version = "0.15", default-features = false }
kodama = "0.2"

linfa = { version = "0.4.0", path = "../.." }
linfa-kernel = { version = "0.4.0", path = "../linfa-kernel" }

[dev-dependencies]
rand = "0.8"
ndarray-rand = "0.13"
ndarray-rand = "0.14"
linfa-datasets = { version = "0.4.0", path = "../../datasets", features = ["iris"] }
10 changes: 5 additions & 5 deletions algorithms/linfa-ica/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ default-features = false
features = ["std", "derive"]

[dependencies]
ndarray = { version = "0.14", default-features = false }
ndarray-linalg = "0.13"
ndarray-rand = "0.13"
ndarray-stats = "0.4"
ndarray = { version = "0.15", default-features = false }
ndarray-linalg = "0.14"
ndarray-rand = "0.14"
ndarray-stats = "0.5"
num-traits = "0.2"
rand_isaac = "0.3"
thiserror = "=1.0.25"

linfa = { version = "0.4.0", path = "../..", features = ["ndarray-linalg"] }

[dev-dependencies]
ndarray-npy = { version = "0.7", default-features = false }
ndarray-npy = { version = "0.8", default-features = false }
paste = "1.0"
6 changes: 3 additions & 3 deletions algorithms/linfa-kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ default-features = false
features = ["std", "derive"]

[dependencies]
ndarray = "0.14"
ndarray = "0.15"
num-traits = "0.2"
sprs = { version="0.9.4", default-features = false }
hnsw = "0.6"
sprs = { version="0.11.0", default-features = false }
hnsw = "0.6.1"
space = "0.10"

linfa = { version = "0.4.0", path = "../.." }
9 changes: 5 additions & 4 deletions algorithms/linfa-kernel/src/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn adjacency_matrix<F: Float, D: Data<Elem = F>>(
let mut hnsw: HNSW<Euclidean<F>> = HNSW::new_params(params);

// insert all rows as data points into HNSW graph
for feature in dataset.genrows().into_iter() {
for feature in dataset.rows().into_iter() {
hnsw.insert(Euclidean(feature), &mut searcher);
}

Expand All @@ -57,7 +57,7 @@ pub fn adjacency_matrix<F: Float, D: Data<Elem = F>>(

// find neighbours for each data point
let mut added = 0;
for (m, feature) in dataset.genrows().into_iter().enumerate() {
for (m, feature) in dataset.rows().into_iter().enumerate() {
hnsw.nearest(&Euclidean(feature), 3 * k, &mut searcher, &mut neighbours);

//dbg!(&neighbours);
Expand All @@ -82,8 +82,9 @@ pub fn adjacency_matrix<F: Float, D: Data<Elem = F>>(
}

// create CSR matrix from data, indptr and indices
let mat = CsMatBase::new((n_points, n_points), indptr, indices, data);
let mut mat = &mat + &mat.transpose_view();
let mat = CsMatBase::new_from_unsorted((n_points, n_points), indptr, indices, data).unwrap();
let transpose = mat.transpose_view().to_other_storage();
let mut mat = sprs::binop::csmat_binop(mat.view(), transpose.view(), |x, y| x.add(*y));

// ensure that all values are one
let val: F = F::one();
Expand Down
8 changes: 4 additions & 4 deletions algorithms/linfa-linear/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ keywords = ["machine-learning", "linfa", "ai", "ml", "linear"]
categories = ["algorithms", "mathematics", "science"]

[dependencies]
ndarray = { version = "0.14", features = ["blas", "approx"] }
ndarray-linalg = "0.13"
ndarray-stats = "0.4"
ndarray = { version = "0.15", features = ["blas", "approx"] }
ndarray-linalg = "0.14"
ndarray-stats = "0.5"
num-traits = "0.2"
argmin = { version = "0.4", features = ["ndarrayl"] }
argmin = { version = "0.4.6", features = ["ndarrayl"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
thiserror = "=1.0.25"

Expand Down
2 changes: 1 addition & 1 deletion algorithms/linfa-linear/src/glm/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl TweedieDistribution {
// 2 * (y * log(y / ypred) - y + ypred)
power if (power - 1.).abs() < 1e-6 => {
let mut div = &y / &ypred;
Zip::from(&mut div).and(y).apply(|y, &x| {
Zip::from(&mut div).and(y).for_each(|y, &x| {
if x == F::zero() {
*y = F::zero();
} else {
Expand Down
6 changes: 3 additions & 3 deletions algorithms/linfa-logistic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ keywords = ["machine-learning", "linfa", "ai", "ml", "linear"]
categories = ["algorithms", "mathematics", "science"]

[dependencies]
ndarray = { version = "0.14", features = ["approx", "blas"] }
ndarray-linalg = "0.13"
ndarray = { version = "0.15", features = ["approx", "blas"] }
ndarray-linalg = "0.14"
num-traits = "0.2"
argmin = { version = "0.4", features = ["ndarrayl"] }
argmin = { version = "0.4.6", features = ["ndarrayl"] }
serde = "1.0"
thiserror = "=1.0.25"

Expand Down
6 changes: 3 additions & 3 deletions algorithms/linfa-nn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ default-features = false
features = ["std", "derive"]

[dependencies]
ndarray = { version = "0.14", features = ["approx"]}
ndarray-stats = "0.4"
ndarray = { version = "0.15", features = ["approx"]}
ndarray-stats = "0.5"
num-traits = "0.2.0"
noisy_float = "0.2.0"
order-stat = "0.1.3"
Expand All @@ -39,7 +39,7 @@ linfa = { version = "0.4.0", path = "../.." }
approx = "0.4"
criterion = "0.3.5"
rand_isaac = "0.3"
ndarray-rand = "0.13"
ndarray-rand = "0.14"

[[bench]]
name = "nn"
Expand Down
Loading