Compare commits
76 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
62de25b2ae | ||
|
|
7d87451333 | ||
|
|
265fd558e7 | ||
|
|
e25e2aea2b | ||
|
|
2f6dd1325e | ||
|
|
b0dece9476 | ||
|
|
c507d976be | ||
|
|
fa54d5ee86 | ||
|
|
459d558d48 | ||
|
|
1b7dda30a2 | ||
|
|
c1bd1df5f6 | ||
|
|
cf751f05aa | ||
|
|
63ed89aadd | ||
|
|
890e9d644c | ||
|
|
af0a740394 | ||
|
|
616e38c282 | ||
|
|
a449fdd4ea | ||
|
|
669f87f812 | ||
|
|
6d529b34d2 | ||
|
|
3ec9e4f0db | ||
|
|
527477dea7 | ||
|
|
5b517c5048 | ||
|
|
2df0795be9 | ||
|
|
0dc97a4e9b | ||
|
|
6c0fd37222 | ||
|
|
d8d0fb6903 | ||
|
|
8d07efd921 | ||
|
|
ba27dd2a55 | ||
|
|
ed9769f651 | ||
|
|
b427e5d8b1 | ||
|
|
fabe362755 | ||
|
|
ee6b6a53d6 | ||
|
|
19f3a2fcc0 | ||
|
|
e09c4ba724 | ||
|
|
6624732a65 | ||
|
|
1cbde3ba22 | ||
|
|
551a6e34a5 | ||
|
|
c45bab491a | ||
|
|
7f35dc54e4 | ||
|
|
8f1a7dfd79 | ||
|
|
712c478af6 | ||
|
|
4d36b7f34f | ||
|
|
a16927aa16 | ||
|
|
d91f4f7ce4 | ||
|
|
a7fa0585eb | ||
|
|
a32eb66a6a | ||
|
|
f605f6e075 | ||
|
|
3b1aaaadf7 | ||
|
|
d015b12402 | ||
|
|
d5200074c2 | ||
|
|
473cdfc44d | ||
|
|
ad2e6c2900 | ||
|
|
9ea3133c27 | ||
|
|
e4c47c7540 | ||
|
|
f4fd4d2239 | ||
|
|
05dfffad5c | ||
|
|
a37b552a7d | ||
|
|
55e1158581 | ||
|
|
cfa824d7db | ||
|
|
bb5b437a32 | ||
|
|
851533dfa7 | ||
|
|
0d996edafe | ||
|
|
f291b71f4a | ||
|
|
2d75c2c405 | ||
|
|
1f2597be74 | ||
|
|
0f442e96c0 | ||
|
|
44e4be23a6 | ||
|
|
01f753f86d | ||
|
|
df766eaf79 | ||
|
|
09d9205696 | ||
|
|
dc7f01db4a | ||
|
|
eb4b49d552 | ||
|
|
98e3465e7b | ||
|
|
ea39024fd2 | ||
|
|
4e94feb872 | ||
|
|
fa802d2d3f |
@@ -37,8 +37,6 @@ $ rust-code-analysis-cli -p src/algorithm/neighbour/fastpair.rs --ls 22 --le 213
|
||||
```
|
||||
* find more information about what happens in your binary with [`twiggy`](https://rustwasm.github.io/twiggy/install.html). This need a compiled binary so create a brief `main {}` function using `smartcore` and then point `twiggy` to that file.
|
||||
|
||||
* Please take a look to the output of a profiler to spot most evident performance problems, see [this guide about using a profiler](http://www.codeofview.com/fix-rs/2017/01/24/how-to-optimize-rust-programs-on-linux/).
|
||||
|
||||
## Issue Report Process
|
||||
|
||||
1. Go to the project's issues.
|
||||
|
||||
@@ -41,4 +41,4 @@ jobs:
|
||||
- name: Upload to codecov.io
|
||||
uses: codecov/codecov-action@v2
|
||||
with:
|
||||
fail_ci_if_error: false
|
||||
fail_ci_if_error: true
|
||||
|
||||
+3
-3
@@ -2,7 +2,7 @@
|
||||
name = "smartcore"
|
||||
description = "Machine Learning in Rust."
|
||||
homepage = "https://smartcorelib.org"
|
||||
version = "0.3.2"
|
||||
version = "0.3.0"
|
||||
authors = ["smartcore Developers"]
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
@@ -42,13 +42,13 @@ std_rand = ["rand/std_rng", "rand/std"]
|
||||
js = ["getrandom/js"]
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
getrandom = { version = "0.2.8", optional = true }
|
||||
getrandom = { version = "*", optional = true }
|
||||
|
||||
[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dev-dependencies]
|
||||
wasm-bindgen-test = "0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
itertools = "0.10.5"
|
||||
itertools = "*"
|
||||
serde_json = "1.0"
|
||||
bincode = "1.3.1"
|
||||
|
||||
|
||||
@@ -18,4 +18,4 @@
|
||||
-----
|
||||
[](https://github.com/smartcorelib/smartcore/actions/workflows/ci.yml)
|
||||
|
||||
To start getting familiar with the new smartcore v0.3 API, there is now available a [**Jupyter Notebook environment repository**](https://github.com/smartcorelib/smartcore-jupyter). Please see instructions there, contributions welcome see [CONTRIBUTING](.github/CONTRIBUTING.md).
|
||||
To start getting familiar with the new smartcore v0.5 API, there is now available a [**Jupyter Notebook environment repository**](https://github.com/smartcorelib/smartcore-jupyter). Please see instructions there, contributions welcome see [CONTRIBUTING](.github/CONTRIBUTING.md).
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="RUST_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/benches" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -179,21 +179,6 @@ impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> FastPair<'a, T, M> {
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Return order dissimilarities from closest to furthest
|
||||
///
|
||||
#[allow(dead_code)]
|
||||
pub fn ordered_pairs(&self) -> std::vec::IntoIter<&PairwiseDistance<T>> {
|
||||
// improvement: implement this to return `impl Iterator<Item = &PairwiseDistance<T>>`
|
||||
// need to implement trait `Iterator` for `Vec<&PairwiseDistance<T>>`
|
||||
let mut distances = self
|
||||
.distances
|
||||
.values()
|
||||
.collect::<Vec<&PairwiseDistance<T>>>();
|
||||
distances.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||
distances.into_iter()
|
||||
}
|
||||
|
||||
//
|
||||
// Compute distances from input to all other points in data-structure.
|
||||
// input is the row index of the sample matrix
|
||||
@@ -275,8 +260,8 @@ mod tests_fastpair {
|
||||
let distances = fastpair.distances;
|
||||
let neighbours = fastpair.neighbours;
|
||||
|
||||
assert!(!distances.is_empty());
|
||||
assert!(!neighbours.is_empty());
|
||||
assert!(distances.len() != 0);
|
||||
assert!(neighbours.len() != 0);
|
||||
|
||||
assert_eq!(10, neighbours.len());
|
||||
assert_eq!(10, distances.len());
|
||||
@@ -291,13 +276,17 @@ mod tests_fastpair {
|
||||
// We expect an error when we run `FastPair` on this dataset,
|
||||
// becuase `FastPair` currently only works on a minimum of 3
|
||||
// points.
|
||||
let fastpair = FastPair::new(&dataset);
|
||||
assert!(fastpair.is_err());
|
||||
let _fastpair = FastPair::new(&dataset);
|
||||
|
||||
if let Err(e) = fastpair {
|
||||
let expected_error =
|
||||
Failed::because(FailedError::FindFailed, "min number of rows should be 3");
|
||||
assert_eq!(e, expected_error)
|
||||
match _fastpair {
|
||||
Err(e) => {
|
||||
let expected_error =
|
||||
Failed::because(FailedError::FindFailed, "min number of rows should be 3");
|
||||
assert_eq!(e, expected_error)
|
||||
}
|
||||
_ => {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -593,7 +582,7 @@ mod tests_fastpair {
|
||||
};
|
||||
for p in dissimilarities.iter() {
|
||||
if p.distance.unwrap() < min_dissimilarity.distance.unwrap() {
|
||||
min_dissimilarity = *p
|
||||
min_dissimilarity = p.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -605,39 +594,4 @@ mod tests_fastpair {
|
||||
|
||||
assert_eq!(closest, min_dissimilarity);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fastpair_ordered_pairs() {
|
||||
let x = DenseMatrix::<f64>::from_2d_array(&[
|
||||
&[5.1, 3.5, 1.4, 0.2],
|
||||
&[4.9, 3.0, 1.4, 0.2],
|
||||
&[4.7, 3.2, 1.3, 0.2],
|
||||
&[4.6, 3.1, 1.5, 0.2],
|
||||
&[5.0, 3.6, 1.4, 0.2],
|
||||
&[5.4, 3.9, 1.7, 0.4],
|
||||
&[4.9, 3.1, 1.5, 0.1],
|
||||
&[7.0, 3.2, 4.7, 1.4],
|
||||
&[6.4, 3.2, 4.5, 1.5],
|
||||
&[6.9, 3.1, 4.9, 1.5],
|
||||
&[5.5, 2.3, 4.0, 1.3],
|
||||
&[6.5, 2.8, 4.6, 1.5],
|
||||
&[4.6, 3.4, 1.4, 0.3],
|
||||
&[5.0, 3.4, 1.5, 0.2],
|
||||
&[4.4, 2.9, 1.4, 0.2],
|
||||
]);
|
||||
let fastpair = FastPair::new(&x).unwrap();
|
||||
|
||||
let ordered = fastpair.ordered_pairs();
|
||||
|
||||
let mut previous: f64 = -1.0;
|
||||
for p in ordered {
|
||||
if previous == -1.0 {
|
||||
previous = p.distance.unwrap();
|
||||
} else {
|
||||
let current = p.distance.unwrap();
|
||||
assert!(current >= previous);
|
||||
previous = current;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,15 +49,20 @@ pub mod linear_search;
|
||||
/// Both, KNN classifier and regressor benefits from underlying search algorithms that helps to speed up queries.
|
||||
/// `KNNAlgorithmName` maintains a list of supported search algorithms, see [KNN algorithms](../algorithm/neighbour/index.html)
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum KNNAlgorithmName {
|
||||
/// Heap Search algorithm, see [`LinearSearch`](../algorithm/neighbour/linear_search/index.html)
|
||||
LinearSearch,
|
||||
/// Cover Tree Search algorithm, see [`CoverTree`](../algorithm/neighbour/cover_tree/index.html)
|
||||
#[default]
|
||||
CoverTree,
|
||||
}
|
||||
|
||||
impl Default for KNNAlgorithmName {
|
||||
fn default() -> Self {
|
||||
KNNAlgorithmName::CoverTree
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum KNNAlgorithm<T: Number, D: Distance<Vec<T>>> {
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
//!
|
||||
//! Example:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! ```
|
||||
//! use smartcore::linalg::basic::matrix::DenseMatrix;
|
||||
//! use smartcore::linalg::basic::arrays::Array2;
|
||||
//! use smartcore::cluster::dbscan::*;
|
||||
@@ -511,6 +511,6 @@ mod tests {
|
||||
.and_then(|dbscan| dbscan.predict(&x))
|
||||
.unwrap();
|
||||
|
||||
println!("{labels:?}");
|
||||
println!("{:?}", labels);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-179
@@ -62,7 +62,7 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::algorithm::neighbour::bbd_tree::BBDTree;
|
||||
use crate::api::{Predictor, UnsupervisedEstimator};
|
||||
use crate::error::Failed;
|
||||
use crate::linalg::basic::arrays::{Array1, Array2, Array};
|
||||
use crate::linalg::basic::arrays::{Array1, Array2};
|
||||
use crate::metrics::distance::euclidian::*;
|
||||
use crate::numbers::basenum::Number;
|
||||
use crate::rand_custom::get_rng_impl;
|
||||
@@ -322,109 +322,6 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>> KMeans<TX, TY, X, Y>
|
||||
})
|
||||
}
|
||||
|
||||
/// Fit algorithm to _NxM_ matrix where _N_ is number of samples and _M_ is number of features.
|
||||
/// * `data` - training instances to cluster
|
||||
/// * `parameters` - cluster parameters
|
||||
/// * `centroids` - starting centroids
|
||||
pub fn fit_with_centroids(
|
||||
data: &X,
|
||||
parameters: KMeansParameters,
|
||||
centroids: Vec<Vec<f64>>,
|
||||
) -> Result<KMeans<TX, TY, X, Y>, Failed> {
|
||||
|
||||
// TODO: reuse existing methods in `crate::metrics`
|
||||
fn euclidean_distance(point1: &Vec<f64>, point2: &Vec<f64>) -> f64 {
|
||||
let mut dist = 0.0;
|
||||
for i in 0..point1.len() {
|
||||
dist += (point1[i] - point2[i]).powi(2);
|
||||
}
|
||||
dist.sqrt()
|
||||
}
|
||||
|
||||
fn closest_centroid(point: &Vec<f64>, centroids: &Vec<Vec<f64>>) -> usize {
|
||||
let mut closest_idx = 0;
|
||||
let mut closest_dist = std::f64::MAX;
|
||||
for (i, centroid) in centroids.iter().enumerate() {
|
||||
let dist = euclidean_distance(point, centroid);
|
||||
if dist < closest_dist {
|
||||
closest_dist = dist;
|
||||
closest_idx = i;
|
||||
}
|
||||
}
|
||||
closest_idx
|
||||
}
|
||||
|
||||
let bbd = BBDTree::new(data);
|
||||
|
||||
if centroids.len() != parameters.k {
|
||||
return Err(Failed::fit(&format!(
|
||||
"number of centroids ({}) must be equal to k ({})",
|
||||
centroids.len(),
|
||||
parameters.k
|
||||
)));
|
||||
}
|
||||
|
||||
let mut y = vec![0; data.shape().0];
|
||||
for i in 0..data.shape().0 {
|
||||
y[i] = closest_centroid(
|
||||
&Vec::from_iterator(data.get_row(i).iterator(0).map(|e| e.to_f64().unwrap()),
|
||||
data.shape().1), ¢roids
|
||||
);
|
||||
}
|
||||
|
||||
let mut size = vec![0; parameters.k];
|
||||
let mut new_centroids = vec![vec![0f64; data.shape().1]; parameters.k];
|
||||
|
||||
for i in 0..data.shape().0 {
|
||||
size[y[i]] += 1;
|
||||
}
|
||||
|
||||
for i in 0..data.shape().0 {
|
||||
for j in 0..data.shape().1 {
|
||||
new_centroids[y[i]][j] += data.get((i, j)).to_f64().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..parameters.k {
|
||||
for j in 0..data.shape().1 {
|
||||
new_centroids[i][j] /= size[i] as f64;
|
||||
}
|
||||
}
|
||||
|
||||
let mut sums = vec![vec![0f64; data.shape().1]; parameters.k];
|
||||
let mut distortion = std::f64::MAX;
|
||||
|
||||
for _ in 1..=parameters.max_iter {
|
||||
let dist = bbd.clustering(&new_centroids, &mut sums, &mut size, &mut y);
|
||||
for i in 0..parameters.k {
|
||||
if size[i] > 0 {
|
||||
for j in 0..data.shape().1 {
|
||||
new_centroids[i][j] = sums[i][j] / size[i] as f64;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if distortion <= dist {
|
||||
break;
|
||||
} else {
|
||||
distortion = dist;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(KMeans {
|
||||
k: parameters.k,
|
||||
_y: y,
|
||||
size,
|
||||
_distortion: distortion,
|
||||
centroids: new_centroids,
|
||||
_phantom_tx: PhantomData,
|
||||
_phantom_ty: PhantomData,
|
||||
_phantom_x: PhantomData,
|
||||
_phantom_y: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/// Predict clusters for `x`
|
||||
/// * `x` - matrix with new data to transform of size _KxM_ , where _K_ is number of new samples and _M_ is number of features.
|
||||
pub fn predict(&self, x: &X) -> Result<Y, Failed> {
|
||||
@@ -520,7 +417,6 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>> KMeans<TX, TY, X, Y>
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::linalg::basic::matrix::DenseMatrix;
|
||||
use crate::algorithm::neighbour::fastpair;
|
||||
|
||||
#[cfg_attr(
|
||||
all(target_arch = "wasm32", not(target_os = "wasi")),
|
||||
@@ -602,80 +498,8 @@ mod tests {
|
||||
|
||||
let y: Vec<usize> = kmeans.predict(&x).unwrap();
|
||||
|
||||
for (i, _y_i) in y.iter().enumerate() {
|
||||
assert_eq!({ y[i] }, kmeans._y[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
all(target_arch = "wasm32", not(target_os = "wasi")),
|
||||
wasm_bindgen_test::wasm_bindgen_test
|
||||
)]
|
||||
#[test]
|
||||
fn fit_with_centroids_predict() {
|
||||
let x = DenseMatrix::from_2d_array(&[
|
||||
&[5.1, 3.5, 1.4, 0.2],
|
||||
&[4.9, 3.0, 1.4, 0.2],
|
||||
&[4.7, 3.2, 1.3, 0.2],
|
||||
&[4.6, 3.1, 1.5, 0.2],
|
||||
&[5.0, 3.6, 1.4, 0.2],
|
||||
&[5.4, 3.9, 1.7, 0.4],
|
||||
&[4.6, 3.4, 1.4, 0.3],
|
||||
&[5.0, 3.4, 1.5, 0.2],
|
||||
&[4.4, 2.9, 1.4, 0.2],
|
||||
&[4.9, 3.1, 1.5, 0.1],
|
||||
&[7.0, 3.2, 4.7, 1.4],
|
||||
&[6.4, 3.2, 4.5, 1.5],
|
||||
&[6.9, 3.1, 4.9, 1.5],
|
||||
&[5.5, 2.3, 4.0, 1.3],
|
||||
&[6.5, 2.8, 4.6, 1.5],
|
||||
&[5.7, 2.8, 4.5, 1.3],
|
||||
&[6.3, 3.3, 4.7, 1.6],
|
||||
&[4.9, 2.4, 3.3, 1.0],
|
||||
&[6.6, 2.9, 4.6, 1.3],
|
||||
&[5.2, 2.7, 3.9, 1.4],
|
||||
]);
|
||||
|
||||
let parameters = KMeansParameters {
|
||||
k: 3,
|
||||
max_iter: 50,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// compute pairs
|
||||
let fastpair = fastpair::FastPair::new(&x).unwrap();
|
||||
|
||||
// compute centroids for N closest pairs
|
||||
let mut n: isize = 2;
|
||||
let mut centroids = vec![vec![0f64; x.shape().1]; n as usize + 1];
|
||||
for p in fastpair.ordered_pairs() {
|
||||
if n == -1 {
|
||||
break
|
||||
}
|
||||
|
||||
centroids[n as usize] = {
|
||||
let mut result: Vec<f64> = Vec::with_capacity(x.shape().1);
|
||||
for val1 in x.get_row(p.node).iterator(0) {
|
||||
for val2 in x.get_row(p.neighbour.unwrap()).iterator(0) {
|
||||
let sum = val1 + val2;
|
||||
let avg = sum * 0.5f64;
|
||||
result.push(avg);
|
||||
}
|
||||
}
|
||||
result
|
||||
};
|
||||
|
||||
n -= 1;
|
||||
}
|
||||
|
||||
|
||||
let kmeans = KMeans::fit_with_centroids(
|
||||
&x, parameters, centroids).unwrap();
|
||||
|
||||
let y: Vec<usize> = kmeans.predict(&x).unwrap();
|
||||
|
||||
for (i, _y_i) in y.iter().enumerate() {
|
||||
assert_eq!({ y[i] }, kmeans._y[i]);
|
||||
for i in 0..y.len() {
|
||||
assert_eq!(y[i] as usize, kmeans._y[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ use crate::dataset::Dataset;
|
||||
pub fn load_dataset() -> Dataset<f32, f32> {
|
||||
let (x, y, num_samples, num_features) = match deserialize_data(std::include_bytes!("boston.xy"))
|
||||
{
|
||||
Err(why) => panic!("Can't deserialize boston.xy. {why}"),
|
||||
Err(why) => panic!("Can't deserialize boston.xy. {}", why),
|
||||
Ok((x, y, num_samples, num_features)) => (x, y, num_samples, num_features),
|
||||
};
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ use crate::dataset::Dataset;
|
||||
pub fn load_dataset() -> Dataset<f32, u32> {
|
||||
let (x, y, num_samples, num_features) =
|
||||
match deserialize_data(std::include_bytes!("breast_cancer.xy")) {
|
||||
Err(why) => panic!("Can't deserialize breast_cancer.xy. {why}"),
|
||||
Err(why) => panic!("Can't deserialize breast_cancer.xy. {}", why),
|
||||
Ok((x, y, num_samples, num_features)) => (
|
||||
x,
|
||||
y.into_iter().map(|x| x as u32).collect(),
|
||||
|
||||
@@ -26,7 +26,7 @@ use crate::dataset::Dataset;
|
||||
pub fn load_dataset() -> Dataset<f32, u32> {
|
||||
let (x, y, num_samples, num_features) =
|
||||
match deserialize_data(std::include_bytes!("diabetes.xy")) {
|
||||
Err(why) => panic!("Can't deserialize diabetes.xy. {why}"),
|
||||
Err(why) => panic!("Can't deserialize diabetes.xy. {}", why),
|
||||
Ok((x, y, num_samples, num_features)) => (
|
||||
x,
|
||||
y.into_iter().map(|x| x as u32).collect(),
|
||||
|
||||
@@ -16,7 +16,7 @@ use crate::dataset::Dataset;
|
||||
pub fn load_dataset() -> Dataset<f32, f32> {
|
||||
let (x, y, num_samples, num_features) = match deserialize_data(std::include_bytes!("digits.xy"))
|
||||
{
|
||||
Err(why) => panic!("Can't deserialize digits.xy. {why}"),
|
||||
Err(why) => panic!("Can't deserialize digits.xy. {}", why),
|
||||
Ok((x, y, num_samples, num_features)) => (x, y, num_samples, num_features),
|
||||
};
|
||||
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ use crate::dataset::Dataset;
|
||||
pub fn load_dataset() -> Dataset<f32, u32> {
|
||||
let (x, y, num_samples, num_features): (Vec<f32>, Vec<u32>, usize, usize) =
|
||||
match deserialize_data(std::include_bytes!("iris.xy")) {
|
||||
Err(why) => panic!("Can't deserialize iris.xy. {why}"),
|
||||
Err(why) => panic!("Can't deserialize iris.xy. {}", why),
|
||||
Ok((x, y, num_samples, num_features)) => (
|
||||
x,
|
||||
y.into_iter().map(|x| x as u32).collect(),
|
||||
|
||||
+1
-1
@@ -78,7 +78,7 @@ pub(crate) fn serialize_data<X: Number + RealNumber, Y: RealNumber>(
|
||||
.collect();
|
||||
file.write_all(&y)?;
|
||||
}
|
||||
Err(why) => panic!("couldn't create {filename}: {why}"),
|
||||
Err(why) => panic!("couldn't create {}: {}", filename, why),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -231,7 +231,8 @@ impl<T: Number + RealNumber, X: Array2<T> + SVDDecomposable<T> + EVDDecomposable
|
||||
|
||||
if parameters.n_components > n {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Number of components, n_components should be <= number of attributes ({n})"
|
||||
"Number of components, n_components should be <= number of attributes ({})",
|
||||
n
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -373,20 +374,21 @@ mod tests {
|
||||
let parameters = PCASearchParameters {
|
||||
n_components: vec![2, 4],
|
||||
use_correlation_matrix: vec![true, false],
|
||||
..Default::default()
|
||||
};
|
||||
let mut iter = parameters.into_iter();
|
||||
let next = iter.next().unwrap();
|
||||
assert_eq!(next.n_components, 2);
|
||||
assert!(next.use_correlation_matrix);
|
||||
assert_eq!(next.use_correlation_matrix, true);
|
||||
let next = iter.next().unwrap();
|
||||
assert_eq!(next.n_components, 4);
|
||||
assert!(next.use_correlation_matrix);
|
||||
assert_eq!(next.use_correlation_matrix, true);
|
||||
let next = iter.next().unwrap();
|
||||
assert_eq!(next.n_components, 2);
|
||||
assert!(!next.use_correlation_matrix);
|
||||
assert_eq!(next.use_correlation_matrix, false);
|
||||
let next = iter.next().unwrap();
|
||||
assert_eq!(next.n_components, 4);
|
||||
assert!(!next.use_correlation_matrix);
|
||||
assert_eq!(next.use_correlation_matrix, false);
|
||||
assert!(iter.next().is_none());
|
||||
}
|
||||
|
||||
@@ -570,8 +572,8 @@ mod tests {
|
||||
epsilon = 1e-4
|
||||
));
|
||||
|
||||
for (i, pca_eigenvalues_i) in pca.eigenvalues.iter().enumerate() {
|
||||
assert!((pca_eigenvalues_i.abs() - expected_eigenvalues[i].abs()).abs() < 1e-8);
|
||||
for i in 0..pca.eigenvalues.len() {
|
||||
assert!((pca.eigenvalues[i].abs() - expected_eigenvalues[i].abs()).abs() < 1e-8);
|
||||
}
|
||||
|
||||
let us_arrests_t = pca.transform(&us_arrests).unwrap();
|
||||
@@ -692,8 +694,8 @@ mod tests {
|
||||
epsilon = 1e-4
|
||||
));
|
||||
|
||||
for (i, pca_eigenvalues_i) in pca.eigenvalues.iter().enumerate() {
|
||||
assert!((pca_eigenvalues_i.abs() - expected_eigenvalues[i].abs()).abs() < 1e-8);
|
||||
for i in 0..pca.eigenvalues.len() {
|
||||
assert!((pca.eigenvalues[i].abs() - expected_eigenvalues[i].abs()).abs() < 1e-8);
|
||||
}
|
||||
|
||||
let us_arrests_t = pca.transform(&us_arrests).unwrap();
|
||||
|
||||
@@ -180,7 +180,8 @@ impl<T: Number + RealNumber, X: Array2<T> + SVDDecomposable<T> + EVDDecomposable
|
||||
|
||||
if parameters.n_components >= p {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Number of components, n_components should be < number of attributes ({p})"
|
||||
"Number of components, n_components should be < number of attributes ({})",
|
||||
p
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -201,7 +202,8 @@ impl<T: Number + RealNumber, X: Array2<T> + SVDDecomposable<T> + EVDDecomposable
|
||||
let (p_c, k) = self.components.shape();
|
||||
if p_c != p {
|
||||
return Err(Failed::transform(&format!(
|
||||
"Can not transform a {n}x{p} matrix into {n}x{k} matrix, incorrect input dimentions"
|
||||
"Can not transform a {}x{} matrix into {}x{} matrix, incorrect input dimentions",
|
||||
n, p, n, k
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -225,6 +227,7 @@ mod tests {
|
||||
fn search_parameters() {
|
||||
let parameters = SVDSearchParameters {
|
||||
n_components: vec![10, 100],
|
||||
..Default::default()
|
||||
};
|
||||
let mut iter = parameters.into_iter();
|
||||
let next = iter.next().unwrap();
|
||||
|
||||
@@ -454,12 +454,8 @@ impl<TX: FloatNumber + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY
|
||||
y: &Y,
|
||||
parameters: RandomForestClassifierParameters,
|
||||
) -> Result<RandomForestClassifier<TX, TY, X, Y>, Failed> {
|
||||
let (x_nrows, num_attributes) = x.shape();
|
||||
let (_, num_attributes) = x.shape();
|
||||
let y_ncols = y.shape();
|
||||
if x_nrows != y_ncols {
|
||||
return Err(Failed::fit("Number of rows in X should = len(y)"));
|
||||
}
|
||||
|
||||
let mut yi: Vec<usize> = vec![0; y_ncols];
|
||||
let classes = y.unique();
|
||||
|
||||
@@ -682,30 +678,6 @@ mod tests {
|
||||
assert!(accuracy(&y, &classifier.predict(&x).unwrap()) >= 0.95);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_random_matrix_with_wrong_rownum() {
|
||||
let x_rand: DenseMatrix<f64> = DenseMatrix::<f64>::rand(21, 200);
|
||||
|
||||
let y: Vec<u32> = vec![0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
|
||||
|
||||
let fail = RandomForestClassifier::fit(
|
||||
&x_rand,
|
||||
&y,
|
||||
RandomForestClassifierParameters {
|
||||
criterion: SplitCriterion::Gini,
|
||||
max_depth: Option::None,
|
||||
min_samples_leaf: 1,
|
||||
min_samples_split: 2,
|
||||
n_trees: 100,
|
||||
m: Option::None,
|
||||
keep_samples: false,
|
||||
seed: 87,
|
||||
},
|
||||
);
|
||||
|
||||
assert!(fail.is_err());
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
all(target_arch = "wasm32", not(target_os = "wasi")),
|
||||
wasm_bindgen_test::wasm_bindgen_test
|
||||
|
||||
@@ -399,10 +399,6 @@ impl<TX: Number + FloatNumber + PartialOrd, TY: Number, X: Array2<TX>, Y: Array1
|
||||
) -> Result<RandomForestRegressor<TX, TY, X, Y>, Failed> {
|
||||
let (n_rows, num_attributes) = x.shape();
|
||||
|
||||
if n_rows != y.shape() {
|
||||
return Err(Failed::fit("Number of rows in X should = len(y)"));
|
||||
}
|
||||
|
||||
let mtry = parameters
|
||||
.m
|
||||
.unwrap_or((num_attributes as f64).sqrt().floor() as usize);
|
||||
@@ -599,32 +595,6 @@ mod tests {
|
||||
assert!(mean_absolute_error(&y, &y_hat) < 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_random_matrix_with_wrong_rownum() {
|
||||
let x_rand: DenseMatrix<f64> = DenseMatrix::<f64>::rand(17, 200);
|
||||
|
||||
let y = vec![
|
||||
83.0, 88.5, 88.2, 89.5, 96.2, 98.1, 99.0, 100.0, 101.2, 104.6, 108.4, 110.8, 112.6,
|
||||
114.2, 115.7, 116.9,
|
||||
];
|
||||
|
||||
let fail = RandomForestRegressor::fit(
|
||||
&x_rand,
|
||||
&y,
|
||||
RandomForestRegressorParameters {
|
||||
max_depth: Option::None,
|
||||
min_samples_leaf: 1,
|
||||
min_samples_split: 2,
|
||||
n_trees: 1000,
|
||||
m: Option::None,
|
||||
keep_samples: false,
|
||||
seed: 87,
|
||||
},
|
||||
);
|
||||
|
||||
assert!(fail.is_err());
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
all(target_arch = "wasm32", not(target_os = "wasi")),
|
||||
wasm_bindgen_test::wasm_bindgen_test
|
||||
|
||||
+2
-2
@@ -30,7 +30,7 @@ pub enum FailedError {
|
||||
DecompositionFailed,
|
||||
/// Can't solve for x
|
||||
SolutionFailed,
|
||||
/// Error in input parameters
|
||||
/// Erro in input
|
||||
ParametersError,
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ impl fmt::Display for FailedError {
|
||||
FailedError::SolutionFailed => "Can't find solution",
|
||||
FailedError::ParametersError => "Error in input, check parameters",
|
||||
};
|
||||
write!(f, "{failed_err_str}")
|
||||
write!(f, "{}", failed_err_str)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -3,8 +3,7 @@
|
||||
clippy::too_many_arguments,
|
||||
clippy::many_single_char_names,
|
||||
clippy::unnecessary_wraps,
|
||||
clippy::upper_case_acronyms,
|
||||
clippy::approx_constant
|
||||
clippy::upper_case_acronyms
|
||||
)]
|
||||
#![warn(missing_docs)]
|
||||
#![warn(rustdoc::missing_doc_code_examples)]
|
||||
|
||||
+30
-13
@@ -548,7 +548,7 @@ pub trait ArrayView2<T: Debug + Display + Copy + Sized>: Array<T, (usize, usize)
|
||||
let (nrows, ncols) = self.shape();
|
||||
for r in 0..nrows {
|
||||
let row: Vec<T> = (0..ncols).map(|c| *self.get((r, c))).collect();
|
||||
writeln!(f, "{row:?}")?
|
||||
writeln!(f, "{:?}", row)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -918,7 +918,8 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
|
||||
let len = self.shape();
|
||||
assert!(
|
||||
index.iter().all(|&i| i < len),
|
||||
"All indices in `take` should be < {len}"
|
||||
"All indices in `take` should be < {}",
|
||||
len
|
||||
);
|
||||
Self::from_iterator(index.iter().map(move |&i| *self.get(i)), index.len())
|
||||
}
|
||||
@@ -989,7 +990,10 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
|
||||
};
|
||||
assert!(
|
||||
d1 == len,
|
||||
"Can not multiply {nrows}x{ncols} matrix by {len} vector"
|
||||
"Can not multiply {}x{} matrix by {} vector",
|
||||
nrows,
|
||||
ncols,
|
||||
len
|
||||
);
|
||||
let mut result = Self::zeros(d2);
|
||||
for i in 0..d2 {
|
||||
@@ -1107,7 +1111,11 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
||||
|
||||
assert!(
|
||||
nrows * ncols == onrows * oncols,
|
||||
"Can't reshape {onrows}x{oncols} array into a {nrows}x{ncols} array"
|
||||
"Can't reshape {}x{} array into a {}x{} array",
|
||||
onrows,
|
||||
oncols,
|
||||
nrows,
|
||||
ncols
|
||||
);
|
||||
|
||||
Self::from_iterator(self.iterator(0).cloned(), nrows, ncols, axis)
|
||||
@@ -1121,7 +1129,11 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
||||
let (o_nrows, o_ncols) = other.shape();
|
||||
assert!(
|
||||
ncols == o_nrows,
|
||||
"Can't multiply {nrows}x{ncols} and {o_nrows}x{o_ncols} matrices"
|
||||
"Can't multiply {}x{} and {}x{} matrices",
|
||||
nrows,
|
||||
ncols,
|
||||
o_nrows,
|
||||
o_ncols
|
||||
);
|
||||
let inner_d = ncols;
|
||||
let mut result = Self::zeros(nrows, o_ncols);
|
||||
@@ -1154,7 +1166,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
||||
_ => (nrows, ncols, o_nrows, o_ncols),
|
||||
};
|
||||
if d1 != d4 {
|
||||
panic!("Can not multiply {d2}x{d1} by {d4}x{d3} matrices");
|
||||
panic!("Can not multiply {}x{} by {}x{} matrices", d2, d1, d4, d3);
|
||||
}
|
||||
let mut result = Self::zeros(d2, d3);
|
||||
for r in 0..d2 {
|
||||
@@ -1186,7 +1198,10 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
||||
};
|
||||
assert!(
|
||||
d2 == len,
|
||||
"Can not multiply {nrows}x{ncols} matrix by {len} vector"
|
||||
"Can not multiply {}x{} matrix by {} vector",
|
||||
nrows,
|
||||
ncols,
|
||||
len
|
||||
);
|
||||
let mut result = Self::zeros(d1, 1);
|
||||
for i in 0..d1 {
|
||||
@@ -1417,7 +1432,8 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
||||
0 => {
|
||||
assert!(
|
||||
index.iter().all(|&i| i < nrows),
|
||||
"All indices in `take` should be < {nrows}"
|
||||
"All indices in `take` should be < {}",
|
||||
nrows
|
||||
);
|
||||
Self::from_iterator(
|
||||
index
|
||||
@@ -1432,7 +1448,8 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
||||
_ => {
|
||||
assert!(
|
||||
index.iter().all(|&i| i < ncols),
|
||||
"All indices in `take` should be < {ncols}"
|
||||
"All indices in `take` should be < {}",
|
||||
ncols
|
||||
);
|
||||
Self::from_iterator(
|
||||
(0..nrows)
|
||||
@@ -1570,7 +1587,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
||||
mean
|
||||
}
|
||||
|
||||
/// copy column as a vector
|
||||
/// copy coumn as a vector
|
||||
fn copy_col_as_vec(&self, col: usize, result: &mut Vec<T>) {
|
||||
for (r, result_r) in result.iter_mut().enumerate().take(self.shape().0) {
|
||||
*result_r = *self.get((r, col));
|
||||
@@ -1719,7 +1736,7 @@ mod tests {
|
||||
let r = Vec::<f32>::rand(4);
|
||||
assert!(r.iterator(0).all(|&e| e <= 1f32));
|
||||
assert!(r.iterator(0).all(|&e| e >= 0f32));
|
||||
assert!(r.iterator(0).copied().sum::<f32>() > 0f32);
|
||||
assert!(r.iterator(0).map(|v| *v).sum::<f32>() > 0f32);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1937,7 +1954,7 @@ mod tests {
|
||||
DenseMatrix::from_2d_array(&[&[1, 3], &[2, 4]])
|
||||
);
|
||||
assert_eq!(
|
||||
DenseMatrix::concatenate_2d(&[&a, &b], 0),
|
||||
DenseMatrix::concatenate_2d(&[&a.clone(), &b.clone()], 0),
|
||||
DenseMatrix::from_2d_array(&[&[1, 2], &[3, 4], &[5, 6], &[7, 8]])
|
||||
);
|
||||
assert_eq!(
|
||||
@@ -2008,7 +2025,7 @@ mod tests {
|
||||
let r = DenseMatrix::<f32>::rand(2, 2);
|
||||
assert!(r.iterator(0).all(|&e| e <= 1f32));
|
||||
assert!(r.iterator(0).all(|&e| e >= 0f32));
|
||||
assert!(r.iterator(0).copied().sum::<f32>() > 0f32);
|
||||
assert!(r.iterator(0).map(|v| *v).sum::<f32>() > 0f32);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+15
-15
@@ -431,9 +431,9 @@ impl<T: Number + RealNumber> SVDDecomposable<T> for DenseMatrix<T> {}
|
||||
impl<'a, T: Debug + Display + Copy + Sized> Array<T, (usize, usize)> for DenseMatrixView<'a, T> {
|
||||
fn get(&self, pos: (usize, usize)) -> &T {
|
||||
if self.column_major {
|
||||
&self.values[pos.0 + pos.1 * self.stride]
|
||||
&self.values[(pos.0 + pos.1 * self.stride)]
|
||||
} else {
|
||||
&self.values[pos.0 * self.stride + pos.1]
|
||||
&self.values[(pos.0 * self.stride + pos.1)]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -495,9 +495,9 @@ impl<'a, T: Debug + Display + Copy + Sized> ArrayView1<T> for DenseMatrixView<'a
|
||||
impl<'a, T: Debug + Display + Copy + Sized> Array<T, (usize, usize)> for DenseMatrixMutView<'a, T> {
|
||||
fn get(&self, pos: (usize, usize)) -> &T {
|
||||
if self.column_major {
|
||||
&self.values[pos.0 + pos.1 * self.stride]
|
||||
&self.values[(pos.0 + pos.1 * self.stride)]
|
||||
} else {
|
||||
&self.values[pos.0 * self.stride + pos.1]
|
||||
&self.values[(pos.0 * self.stride + pos.1)]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -519,9 +519,9 @@ impl<'a, T: Debug + Display + Copy + Sized> MutArray<T, (usize, usize)>
|
||||
{
|
||||
fn set(&mut self, pos: (usize, usize), x: T) {
|
||||
if self.column_major {
|
||||
self.values[pos.0 + pos.1 * self.stride] = x;
|
||||
self.values[(pos.0 + pos.1 * self.stride)] = x;
|
||||
} else {
|
||||
self.values[pos.0 * self.stride + pos.1] = x;
|
||||
self.values[(pos.0 * self.stride + pos.1)] = x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -581,9 +581,9 @@ mod tests {
|
||||
vec![4, 5, 6],
|
||||
DenseMatrix::from_slice(&(*x.slice(1..2, 0..3))).values
|
||||
);
|
||||
let second_row: Vec<i32> = x.slice(1..2, 0..3).iterator(0).copied().collect();
|
||||
let second_row: Vec<i32> = x.slice(1..2, 0..3).iterator(0).map(|x| *x).collect();
|
||||
assert_eq!(vec![4, 5, 6], second_row);
|
||||
let second_col: Vec<i32> = x.slice(0..3, 1..2).iterator(0).copied().collect();
|
||||
let second_col: Vec<i32> = x.slice(0..3, 1..2).iterator(0).map(|x| *x).collect();
|
||||
assert_eq!(vec![2, 5, 8], second_col);
|
||||
}
|
||||
|
||||
@@ -640,12 +640,12 @@ mod tests {
|
||||
let x = DenseMatrix::<&str>::from_2d_array(&[&["1", "2", "3"], &["4", "5", "6"]]);
|
||||
|
||||
assert_eq!(vec!["1", "4", "2", "5", "3", "6"], x.values);
|
||||
assert!(x.column_major);
|
||||
assert!(x.column_major == true);
|
||||
|
||||
// transpose
|
||||
let x = x.transpose();
|
||||
assert_eq!(vec!["1", "4", "2", "5", "3", "6"], x.values);
|
||||
assert!(!x.column_major); // should change column_major
|
||||
assert!(x.column_major == false); // should change column_major
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -659,7 +659,7 @@ mod tests {
|
||||
vec![1, 2, 3, 4, 5, 6],
|
||||
m.values.iter().map(|e| **e).collect::<Vec<i32>>()
|
||||
);
|
||||
assert!(!m.column_major);
|
||||
assert!(m.column_major == false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -667,10 +667,10 @@ mod tests {
|
||||
let a = DenseMatrix::from_2d_array(&[&[1, 2, 3], &[4, 5, 6]]);
|
||||
let b = DenseMatrix::from_2d_array(&[&[1, 2], &[3, 4], &[5, 6]]);
|
||||
|
||||
println!("{a}");
|
||||
println!("{}", a);
|
||||
// take column 0 and 2
|
||||
assert_eq!(vec![1, 3, 4, 6], a.take(&[0, 2], 1).values);
|
||||
println!("{b}");
|
||||
println!("{}", b);
|
||||
// take rows 0 and 2
|
||||
assert_eq!(vec![1, 2, 5, 6], b.take(&[0, 2], 0).values);
|
||||
}
|
||||
@@ -692,11 +692,11 @@ mod tests {
|
||||
|
||||
let a = a.reshape(2, 6, 0);
|
||||
assert_eq!(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], a.values);
|
||||
assert!(a.ncols == 6 && a.nrows == 2 && !a.column_major);
|
||||
assert!(a.ncols == 6 && a.nrows == 2 && a.column_major == false);
|
||||
|
||||
let a = a.reshape(3, 4, 1);
|
||||
assert_eq!(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], a.values);
|
||||
assert!(a.ncols == 4 && a.nrows == 3 && a.column_major);
|
||||
assert!(a.ncols == 4 && a.nrows == 3 && a.column_major == true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -15,25 +15,6 @@ pub struct VecView<'a, T: Debug + Display + Copy + Sized> {
|
||||
ptr: &'a [T],
|
||||
}
|
||||
|
||||
impl<T: Debug + Display + Copy + Sized> Array<T, usize> for &[T] {
|
||||
fn get(&self, i: usize) -> &T {
|
||||
&self[i]
|
||||
}
|
||||
|
||||
fn shape(&self) -> usize {
|
||||
self.len()
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.len() > 0
|
||||
}
|
||||
|
||||
fn iterator<'b>(&'b self, axis: u8) -> Box<dyn Iterator<Item = &'b T> + 'b> {
|
||||
assert!(axis == 0, "For one dimensional array `axis` should == 0");
|
||||
Box::new(self.iter())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Debug + Display + Copy + Sized> Array<T, usize> for Vec<T> {
|
||||
fn get(&self, i: usize) -> &T {
|
||||
&self[i]
|
||||
@@ -65,7 +46,6 @@ impl<T: Debug + Display + Copy + Sized> MutArray<T, usize> for Vec<T> {
|
||||
}
|
||||
|
||||
impl<T: Debug + Display + Copy + Sized> ArrayView1<T> for Vec<T> {}
|
||||
impl<T: Debug + Display + Copy + Sized> ArrayView1<T> for &[T] {}
|
||||
|
||||
impl<T: Debug + Display + Copy + Sized> MutArrayView1<T> for Vec<T> {}
|
||||
|
||||
@@ -180,8 +160,8 @@ mod tests {
|
||||
fn dot_product<T: Number, V: Array1<T>>(v: &V) -> T {
|
||||
let vv = V::zeros(10);
|
||||
let v_s = vv.slice(0..3);
|
||||
|
||||
v_s.dot(v)
|
||||
let dot = v_s.dot(v);
|
||||
dot
|
||||
}
|
||||
|
||||
fn vector_ops<T: Number + PartialOrd, V: Array1<T>>(_: &V) -> T {
|
||||
@@ -236,7 +216,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_mut_iterator() {
|
||||
let mut x = vec![1, 2, 3];
|
||||
x.iterator_mut(0).for_each(|v| *v *= 2);
|
||||
x.iterator_mut(0).for_each(|v| *v = *v * 2);
|
||||
assert_eq!(vec![2, 4, 6], x);
|
||||
}
|
||||
|
||||
|
||||
@@ -217,7 +217,7 @@ mod tests {
|
||||
fn test_iterator() {
|
||||
let a = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
||||
|
||||
let v: Vec<i32> = a.iterator(0).copied().collect();
|
||||
let v: Vec<i32> = a.iterator(0).map(|&v| v).collect();
|
||||
assert_eq!(v, vec!(1, 2, 3, 4, 5, 6));
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ mod tests {
|
||||
let x = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
||||
let x_slice = Array2::slice(&x, 0..2, 1..2);
|
||||
assert_eq!((2, 1), x_slice.shape());
|
||||
let v: Vec<i32> = x_slice.iterator(0).copied().collect();
|
||||
let v: Vec<i32> = x_slice.iterator(0).map(|&v| v).collect();
|
||||
assert_eq!(v, [2, 5]);
|
||||
}
|
||||
|
||||
@@ -245,11 +245,11 @@ mod tests {
|
||||
let x = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
||||
let x_slice = Array2::slice(&x, 0..2, 0..3);
|
||||
assert_eq!(
|
||||
x_slice.iterator(0).copied().collect::<Vec<i32>>(),
|
||||
x_slice.iterator(0).map(|&v| v).collect::<Vec<i32>>(),
|
||||
vec![1, 2, 3, 4, 5, 6]
|
||||
);
|
||||
assert_eq!(
|
||||
x_slice.iterator(1).copied().collect::<Vec<i32>>(),
|
||||
x_slice.iterator(1).map(|&v| v).collect::<Vec<i32>>(),
|
||||
vec![1, 4, 2, 5, 3, 6]
|
||||
);
|
||||
}
|
||||
@@ -279,8 +279,8 @@ mod tests {
|
||||
fn test_c_from_iterator() {
|
||||
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
|
||||
let a: NDArray2<i32> = Array2::from_iterator(data.clone().into_iter(), 4, 3, 0);
|
||||
println!("{a}");
|
||||
println!("{}", a);
|
||||
let a: NDArray2<i32> = Array2::from_iterator(data.into_iter(), 4, 3, 1);
|
||||
println!("{a}");
|
||||
println!("{}", a);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ mod tests {
|
||||
fn test_iterator() {
|
||||
let a = arr1(&[1, 2, 3]);
|
||||
|
||||
let v: Vec<i32> = a.iterator(0).copied().collect();
|
||||
let v: Vec<i32> = a.iterator(0).map(|&v| v).collect();
|
||||
assert_eq!(v, vec!(1, 2, 3));
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ pub trait EVDDecomposable<T: Number + RealNumber>: Array2<T> {
|
||||
fn evd_mut(mut self, symmetric: bool) -> Result<EVD<T, Self>, Failed> {
|
||||
let (nrows, ncols) = self.shape();
|
||||
if ncols != nrows {
|
||||
panic!("Matrix is not square: {nrows} x {ncols}");
|
||||
panic!("Matrix is not square: {} x {}", nrows, ncols);
|
||||
}
|
||||
|
||||
let n = nrows;
|
||||
@@ -837,8 +837,10 @@ mod tests {
|
||||
evd.V.abs(),
|
||||
epsilon = 1e-4
|
||||
));
|
||||
for (i, eigen_values_i) in eigen_values.iter().enumerate() {
|
||||
assert!((eigen_values_i - evd.d[i]).abs() < 1e-4);
|
||||
for i in 0..eigen_values.len() {
|
||||
assert!((eigen_values[i] - evd.d[i]).abs() < 1e-4);
|
||||
}
|
||||
for i in 0..eigen_values.len() {
|
||||
assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON);
|
||||
}
|
||||
}
|
||||
@@ -869,8 +871,10 @@ mod tests {
|
||||
evd.V.abs(),
|
||||
epsilon = 1e-4
|
||||
));
|
||||
for (i, eigen_values_i) in eigen_values.iter().enumerate() {
|
||||
assert!((eigen_values_i - evd.d[i]).abs() < 1e-4);
|
||||
for i in 0..eigen_values.len() {
|
||||
assert!((eigen_values[i] - evd.d[i]).abs() < 1e-4);
|
||||
}
|
||||
for i in 0..eigen_values.len() {
|
||||
assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON);
|
||||
}
|
||||
}
|
||||
@@ -904,11 +908,11 @@ mod tests {
|
||||
evd.V.abs(),
|
||||
epsilon = 1e-4
|
||||
));
|
||||
for (i, eigen_values_d_i) in eigen_values_d.iter().enumerate() {
|
||||
assert!((eigen_values_d_i - evd.d[i]).abs() < 1e-4);
|
||||
for i in 0..eigen_values_d.len() {
|
||||
assert!((eigen_values_d[i] - evd.d[i]).abs() < 1e-4);
|
||||
}
|
||||
for (i, eigen_values_e_i) in eigen_values_e.iter().enumerate() {
|
||||
assert!((eigen_values_e_i - evd.e[i]).abs() < 1e-4);
|
||||
for i in 0..eigen_values_e.len() {
|
||||
assert!((eigen_values_e[i] - evd.e[i]).abs() < 1e-4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ impl<T: Number + RealNumber, M: Array2<T>> LU<T, M> {
|
||||
let (m, n) = self.LU.shape();
|
||||
|
||||
if m != n {
|
||||
panic!("Matrix is not square: {m}x{n}");
|
||||
panic!("Matrix is not square: {}x{}", m, n);
|
||||
}
|
||||
|
||||
let mut inv = M::zeros(n, n);
|
||||
@@ -143,7 +143,10 @@ impl<T: Number + RealNumber, M: Array2<T>> LU<T, M> {
|
||||
let (b_m, b_n) = b.shape();
|
||||
|
||||
if b_m != m {
|
||||
panic!("Row dimensions do not agree: A is {m} x {n}, but B is {b_m} x {b_n}");
|
||||
panic!(
|
||||
"Row dimensions do not agree: A is {} x {}, but B is {} x {}",
|
||||
m, n, b_m, b_n
|
||||
);
|
||||
}
|
||||
|
||||
if self.singular {
|
||||
|
||||
@@ -102,7 +102,10 @@ impl<T: Number + RealNumber, M: Array2<T>> QR<T, M> {
|
||||
let (b_nrows, b_ncols) = b.shape();
|
||||
|
||||
if b_nrows != m {
|
||||
panic!("Row dimensions do not agree: A is {m} x {n}, but B is {b_nrows} x {b_ncols}");
|
||||
panic!(
|
||||
"Row dimensions do not agree: A is {} x {}, but B is {} x {}",
|
||||
m, n, b_nrows, b_ncols
|
||||
);
|
||||
}
|
||||
|
||||
if self.singular {
|
||||
|
||||
@@ -286,7 +286,7 @@ mod tests {
|
||||
}
|
||||
|
||||
{
|
||||
let mut m = m;
|
||||
let mut m = m.clone();
|
||||
m.standard_scale_mut(&m.mean(1), &m.std(1), 1);
|
||||
assert_eq!(&m, &expected_1);
|
||||
}
|
||||
|
||||
@@ -509,8 +509,8 @@ mod tests {
|
||||
|
||||
assert!(relative_eq!(V.abs(), svd.V.abs(), epsilon = 1e-4));
|
||||
assert!(relative_eq!(U.abs(), svd.U.abs(), epsilon = 1e-4));
|
||||
for (i, s_i) in s.iter().enumerate() {
|
||||
assert!((s_i - svd.s[i]).abs() < 1e-4);
|
||||
for i in 0..s.len() {
|
||||
assert!((s[i] - svd.s[i]).abs() < 1e-4);
|
||||
}
|
||||
}
|
||||
#[cfg_attr(
|
||||
@@ -713,8 +713,8 @@ mod tests {
|
||||
|
||||
assert!(relative_eq!(V.abs(), svd.V.abs(), epsilon = 1e-4));
|
||||
assert!(relative_eq!(U.abs(), svd.U.abs(), epsilon = 1e-4));
|
||||
for (i, s_i) in s.iter().enumerate() {
|
||||
assert!((s_i - svd.s[i]).abs() < 1e-4);
|
||||
for i in 0..s.len() {
|
||||
assert!((s[i] - svd.s[i]).abs() < 1e-4);
|
||||
}
|
||||
}
|
||||
#[cfg_attr(
|
||||
|
||||
@@ -425,7 +425,10 @@ impl<TX: FloatNumber + RealNumber, TY: Number, X: Array2<TX>, Y: Array1<TY>>
|
||||
|
||||
for (i, col_std_i) in col_std.iter().enumerate() {
|
||||
if (*col_std_i - TX::zero()).abs() < TX::epsilon() {
|
||||
return Err(Failed::fit(&format!("Cannot rescale constant column {i}")));
|
||||
return Err(Failed::fit(&format!(
|
||||
"Cannot rescale constant column {}",
|
||||
i
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -356,7 +356,10 @@ impl<TX: FloatNumber + RealNumber, TY: Number, X: Array2<TX>, Y: Array1<TY>> Las
|
||||
|
||||
for (i, col_std_i) in col_std.iter().enumerate() {
|
||||
if (*col_std_i - TX::zero()).abs() < TX::epsilon() {
|
||||
return Err(Failed::fit(&format!("Cannot rescale constant column {i}")));
|
||||
return Err(Failed::fit(&format!(
|
||||
"Cannot rescale constant column {}",
|
||||
i
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -71,14 +71,19 @@ use crate::optimization::line_search::Backtracking;
|
||||
use crate::optimization::FunctionOrder;
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
/// Solver options for Logistic regression. Right now only LBFGS solver is supported.
|
||||
pub enum LogisticRegressionSolverName {
|
||||
/// Limited-memory Broyden–Fletcher–Goldfarb–Shanno method, see [LBFGS paper](http://users.iems.northwestern.edu/~nocedal/lbfgsb.html)
|
||||
#[default]
|
||||
LBFGS,
|
||||
}
|
||||
|
||||
impl Default for LogisticRegressionSolverName {
|
||||
fn default() -> Self {
|
||||
LogisticRegressionSolverName::LBFGS
|
||||
}
|
||||
}
|
||||
|
||||
/// Logistic Regression parameters
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -444,7 +449,8 @@ impl<TX: Number + FloatNumber + RealNumber, TY: Number + Ord, X: Array2<TX>, Y:
|
||||
|
||||
match k.cmp(&2) {
|
||||
Ordering::Less => Err(Failed::fit(&format!(
|
||||
"incorrect number of classes: {k}. Should be >= 2."
|
||||
"incorrect number of classes: {}. Should be >= 2.",
|
||||
k
|
||||
))),
|
||||
Ordering::Equal => {
|
||||
let x0 = Vec::zeros(num_attributes + 1);
|
||||
@@ -630,19 +636,19 @@ mod tests {
|
||||
|
||||
assert!((g[0] + 33.000068218163484).abs() < std::f64::EPSILON);
|
||||
|
||||
let f = objective.f(&[1., 2., 3., 4., 5., 6., 7., 8., 9.]);
|
||||
let f = objective.f(&vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]);
|
||||
|
||||
assert!((f - 408.0052230582765).abs() < std::f64::EPSILON);
|
||||
|
||||
let objective_reg = MultiClassObjectiveFunction {
|
||||
x: &x,
|
||||
y,
|
||||
y: y.clone(),
|
||||
k: 3,
|
||||
alpha: 1.0,
|
||||
_phantom_t: PhantomData,
|
||||
};
|
||||
|
||||
let f = objective_reg.f(&[1., 2., 3., 4., 5., 6., 7., 8., 9.]);
|
||||
let f = objective_reg.f(&vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]);
|
||||
assert!((f - 487.5052).abs() < 1e-4);
|
||||
|
||||
objective_reg.df(&mut g, &vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]);
|
||||
@@ -691,18 +697,18 @@ mod tests {
|
||||
assert!((g[1] - 10.239000702928523).abs() < std::f64::EPSILON);
|
||||
assert!((g[2] - 3.869294270156324).abs() < std::f64::EPSILON);
|
||||
|
||||
let f = objective.f(&[1., 2., 3.]);
|
||||
let f = objective.f(&vec![1., 2., 3.]);
|
||||
|
||||
assert!((f - 59.76994756647412).abs() < std::f64::EPSILON);
|
||||
|
||||
let objective_reg = BinaryObjectiveFunction {
|
||||
x: &x,
|
||||
y,
|
||||
y: y.clone(),
|
||||
alpha: 1.0,
|
||||
_phantom_t: PhantomData,
|
||||
};
|
||||
|
||||
let f = objective_reg.f(&[1., 2., 3.]);
|
||||
let f = objective_reg.f(&vec![1., 2., 3.]);
|
||||
assert!((f - 62.2699).abs() < 1e-4);
|
||||
|
||||
objective_reg.df(&mut g, &vec![1., 2., 3.]);
|
||||
|
||||
@@ -71,16 +71,21 @@ use crate::numbers::basenum::Number;
|
||||
use crate::numbers::realnum::RealNumber;
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
/// Approach to use for estimation of regression coefficients. Cholesky is more efficient but SVD is more stable.
|
||||
pub enum RidgeRegressionSolverName {
|
||||
/// Cholesky decomposition, see [Cholesky](../../linalg/cholesky/index.html)
|
||||
#[default]
|
||||
Cholesky,
|
||||
/// SVD decomposition, see [SVD](../../linalg/svd/index.html)
|
||||
SVD,
|
||||
}
|
||||
|
||||
impl Default for RidgeRegressionSolverName {
|
||||
fn default() -> Self {
|
||||
RidgeRegressionSolverName::Cholesky
|
||||
}
|
||||
}
|
||||
|
||||
/// Ridge Regression parameters
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -379,7 +384,10 @@ impl<
|
||||
|
||||
for (i, col_std_i) in col_std.iter().enumerate() {
|
||||
if (*col_std_i - TX::zero()).abs() < TX::epsilon() {
|
||||
return Err(Failed::fit(&format!("Cannot rescale constant column {i}")));
|
||||
return Err(Failed::fit(&format!(
|
||||
"Cannot rescale constant column {}",
|
||||
i
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -98,8 +98,8 @@ mod tests {
|
||||
let mut scores = HCVScore::new();
|
||||
scores.compute(&v1, &v2);
|
||||
|
||||
assert!((0.2548 - scores.homogeneity.unwrap()).abs() < 1e-4);
|
||||
assert!((0.5440 - scores.completeness.unwrap()).abs() < 1e-4);
|
||||
assert!((0.3471 - scores.v_measure.unwrap()).abs() < 1e-4);
|
||||
assert!((0.2548 - scores.homogeneity.unwrap() as f64).abs() < 1e-4);
|
||||
assert!((0.5440 - scores.completeness.unwrap() as f64).abs() < 1e-4);
|
||||
assert!((0.3471 - scores.v_measure.unwrap() as f64).abs() < 1e-4);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ mod tests {
|
||||
fn entropy_test() {
|
||||
let v1 = vec![0, 0, 1, 1, 2, 0, 4];
|
||||
|
||||
assert!((1.2770 - entropy(&v1).unwrap()).abs() < 1e-4);
|
||||
assert!((1.2770 - entropy(&v1).unwrap() as f64).abs() < 1e-4);
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
|
||||
+2
-2
@@ -95,8 +95,8 @@ mod tests {
|
||||
let score1: f64 = F1::new_with(beta).get_score(&y_true, &y_pred);
|
||||
let score2: f64 = F1::new_with(beta).get_score(&y_true, &y_true);
|
||||
|
||||
println!("{score1:?}");
|
||||
println!("{score2:?}");
|
||||
println!("{:?}", score1);
|
||||
println!("{:?}", score2);
|
||||
|
||||
assert!((score1 - 0.57142857).abs() < 1e-8);
|
||||
assert!((score2 - 1.0).abs() < 1e-8);
|
||||
|
||||
@@ -213,17 +213,17 @@ mod tests {
|
||||
|
||||
for t in &test_masks[0][0..11] {
|
||||
// TODO: this can be prob done better
|
||||
assert!(*t)
|
||||
assert_eq!(*t, true)
|
||||
}
|
||||
for t in &test_masks[0][11..22] {
|
||||
assert!(!*t)
|
||||
assert_eq!(*t, false)
|
||||
}
|
||||
|
||||
for t in &test_masks[1][0..11] {
|
||||
assert!(!*t)
|
||||
assert_eq!(*t, false)
|
||||
}
|
||||
for t in &test_masks[1][11..22] {
|
||||
assert!(*t)
|
||||
assert_eq!(*t, true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,7 +283,9 @@ mod tests {
|
||||
(vec![0, 1, 2, 3, 7, 8, 9], vec![4, 5, 6]),
|
||||
(vec![0, 1, 2, 3, 4, 5, 6], vec![7, 8, 9]),
|
||||
];
|
||||
for ((train, test), (expected_train, expected_test)) in k.split(&x).zip(expected) {
|
||||
for ((train, test), (expected_train, expected_test)) in
|
||||
k.split(&x).into_iter().zip(expected)
|
||||
{
|
||||
assert_eq!(test, expected_test);
|
||||
assert_eq!(train, expected_train);
|
||||
}
|
||||
@@ -305,7 +307,9 @@ mod tests {
|
||||
(vec![0, 1, 2, 3, 7, 8, 9], vec![4, 5, 6]),
|
||||
(vec![0, 1, 2, 3, 4, 5, 6], vec![7, 8, 9]),
|
||||
];
|
||||
for ((train, test), (expected_train, expected_test)) in k.split(&x).zip(expected) {
|
||||
for ((train, test), (expected_train, expected_test)) in
|
||||
k.split(&x).into_iter().zip(expected)
|
||||
{
|
||||
assert_eq!(test.len(), expected_test.len());
|
||||
assert_eq!(train.len(), expected_train.len());
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ pub fn train_test_split<
|
||||
let n_test = ((n as f32) * test_size) as usize;
|
||||
|
||||
if n_test < 1 {
|
||||
panic!("number of sample is too small {n}");
|
||||
panic!("number of sample is too small {}", n);
|
||||
}
|
||||
|
||||
let mut indices: Vec<usize> = (0..n).collect();
|
||||
@@ -553,6 +553,6 @@ mod tests {
|
||||
&accuracy,
|
||||
)
|
||||
.unwrap();
|
||||
println!("{results:?}");
|
||||
println!("{:?}", results);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,18 +271,21 @@ impl<TY: Number + Ord + Unsigned> BernoulliNBDistribution<TY> {
|
||||
let y_samples = y.shape();
|
||||
if y_samples != n_samples {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x should equal size of y; |x|=[{n_samples}], |y|=[{y_samples}]"
|
||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
||||
n_samples, y_samples
|
||||
)));
|
||||
}
|
||||
|
||||
if n_samples == 0 {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x and y should greater than 0; |x|=[{n_samples}]"
|
||||
"Size of x and y should greater than 0; |x|=[{}]",
|
||||
n_samples
|
||||
)));
|
||||
}
|
||||
if alpha < 0f64 {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Alpha should be greater than 0; |alpha|=[{alpha}]"
|
||||
"Alpha should be greater than 0; |alpha|=[{}]",
|
||||
alpha
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -315,7 +318,8 @@ impl<TY: Number + Ord + Unsigned> BernoulliNBDistribution<TY> {
|
||||
feature_in_class_counter[class_index][idx] +=
|
||||
row_i.to_usize().ok_or_else(|| {
|
||||
Failed::fit(&format!(
|
||||
"Elements of the matrix should be 1.0 or 0.0 |found|=[{row_i}]"
|
||||
"Elements of the matrix should be 1.0 or 0.0 |found|=[{}]",
|
||||
row_i
|
||||
))
|
||||
})?;
|
||||
}
|
||||
|
||||
@@ -158,7 +158,8 @@ impl<T: Number + Unsigned> CategoricalNBDistribution<T> {
|
||||
pub fn fit<X: Array2<T>, Y: Array1<T>>(x: &X, y: &Y, alpha: f64) -> Result<Self, Failed> {
|
||||
if alpha < 0f64 {
|
||||
return Err(Failed::fit(&format!(
|
||||
"alpha should be >= 0, alpha=[{alpha}]"
|
||||
"alpha should be >= 0, alpha=[{}]",
|
||||
alpha
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -166,13 +167,15 @@ impl<T: Number + Unsigned> CategoricalNBDistribution<T> {
|
||||
let y_samples = y.shape();
|
||||
if y_samples != n_samples {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x should equal size of y; |x|=[{n_samples}], |y|=[{y_samples}]"
|
||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
||||
n_samples, y_samples
|
||||
)));
|
||||
}
|
||||
|
||||
if n_samples == 0 {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x and y should greater than 0; |x|=[{n_samples}]"
|
||||
"Size of x and y should greater than 0; |x|=[{}]",
|
||||
n_samples
|
||||
)));
|
||||
}
|
||||
let y: Vec<usize> = y.iterator(0).map(|y_i| y_i.to_usize().unwrap()).collect();
|
||||
@@ -199,7 +202,8 @@ impl<T: Number + Unsigned> CategoricalNBDistribution<T> {
|
||||
.max()
|
||||
.ok_or_else(|| {
|
||||
Failed::fit(&format!(
|
||||
"Failed to get the categories for feature = {feature}"
|
||||
"Failed to get the categories for feature = {}",
|
||||
feature
|
||||
))
|
||||
})?;
|
||||
n_categories.push(feature_max + 1);
|
||||
@@ -425,6 +429,7 @@ mod tests {
|
||||
fn search_parameters() {
|
||||
let parameters = CategoricalNBSearchParameters {
|
||||
alpha: vec![1., 2.],
|
||||
..Default::default()
|
||||
};
|
||||
let mut iter = parameters.into_iter();
|
||||
let next = iter.next().unwrap();
|
||||
|
||||
@@ -185,13 +185,15 @@ impl<TY: Number + Ord + Unsigned> GaussianNBDistribution<TY> {
|
||||
let y_samples = y.shape();
|
||||
if y_samples != n_samples {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x should equal size of y; |x|=[{n_samples}], |y|=[{y_samples}]"
|
||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
||||
n_samples, y_samples
|
||||
)));
|
||||
}
|
||||
|
||||
if n_samples == 0 {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x and y should greater than 0; |x|=[{n_samples}]"
|
||||
"Size of x and y should greater than 0; |x|=[{}]",
|
||||
n_samples
|
||||
)));
|
||||
}
|
||||
let (class_labels, indices) = y.unique_with_indices();
|
||||
@@ -373,6 +375,7 @@ mod tests {
|
||||
fn search_parameters() {
|
||||
let parameters = GaussianNBSearchParameters {
|
||||
priors: vec![Some(vec![1.]), Some(vec![2.])],
|
||||
..Default::default()
|
||||
};
|
||||
let mut iter = parameters.into_iter();
|
||||
let next = iter.next().unwrap();
|
||||
|
||||
@@ -220,18 +220,21 @@ impl<TY: Number + Ord + Unsigned> MultinomialNBDistribution<TY> {
|
||||
let y_samples = y.shape();
|
||||
if y_samples != n_samples {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x should equal size of y; |x|=[{n_samples}], |y|=[{y_samples}]"
|
||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
||||
n_samples, y_samples
|
||||
)));
|
||||
}
|
||||
|
||||
if n_samples == 0 {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x and y should greater than 0; |x|=[{n_samples}]"
|
||||
"Size of x and y should greater than 0; |x|=[{}]",
|
||||
n_samples
|
||||
)));
|
||||
}
|
||||
if alpha < 0f64 {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Alpha should be greater than 0; |alpha|=[{alpha}]"
|
||||
"Alpha should be greater than 0; |alpha|=[{}]",
|
||||
alpha
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -263,7 +266,8 @@ impl<TY: Number + Ord + Unsigned> MultinomialNBDistribution<TY> {
|
||||
feature_in_class_counter[class_index][idx] +=
|
||||
row_i.to_usize().ok_or_else(|| {
|
||||
Failed::fit(&format!(
|
||||
"Elements of the matrix should be convertible to usize |found|=[{row_i}]"
|
||||
"Elements of the matrix should be convertible to usize |found|=[{}]",
|
||||
row_i
|
||||
))
|
||||
})?;
|
||||
}
|
||||
|
||||
@@ -236,7 +236,8 @@ impl<TX: Number, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>, D: Distance<Vec
|
||||
|
||||
if x_n != y_n {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x should equal size of y; |x|=[{x_n}], |y|=[{y_n}]"
|
||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
||||
x_n, y_n
|
||||
)));
|
||||
}
|
||||
|
||||
|
||||
@@ -224,7 +224,8 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>, D: Distance<Vec<TX>>>
|
||||
|
||||
if x_n != y_n {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x should equal size of y; |x|=[{x_n}], |y|=[{y_n}]"
|
||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
||||
x_n, y_n
|
||||
)));
|
||||
}
|
||||
|
||||
|
||||
@@ -49,15 +49,20 @@ pub type KNNAlgorithmName = crate::algorithm::neighbour::KNNAlgorithmName;
|
||||
|
||||
/// Weight function that is used to determine estimated value.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum KNNWeightFunction {
|
||||
/// All k nearest points are weighted equally
|
||||
#[default]
|
||||
Uniform,
|
||||
/// k nearest points are weighted by the inverse of their distance. Closer neighbors will have a greater influence than neighbors which are further away.
|
||||
Distance,
|
||||
}
|
||||
|
||||
impl Default for KNNWeightFunction {
|
||||
fn default() -> Self {
|
||||
KNNWeightFunction::Uniform
|
||||
}
|
||||
}
|
||||
|
||||
impl KNNWeightFunction {
|
||||
fn calc_weights(&self, distances: Vec<f64>) -> std::vec::Vec<f64> {
|
||||
match *self {
|
||||
|
||||
+3
-26
@@ -2,13 +2,9 @@
|
||||
//! Most algorithms in `smartcore` rely on basic linear algebra operations like dot product, matrix decomposition and other subroutines that are defined for a set of real numbers, ℝ.
|
||||
//! This module defines real number and some useful functions that are used in [Linear Algebra](../../linalg/index.html) module.
|
||||
|
||||
use rand::rngs::SmallRng;
|
||||
use rand::{Rng, SeedableRng};
|
||||
|
||||
use num_traits::Float;
|
||||
|
||||
use crate::numbers::basenum::Number;
|
||||
use crate::rand_custom::get_rng_impl;
|
||||
|
||||
/// Defines real number
|
||||
/// <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_CHTML"></script>
|
||||
@@ -67,12 +63,8 @@ impl RealNumber for f64 {
|
||||
}
|
||||
|
||||
fn rand() -> f64 {
|
||||
let mut small_rng = get_rng_impl(None);
|
||||
|
||||
let mut rngs: Vec<SmallRng> = (0..3)
|
||||
.map(|_| SmallRng::from_rng(&mut small_rng).unwrap())
|
||||
.collect();
|
||||
rngs[0].gen::<f64>()
|
||||
// TODO: to be implemented, see issue smartcore#214
|
||||
1.0
|
||||
}
|
||||
|
||||
fn two() -> Self {
|
||||
@@ -116,12 +108,7 @@ impl RealNumber for f32 {
|
||||
}
|
||||
|
||||
fn rand() -> f32 {
|
||||
let mut small_rng = get_rng_impl(None);
|
||||
|
||||
let mut rngs: Vec<SmallRng> = (0..3)
|
||||
.map(|_| SmallRng::from_rng(&mut small_rng).unwrap())
|
||||
.collect();
|
||||
rngs[0].gen::<f32>()
|
||||
1.0
|
||||
}
|
||||
|
||||
fn two() -> Self {
|
||||
@@ -162,14 +149,4 @@ mod tests {
|
||||
fn f64_from_string() {
|
||||
assert_eq!(f64::from_str("1.111111111").unwrap(), 1.111111111)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn f64_rand() {
|
||||
f64::rand();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn f32_rand() {
|
||||
f32::rand();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,13 +113,12 @@ mod tests {
|
||||
g[1] = 200. * (x[1] - x[0].powf(2.));
|
||||
};
|
||||
|
||||
let ls: Backtracking<f64> = Backtracking::<f64> {
|
||||
order: FunctionOrder::THIRD,
|
||||
..Default::default()
|
||||
};
|
||||
let mut ls: Backtracking<f64> = Default::default();
|
||||
ls.order = FunctionOrder::THIRD;
|
||||
let optimizer: GradientDescent = Default::default();
|
||||
|
||||
let result = optimizer.optimize(&f, &df, &x0, &ls);
|
||||
println!("{:?}", result);
|
||||
|
||||
assert!((result.f_x - 0.0).abs() < 1e-5);
|
||||
assert!((result.x[0] - 1.0).abs() < 1e-2);
|
||||
|
||||
@@ -196,9 +196,9 @@ impl LBFGS {
|
||||
}
|
||||
|
||||
///
|
||||
fn update_hessian<T: FloatNumber, X: Array1<T>>(
|
||||
fn update_hessian<'a, T: FloatNumber, X: Array1<T>>(
|
||||
&self,
|
||||
_: &DF<'_, X>,
|
||||
_: &'a DF<'_, X>,
|
||||
state: &mut LBFGSState<T, X>,
|
||||
) {
|
||||
state.dg = state.x_df.sub(&state.x_df_prev);
|
||||
@@ -291,10 +291,8 @@ mod tests {
|
||||
g[0] = -2. * (1. - x[0]) - 400. * (x[1] - x[0].powf(2.)) * x[0];
|
||||
g[1] = 200. * (x[1] - x[0].powf(2.));
|
||||
};
|
||||
let ls: Backtracking<f64> = Backtracking::<f64> {
|
||||
order: FunctionOrder::THIRD,
|
||||
..Default::default()
|
||||
};
|
||||
let mut ls: Backtracking<f64> = Default::default();
|
||||
ls.order = FunctionOrder::THIRD;
|
||||
let optimizer: LBFGS = Default::default();
|
||||
|
||||
let result = optimizer.optimize(&f, &df, &x0, &ls);
|
||||
|
||||
@@ -132,7 +132,8 @@ impl OneHotEncoder {
|
||||
data.copy_col_as_vec(idx, &mut col_buf);
|
||||
if !validate_col_is_categorical(&col_buf) {
|
||||
let msg = format!(
|
||||
"Column {idx} of data matrix containts non categorizable (integer) values"
|
||||
"Column {} of data matrix containts non categorizable (integer) values",
|
||||
idx
|
||||
);
|
||||
return Err(Failed::fit(&msg[..]));
|
||||
}
|
||||
@@ -181,7 +182,7 @@ impl OneHotEncoder {
|
||||
match oh_vec {
|
||||
None => {
|
||||
// Since we support T types, bad value in a series causes in to be invalid
|
||||
let msg = format!("At least one value in column {old_cidx} doesn't conform to category definition");
|
||||
let msg = format!("At least one value in column {} doesn't conform to category definition", old_cidx);
|
||||
return Err(Failed::transform(&msg[..]));
|
||||
}
|
||||
Some(v) => {
|
||||
@@ -337,7 +338,11 @@ mod tests {
|
||||
]);
|
||||
|
||||
let params = OneHotEncoderParams::from_cat_idx(&[1]);
|
||||
let result = OneHotEncoder::fit(&m, params);
|
||||
assert!(result.is_err());
|
||||
match OneHotEncoder::fit(&m, params) {
|
||||
Err(_) => {
|
||||
assert!(true);
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,7 +294,7 @@ mod tests {
|
||||
&[0.5708488802, 0.1846414616, 0.9590802982, 0.5591871046],
|
||||
&[0.8387612750, 0.5754861361, 0.5537109852, 0.1077646442],
|
||||
]));
|
||||
println!("{transformed_values}");
|
||||
println!("{}", transformed_values);
|
||||
assert!(transformed_values.approximate_eq(
|
||||
&DenseMatrix::from_2d_array(&[
|
||||
&[-1.1154020653, -0.4031985330, 0.9284605204, -0.4271473866],
|
||||
|
||||
@@ -206,7 +206,7 @@ mod tests {
|
||||
#[test]
|
||||
fn from_categories() {
|
||||
let fake_categories: Vec<usize> = vec![1, 2, 3, 4, 5, 3, 5, 3, 1, 2, 4];
|
||||
let it = fake_categories.iter().copied();
|
||||
let it = fake_categories.iter().map(|&a| a);
|
||||
let enc = CategoryMapper::<usize>::fit_to_iter(it);
|
||||
let oh_vec: Vec<f64> = match enc.get_one_hot(&1) {
|
||||
None => panic!("Wrong categories"),
|
||||
@@ -218,8 +218,8 @@ mod tests {
|
||||
|
||||
fn build_fake_str_enc<'a>() -> CategoryMapper<&'a str> {
|
||||
let fake_category_pos = vec!["background", "dog", "cat"];
|
||||
|
||||
CategoryMapper::<&str>::from_positional_category_vec(fake_category_pos)
|
||||
let enc = CategoryMapper::<&str>::from_positional_category_vec(fake_category_pos);
|
||||
enc
|
||||
}
|
||||
#[cfg_attr(
|
||||
all(target_arch = "wasm32", not(target_os = "wasi")),
|
||||
@@ -275,7 +275,7 @@ mod tests {
|
||||
let lab = enc.invert_one_hot(res).unwrap();
|
||||
assert_eq!(lab, "dog");
|
||||
if let Err(e) = enc.invert_one_hot(vec![0.0, 0.0, 0.0]) {
|
||||
let pos_entries = "Expected a single positive entry, 0 entires found".to_string();
|
||||
let pos_entries = format!("Expected a single positive entry, 0 entires found");
|
||||
assert_eq!(e, Failed::transform(&pos_entries[..]));
|
||||
};
|
||||
}
|
||||
|
||||
+11
-5
@@ -83,7 +83,7 @@ where
|
||||
Matrix: Array2<T>,
|
||||
{
|
||||
let csv_text = read_string_from_source(source)?;
|
||||
let rows: Vec<Vec<T>> = extract_row_vectors_from_csv_text(
|
||||
let rows: Vec<Vec<T>> = extract_row_vectors_from_csv_text::<T, RowVector, Matrix>(
|
||||
&csv_text,
|
||||
&definition,
|
||||
detect_row_format(&csv_text, &definition)?,
|
||||
@@ -103,7 +103,12 @@ where
|
||||
|
||||
/// Given a string containing the contents of a csv file, extract its value
|
||||
/// into row-vectors.
|
||||
fn extract_row_vectors_from_csv_text<'a, T: Number + RealNumber + std::str::FromStr>(
|
||||
fn extract_row_vectors_from_csv_text<
|
||||
'a,
|
||||
T: Number + RealNumber + std::str::FromStr,
|
||||
RowVector: Array1<T>,
|
||||
Matrix: Array2<T>,
|
||||
>(
|
||||
csv_text: &'a str,
|
||||
definition: &'a CSVDefinition<'_>,
|
||||
row_format: CSVRowFormat<'_>,
|
||||
@@ -162,7 +167,7 @@ where
|
||||
}
|
||||
|
||||
/// Ensure that a string containing a csv row conforms to a specified row format.
|
||||
fn validate_csv_row(row: &str, row_format: &CSVRowFormat<'_>) -> Result<(), ReadingError> {
|
||||
fn validate_csv_row<'a>(row: &'a str, row_format: &CSVRowFormat<'_>) -> Result<(), ReadingError> {
|
||||
let actual_number_of_fields = row.split(row_format.field_seperator).count();
|
||||
if row_format.n_fields == actual_number_of_fields {
|
||||
Ok(())
|
||||
@@ -203,7 +208,7 @@ where
|
||||
match value_string.parse::<T>().ok() {
|
||||
Some(value) => Ok(value),
|
||||
None => Err(ReadingError::InvalidField {
|
||||
msg: format!("Value '{value_string}' could not be read.",),
|
||||
msg: format!("Value '{}' could not be read.", value_string,),
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -300,11 +305,12 @@ mod tests {
|
||||
}
|
||||
mod extract_row_vectors_from_csv_text {
|
||||
use super::super::{extract_row_vectors_from_csv_text, CSVDefinition, CSVRowFormat};
|
||||
use crate::linalg::basic::matrix::DenseMatrix;
|
||||
|
||||
#[test]
|
||||
fn read_default_csv() {
|
||||
assert_eq!(
|
||||
extract_row_vectors_from_csv_text::<f64>(
|
||||
extract_row_vectors_from_csv_text::<f64, Vec<_>, DenseMatrix<_>>(
|
||||
"column 1, column 2, column3\n1.0,2.0,3.0\n4.0,5.0,6.0",
|
||||
&CSVDefinition::default(),
|
||||
CSVRowFormat {
|
||||
|
||||
+74
-57
@@ -322,26 +322,19 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX> + 'a, Y: Array
|
||||
let (n, _) = x.shape();
|
||||
let mut y_hat: Vec<TX> = Array1::zeros(n);
|
||||
|
||||
let mut row = Vec::with_capacity(n);
|
||||
for i in 0..n {
|
||||
row.clear();
|
||||
row.extend(x.get_row(i).iterator(0).copied());
|
||||
let row_pred: TX = self.predict_for_row(&row);
|
||||
let row_pred: TX =
|
||||
self.predict_for_row(Vec::from_iterator(x.get_row(i).iterator(0).copied(), n));
|
||||
y_hat.set(i, row_pred);
|
||||
}
|
||||
|
||||
Ok(y_hat)
|
||||
}
|
||||
|
||||
fn predict_for_row(&self, x: &[TX]) -> TX {
|
||||
fn predict_for_row(&self, x: Vec<TX>) -> TX {
|
||||
let mut f = self.b.unwrap();
|
||||
|
||||
let xi: Vec<_> = x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
for i in 0..self.instances.as_ref().unwrap().len() {
|
||||
let xj: Vec<_> = self.instances.as_ref().unwrap()[i]
|
||||
.iter()
|
||||
.map(|e| e.to_f64().unwrap())
|
||||
.collect();
|
||||
f += self.w.as_ref().unwrap()[i]
|
||||
* TX::from(
|
||||
self.parameters
|
||||
@@ -350,7 +343,13 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX> + 'a, Y: Array
|
||||
.kernel
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.apply(&xi, &xj)
|
||||
.apply(
|
||||
&x.iter().map(|e| e.to_f64().unwrap()).collect(),
|
||||
&self.instances.as_ref().unwrap()[i]
|
||||
.iter()
|
||||
.map(|e| e.to_f64().unwrap())
|
||||
.collect(),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
@@ -473,12 +472,14 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
let tol = self.parameters.tol;
|
||||
let good_enough = TX::from_i32(1000).unwrap();
|
||||
|
||||
let mut x = Vec::with_capacity(n);
|
||||
for _ in 0..self.parameters.epoch {
|
||||
for i in self.permutate(n) {
|
||||
x.clear();
|
||||
x.extend(self.x.get_row(i).iterator(0).take(n).copied());
|
||||
self.process(i, &x, *self.y.get(i), &mut cache);
|
||||
self.process(
|
||||
i,
|
||||
Vec::from_iterator(self.x.get_row(i).iterator(0).copied(), n),
|
||||
*self.y.get(i),
|
||||
&mut cache,
|
||||
);
|
||||
loop {
|
||||
self.reprocess(tol, &mut cache);
|
||||
self.find_min_max_gradient();
|
||||
@@ -510,17 +511,24 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
let mut cp = 0;
|
||||
let mut cn = 0;
|
||||
|
||||
let mut x = Vec::with_capacity(n);
|
||||
for i in self.permutate(n) {
|
||||
x.clear();
|
||||
x.extend(self.x.get_row(i).iterator(0).take(n).copied());
|
||||
if *self.y.get(i) == TY::one() && cp < few {
|
||||
if self.process(i, &x, *self.y.get(i), cache) {
|
||||
if self.process(
|
||||
i,
|
||||
Vec::from_iterator(self.x.get_row(i).iterator(0).copied(), n),
|
||||
*self.y.get(i),
|
||||
cache,
|
||||
) {
|
||||
cp += 1;
|
||||
}
|
||||
} else if *self.y.get(i) == TY::from(-1).unwrap()
|
||||
&& cn < few
|
||||
&& self.process(i, &x, *self.y.get(i), cache)
|
||||
&& self.process(
|
||||
i,
|
||||
Vec::from_iterator(self.x.get_row(i).iterator(0).copied(), n),
|
||||
*self.y.get(i),
|
||||
cache,
|
||||
)
|
||||
{
|
||||
cn += 1;
|
||||
}
|
||||
@@ -531,7 +539,7 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
}
|
||||
}
|
||||
|
||||
fn process(&mut self, i: usize, x: &[TX], y: TY, cache: &mut Cache<TX, TY, X, Y>) -> bool {
|
||||
fn process(&mut self, i: usize, x: Vec<TX>, y: TY, cache: &mut Cache<TX, TY, X, Y>) -> bool {
|
||||
for j in 0..self.sv.len() {
|
||||
if self.sv[j].index == i {
|
||||
return true;
|
||||
@@ -543,14 +551,15 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
let mut cache_values: Vec<((usize, usize), TX)> = Vec::new();
|
||||
|
||||
for v in self.sv.iter() {
|
||||
let xi: Vec<_> = v.x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
let xj: Vec<_> = x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
let k = self
|
||||
.parameters
|
||||
.kernel
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.apply(&xi, &xj)
|
||||
.apply(
|
||||
&v.x.iter().map(|e| e.to_f64().unwrap()).collect(),
|
||||
&x.iter().map(|e| e.to_f64().unwrap()).collect(),
|
||||
)
|
||||
.unwrap();
|
||||
cache_values.push(((i, v.index), TX::from(k).unwrap()));
|
||||
g -= v.alpha * k;
|
||||
@@ -569,7 +578,7 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
cache.insert(v.0, v.1.to_f64().unwrap());
|
||||
}
|
||||
|
||||
let x_f64: Vec<_> = x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
let x_f64 = x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
let k_v = self
|
||||
.parameters
|
||||
.kernel
|
||||
@@ -692,10 +701,8 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
let km = sv1.k;
|
||||
let gm = sv1.grad;
|
||||
let mut best = 0f64;
|
||||
let xi: Vec<_> = sv1.x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
for i in 0..self.sv.len() {
|
||||
let v = &self.sv[i];
|
||||
let xj: Vec<_> = v.x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
let z = v.grad - gm;
|
||||
let k = cache.get(
|
||||
sv1,
|
||||
@@ -704,7 +711,10 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
.kernel
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.apply(&xi, &xj)
|
||||
.apply(
|
||||
&sv1.x.iter().map(|e| e.to_f64().unwrap()).collect(),
|
||||
&v.x.iter().map(|e| e.to_f64().unwrap()).collect(),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
let mut curv = km + v.k - 2f64 * k;
|
||||
@@ -722,12 +732,6 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
}
|
||||
}
|
||||
|
||||
let xi: Vec<_> = self.sv[idx_1]
|
||||
.x
|
||||
.iter()
|
||||
.map(|e| e.to_f64().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
idx_2.map(|idx_2| {
|
||||
(
|
||||
idx_1,
|
||||
@@ -738,12 +742,16 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.apply(
|
||||
&xi,
|
||||
&self.sv[idx_1]
|
||||
.x
|
||||
.iter()
|
||||
.map(|e| e.to_f64().unwrap())
|
||||
.collect(),
|
||||
&self.sv[idx_2]
|
||||
.x
|
||||
.iter()
|
||||
.map(|e| e.to_f64().unwrap())
|
||||
.collect::<Vec<_>>(),
|
||||
.collect(),
|
||||
)
|
||||
.unwrap()
|
||||
}),
|
||||
@@ -757,11 +765,8 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
let km = sv2.k;
|
||||
let gm = sv2.grad;
|
||||
let mut best = 0f64;
|
||||
|
||||
let xi: Vec<_> = sv2.x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
for i in 0..self.sv.len() {
|
||||
let v = &self.sv[i];
|
||||
let xj: Vec<_> = v.x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
let z = gm - v.grad;
|
||||
let k = cache.get(
|
||||
sv2,
|
||||
@@ -770,7 +775,10 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
.kernel
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.apply(&xi, &xj)
|
||||
.apply(
|
||||
&sv2.x.iter().map(|e| e.to_f64().unwrap()).collect(),
|
||||
&v.x.iter().map(|e| e.to_f64().unwrap()).collect(),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
let mut curv = km + v.k - 2f64 * k;
|
||||
@@ -789,12 +797,6 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
}
|
||||
}
|
||||
|
||||
let xj: Vec<_> = self.sv[idx_2]
|
||||
.x
|
||||
.iter()
|
||||
.map(|e| e.to_f64().unwrap())
|
||||
.collect();
|
||||
|
||||
idx_1.map(|idx_1| {
|
||||
(
|
||||
idx_1,
|
||||
@@ -809,8 +811,12 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
.x
|
||||
.iter()
|
||||
.map(|e| e.to_f64().unwrap())
|
||||
.collect::<Vec<_>>(),
|
||||
&xj,
|
||||
.collect(),
|
||||
&self.sv[idx_2]
|
||||
.x
|
||||
.iter()
|
||||
.map(|e| e.to_f64().unwrap())
|
||||
.collect(),
|
||||
)
|
||||
.unwrap()
|
||||
}),
|
||||
@@ -829,12 +835,12 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
.x
|
||||
.iter()
|
||||
.map(|e| e.to_f64().unwrap())
|
||||
.collect::<Vec<_>>(),
|
||||
.collect(),
|
||||
&self.sv[idx_2]
|
||||
.x
|
||||
.iter()
|
||||
.map(|e| e.to_f64().unwrap())
|
||||
.collect::<Vec<_>>(),
|
||||
.collect(),
|
||||
)
|
||||
.unwrap(),
|
||||
)),
|
||||
@@ -889,10 +895,7 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
self.sv[v1].alpha -= step.to_f64().unwrap();
|
||||
self.sv[v2].alpha += step.to_f64().unwrap();
|
||||
|
||||
let xi_v1: Vec<_> = self.sv[v1].x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
let xi_v2: Vec<_> = self.sv[v2].x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
for i in 0..self.sv.len() {
|
||||
let xj: Vec<_> = self.sv[i].x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
let k2 = cache.get(
|
||||
&self.sv[v2],
|
||||
&self.sv[i],
|
||||
@@ -900,7 +903,10 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
.kernel
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.apply(&xi_v2, &xj)
|
||||
.apply(
|
||||
&self.sv[v2].x.iter().map(|e| e.to_f64().unwrap()).collect(),
|
||||
&self.sv[i].x.iter().map(|e| e.to_f64().unwrap()).collect(),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
let k1 = cache.get(
|
||||
@@ -910,7 +916,10 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>
|
||||
.kernel
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.apply(&xi_v1, &xj)
|
||||
.apply(
|
||||
&self.sv[v1].x.iter().map(|e| e.to_f64().unwrap()).collect(),
|
||||
&self.sv[i].x.iter().map(|e| e.to_f64().unwrap()).collect(),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
self.sv[i].grad -= step.to_f64().unwrap() * (k2 - k1);
|
||||
@@ -974,7 +983,11 @@ mod tests {
|
||||
.unwrap();
|
||||
let acc = accuracy(&y, &(y_hat.iter().map(|e| e.to_i32().unwrap()).collect()));
|
||||
|
||||
assert!(acc >= 0.9, "accuracy ({acc}) is not larger or equal to 0.9");
|
||||
assert!(
|
||||
acc >= 0.9,
|
||||
"accuracy ({}) is not larger or equal to 0.9",
|
||||
acc
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
@@ -1063,7 +1076,11 @@ mod tests {
|
||||
|
||||
let acc = accuracy(&y, &(y_hat.iter().map(|e| e.to_i32().unwrap()).collect()));
|
||||
|
||||
assert!(acc >= 0.9, "accuracy ({acc}) is not larger or equal to 0.9");
|
||||
assert!(
|
||||
acc >= 0.9,
|
||||
"accuracy ({}) is not larger or equal to 0.9",
|
||||
acc
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
|
||||
+10
-8
@@ -248,20 +248,19 @@ impl<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> SVR<'
|
||||
|
||||
let mut y_hat: Vec<T> = Vec::<T>::zeros(n);
|
||||
|
||||
let mut x_i = Vec::with_capacity(n);
|
||||
for i in 0..n {
|
||||
x_i.clear();
|
||||
x_i.extend(x.get_row(i).iterator(0).copied());
|
||||
y_hat.set(i, self.predict_for_row(&x_i));
|
||||
y_hat.set(
|
||||
i,
|
||||
self.predict_for_row(Vec::from_iterator(x.get_row(i).iterator(0).copied(), n)),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(y_hat)
|
||||
}
|
||||
|
||||
pub(crate) fn predict_for_row(&self, x: &[T]) -> T {
|
||||
pub(crate) fn predict_for_row(&self, x: Vec<T>) -> T {
|
||||
let mut f = self.b;
|
||||
|
||||
let xi: Vec<_> = x.iter().map(|e| e.to_f64().unwrap()).collect();
|
||||
for i in 0..self.instances.as_ref().unwrap().len() {
|
||||
f += self.w.as_ref().unwrap()[i]
|
||||
* T::from(
|
||||
@@ -271,7 +270,10 @@ impl<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> SVR<'
|
||||
.kernel
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.apply(&xi, &self.instances.as_ref().unwrap()[i])
|
||||
.apply(
|
||||
&x.iter().map(|e| e.to_f64().unwrap()).collect(),
|
||||
&self.instances.as_ref().unwrap()[i],
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
@@ -660,7 +662,7 @@ mod tests {
|
||||
.unwrap();
|
||||
|
||||
let t = mean_squared_error(&y_hat, &y);
|
||||
println!("{t:?}");
|
||||
println!("{:?}", t);
|
||||
assert!(t < 2.5);
|
||||
}
|
||||
|
||||
|
||||
@@ -137,17 +137,16 @@ impl<TX: Number + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>>
|
||||
self.classes.as_ref()
|
||||
}
|
||||
/// Get depth of tree
|
||||
pub fn depth(&self) -> u16 {
|
||||
fn depth(&self) -> u16 {
|
||||
self.depth
|
||||
}
|
||||
}
|
||||
|
||||
/// The function to measure the quality of a split.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum SplitCriterion {
|
||||
/// [Gini index](../decision_tree_classifier/index.html)
|
||||
#[default]
|
||||
Gini,
|
||||
/// [Entropy](../decision_tree_classifier/index.html)
|
||||
Entropy,
|
||||
@@ -155,6 +154,12 @@ pub enum SplitCriterion {
|
||||
ClassificationError,
|
||||
}
|
||||
|
||||
impl Default for SplitCriterion {
|
||||
fn default() -> Self {
|
||||
SplitCriterion::Gini
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
struct Node {
|
||||
@@ -538,10 +543,6 @@ impl<TX: Number + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>>
|
||||
parameters: DecisionTreeClassifierParameters,
|
||||
) -> Result<DecisionTreeClassifier<TX, TY, X, Y>, Failed> {
|
||||
let (x_nrows, num_attributes) = x.shape();
|
||||
if x_nrows != y.shape() {
|
||||
return Err(Failed::fit("Size of x should equal size of y"));
|
||||
}
|
||||
|
||||
let samples = vec![1; x_nrows];
|
||||
DecisionTreeClassifier::fit_weak_learner(x, y, samples, num_attributes, parameters)
|
||||
}
|
||||
@@ -559,7 +560,8 @@ impl<TX: Number + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>>
|
||||
let k = classes.len();
|
||||
if k < 2 {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Incorrect number of classes: {k}. Should be >= 2."
|
||||
"Incorrect number of classes: {}. Should be >= 2.",
|
||||
k
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -899,13 +901,15 @@ mod tests {
|
||||
)]
|
||||
#[test]
|
||||
fn gini_impurity() {
|
||||
assert!((impurity(&SplitCriterion::Gini, &[7, 3], 10) - 0.42).abs() < std::f64::EPSILON);
|
||||
assert!(
|
||||
(impurity(&SplitCriterion::Entropy, &[7, 3], 10) - 0.8812908992306927).abs()
|
||||
(impurity(&SplitCriterion::Gini, &vec![7, 3], 10) - 0.42).abs() < std::f64::EPSILON
|
||||
);
|
||||
assert!(
|
||||
(impurity(&SplitCriterion::Entropy, &vec![7, 3], 10) - 0.8812908992306927).abs()
|
||||
< std::f64::EPSILON
|
||||
);
|
||||
assert!(
|
||||
(impurity(&SplitCriterion::ClassificationError, &[7, 3], 10) - 0.3).abs()
|
||||
(impurity(&SplitCriterion::ClassificationError, &vec![7, 3], 10) - 0.3).abs()
|
||||
< std::f64::EPSILON
|
||||
);
|
||||
}
|
||||
@@ -967,17 +971,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_random_matrix_with_wrong_rownum() {
|
||||
let x_rand: DenseMatrix<f64> = DenseMatrix::<f64>::rand(21, 200);
|
||||
|
||||
let y: Vec<u32> = vec![0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
|
||||
|
||||
let fail = DecisionTreeClassifier::fit(&x_rand, &y, Default::default());
|
||||
|
||||
assert!(fail.is_err());
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
all(target_arch = "wasm32", not(target_os = "wasi")),
|
||||
wasm_bindgen_test::wasm_bindgen_test
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
//! Example:
|
||||
//!
|
||||
//! ```
|
||||
//! use rand::thread_rng;
|
||||
//! use smartcore::linalg::basic::matrix::DenseMatrix;
|
||||
//! use smartcore::tree::decision_tree_regressor::*;
|
||||
//!
|
||||
@@ -421,10 +422,6 @@ impl<TX: Number + PartialOrd, TY: Number, X: Array2<TX>, Y: Array1<TY>>
|
||||
parameters: DecisionTreeRegressorParameters,
|
||||
) -> Result<DecisionTreeRegressor<TX, TY, X, Y>, Failed> {
|
||||
let (x_nrows, num_attributes) = x.shape();
|
||||
if x_nrows != y.shape() {
|
||||
return Err(Failed::fit("Size of x should equal size of y"));
|
||||
}
|
||||
|
||||
let samples = vec![1; x_nrows];
|
||||
DecisionTreeRegressor::fit_weak_learner(x, y, samples, num_attributes, parameters)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user