Skip to content

Commit dd341f4

Browse files
Volodymyr OrlovVolodymyr Orlov
authored andcommitted
feat: + builders for algorithm parameters
1 parent 74f0d9e commit dd341f4

17 files changed

+276
-8
lines changed

src/cluster/dbscan.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,32 @@ pub struct DBSCAN<T: RealNumber, D: Distance<Vec<T>, T>> {
5353
#[derive(Debug, Clone)]
5454
/// DBSCAN clustering algorithm parameters
5555
pub struct DBSCANParameters<T: RealNumber> {
56-
/// Maximum number of iterations of the k-means algorithm for a single run.
56+
/// The number of samples (or total weight) in a neighborhood for a point to be considered as a core point.
5757
pub min_samples: usize,
58-
/// The number of samples in a neighborhood for a point to be considered as a core point.
58+
/// The maximum distance between two samples for one to be considered as in the neighborhood of the other.
5959
pub eps: T,
6060
/// KNN algorithm to use.
6161
pub algorithm: KNNAlgorithmName,
6262
}
6363

64+
impl<T: RealNumber> DBSCANParameters<T> {
65+
/// The number of samples (or total weight) in a neighborhood for a point to be considered as a core point.
66+
pub fn with_min_samples(mut self, min_samples: usize) -> Self {
67+
self.min_samples = min_samples;
68+
self
69+
}
70+
/// The maximum distance between two samples for one to be considered as in the neighborhood of the other.
71+
pub fn with_eps(mut self, eps: T) -> Self {
72+
self.eps = eps;
73+
self
74+
}
75+
/// KNN algorithm to use.
76+
pub fn with_algorithm(mut self, algorithm: KNNAlgorithmName) -> Self {
77+
self.algorithm = algorithm;
78+
self
79+
}
80+
}
81+
6482
impl<T: RealNumber, D: Distance<Vec<T>, T>> PartialEq for DBSCAN<T, D> {
6583
fn eq(&self, other: &Self) -> bool {
6684
self.cluster_labels.len() == other.cluster_labels.len()

src/cluster/kmeans.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ pub struct KMeansParameters {
105105
pub max_iter: usize,
106106
}
107107

108+
impl KMeansParameters {
109+
/// Maximum number of iterations of the k-means algorithm for a single run.
110+
pub fn with_max_iter(mut self, max_iter: usize) -> Self {
111+
self.max_iter = max_iter;
112+
self
113+
}
114+
}
115+
108116
impl Default for KMeansParameters {
109117
fn default() -> Self {
110118
KMeansParameters { max_iter: 100 }

src/decomposition/pca.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ pub struct PCAParameters {
8888
pub use_correlation_matrix: bool,
8989
}
9090

91+
impl PCAParameters {
92+
/// By default, covariance matrix is used to compute principal components.
93+
/// Enable this flag if you want to use correlation matrix instead.
94+
pub fn with_use_correlation_matrix(mut self, use_correlation_matrix: bool) -> Self {
95+
self.use_correlation_matrix = use_correlation_matrix;
96+
self
97+
}
98+
}
99+
91100
impl Default for PCAParameters {
92101
fn default() -> Self {
93102
PCAParameters {

src/ensemble/random_forest_classifier.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,39 @@ pub struct RandomForestClassifier<T: RealNumber> {
8585
classes: Vec<T>,
8686
}
8787

88+
impl RandomForestClassifierParameters {
89+
/// Split criteria to use when building a tree. See [Decision Tree Classifier](../../tree/decision_tree_classifier/index.html)
90+
pub fn with_criterion(mut self, criterion: SplitCriterion) -> Self {
91+
self.criterion = criterion;
92+
self
93+
}
94+
/// Tree max depth. See [Decision Tree Classifier](../../tree/decision_tree_classifier/index.html)
95+
pub fn with_max_depth(mut self, max_depth: u16) -> Self {
96+
self.max_depth = Some(max_depth);
97+
self
98+
}
99+
/// The minimum number of samples required to be at a leaf node. See [Decision Tree Classifier](../../tree/decision_tree_classifier/index.html)
100+
pub fn with_min_samples_leaf(mut self, min_samples_leaf: usize) -> Self {
101+
self.min_samples_leaf = min_samples_leaf;
102+
self
103+
}
104+
/// The minimum number of samples required to split an internal node. See [Decision Tree Classifier](../../tree/decision_tree_classifier/index.html)
105+
pub fn with_min_samples_split(mut self, min_samples_split: usize) -> Self {
106+
self.min_samples_split = min_samples_split;
107+
self
108+
}
109+
/// The number of trees in the forest.
110+
pub fn with_n_trees(mut self, n_trees: u16) -> Self {
111+
self.n_trees = n_trees;
112+
self
113+
}
114+
/// Number of random sample of predictors to use as split candidates.
115+
pub fn with_m(mut self, m: usize) -> Self {
116+
self.m = Some(m);
117+
self
118+
}
119+
}
120+
88121
impl<T: RealNumber> PartialEq for RandomForestClassifier<T> {
89122
fn eq(&self, other: &Self) -> bool {
90123
if self.classes.len() != other.classes.len() || self.trees.len() != other.trees.len() {

src/ensemble/random_forest_regressor.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,34 @@ pub struct RandomForestRegressor<T: RealNumber> {
8080
trees: Vec<DecisionTreeRegressor<T>>,
8181
}
8282

83+
impl RandomForestRegressorParameters {
84+
/// Tree max depth. See [Decision Tree Classifier](../../tree/decision_tree_classifier/index.html)
85+
pub fn with_max_depth(mut self, max_depth: u16) -> Self {
86+
self.max_depth = Some(max_depth);
87+
self
88+
}
89+
/// The minimum number of samples required to be at a leaf node. See [Decision Tree Classifier](../../tree/decision_tree_classifier/index.html)
90+
pub fn with_min_samples_leaf(mut self, min_samples_leaf: usize) -> Self {
91+
self.min_samples_leaf = min_samples_leaf;
92+
self
93+
}
94+
/// The minimum number of samples required to split an internal node. See [Decision Tree Classifier](../../tree/decision_tree_classifier/index.html)
95+
pub fn with_min_samples_split(mut self, min_samples_split: usize) -> Self {
96+
self.min_samples_split = min_samples_split;
97+
self
98+
}
99+
/// The number of trees in the forest.
100+
pub fn with_n_trees(mut self, n_trees: usize) -> Self {
101+
self.n_trees = n_trees;
102+
self
103+
}
104+
/// Number of random sample of predictors to use as split candidates.
105+
pub fn with_m(mut self, m: usize) -> Self {
106+
self.m = Some(m);
107+
self
108+
}
109+
}
110+
83111
impl Default for RandomForestRegressorParameters {
84112
fn default() -> Self {
85113
RandomForestRegressorParameters {

src/linear/elastic_net.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,36 @@ pub struct ElasticNet<T: RealNumber, M: Matrix<T>> {
9090
intercept: T,
9191
}
9292

93+
impl<T: RealNumber> ElasticNetParameters<T> {
94+
/// Regularization parameter.
95+
pub fn with_alpha(mut self, alpha: T) -> Self {
96+
self.alpha = alpha;
97+
self
98+
}
99+
/// The elastic net mixing parameter, with 0 <= l1_ratio <= 1.
100+
/// For l1_ratio = 0 the penalty is an L2 penalty.
101+
/// For l1_ratio = 1 it is an L1 penalty. For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2.
102+
pub fn with_l1_ratio(mut self, l1_ratio: T) -> Self {
103+
self.l1_ratio = l1_ratio;
104+
self
105+
}
106+
/// If True, the regressors X will be normalized before regression by subtracting the mean and dividing by the standard deviation.
107+
pub fn with_normalize(mut self, normalize: bool) -> Self {
108+
self.normalize = normalize;
109+
self
110+
}
111+
/// The tolerance for the optimization
112+
pub fn with_tol(mut self, tol: T) -> Self {
113+
self.tol = tol;
114+
self
115+
}
116+
/// The maximum number of iterations
117+
pub fn with_max_iter(mut self, max_iter: usize) -> Self {
118+
self.max_iter = max_iter;
119+
self
120+
}
121+
}
122+
93123
impl<T: RealNumber> Default for ElasticNetParameters<T> {
94124
fn default() -> Self {
95125
ElasticNetParameters {

src/linear/lasso.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,29 @@ pub struct Lasso<T: RealNumber, M: Matrix<T>> {
5454
intercept: T,
5555
}
5656

57+
impl<T: RealNumber> LassoParameters<T> {
58+
/// Regularization parameter.
59+
pub fn with_alpha(mut self, alpha: T) -> Self {
60+
self.alpha = alpha;
61+
self
62+
}
63+
/// If True, the regressors X will be normalized before regression by subtracting the mean and dividing by the standard deviation.
64+
pub fn with_normalize(mut self, normalize: bool) -> Self {
65+
self.normalize = normalize;
66+
self
67+
}
68+
/// The tolerance for the optimization
69+
pub fn with_tol(mut self, tol: T) -> Self {
70+
self.tol = tol;
71+
self
72+
}
73+
/// The maximum number of iterations
74+
pub fn with_max_iter(mut self, max_iter: usize) -> Self {
75+
self.max_iter = max_iter;
76+
self
77+
}
78+
}
79+
5780
impl<T: RealNumber> Default for LassoParameters<T> {
5881
fn default() -> Self {
5982
LassoParameters {

src/linear/linear_regression.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ pub struct LinearRegression<T: RealNumber, M: Matrix<T>> {
9393
solver: LinearRegressionSolverName,
9494
}
9595

96+
impl LinearRegressionParameters {
97+
/// Solver to use for estimation of regression coefficients.
98+
pub fn with_solver(mut self, solver: LinearRegressionSolverName) -> Self {
99+
self.solver = solver;
100+
self
101+
}
102+
}
103+
96104
impl Default for LinearRegressionParameters {
97105
fn default() -> Self {
98106
LinearRegressionParameters {

src/linear/ridge_regression.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ pub struct RidgeRegression<T: RealNumber, M: Matrix<T>> {
9898
solver: RidgeRegressionSolverName,
9999
}
100100

101+
impl<T: RealNumber> RidgeRegressionParameters<T> {
102+
/// Regularization parameter.
103+
pub fn with_alpha(mut self, alpha: T) -> Self {
104+
self.alpha = alpha;
105+
self
106+
}
107+
/// Solver to use for estimation of regression coefficients.
108+
pub fn with_solver(mut self, solver: RidgeRegressionSolverName) -> Self {
109+
self.solver = solver;
110+
self
111+
}
112+
/// If True, the regressors X will be normalized before regression by subtracting the mean and dividing by the standard deviation.
113+
pub fn with_normalize(mut self, normalize: bool) -> Self {
114+
self.normalize = normalize;
115+
self
116+
}
117+
}
118+
101119
impl<T: RealNumber> Default for RidgeRegressionParameters<T> {
102120
fn default() -> Self {
103121
RidgeRegressionParameters {

src/naive_bayes/bernoulli.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ impl<T: RealNumber> BernoulliNBParameters<T> {
9696
binarize,
9797
}
9898
}
99+
/// Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
100+
pub fn with_alpha(mut self, alpha: T) -> Self {
101+
self.alpha = alpha;
102+
self
103+
}
104+
/// Prior probabilities of the classes. If specified the priors are not adjusted according to the data
105+
pub fn with_priors(mut self, priors: Vec<T>) -> Self {
106+
self.priors = Some(priors);
107+
self
108+
}
109+
/// Threshold for binarizing (mapping to booleans) of sample features. If None, input is presumed to already consist of binary vectors.
110+
pub fn with_binarize(mut self, binarize: T) -> Self {
111+
self.binarize = Some(binarize);
112+
self
113+
}
99114
}
100115

101116
impl<T: RealNumber> Default for BernoulliNBParameters<T> {

0 commit comments

Comments
 (0)