rusty_machine/lib.rs
1//! # The rusty-machine crate.
2//!
3//! A crate built for machine learning that works out-of-the-box.
4//!
5//! ---
6//!
7//! ## Structure
8//!
9//! The crate is made up of two primary modules: learning and linalg.
10//!
11//! ### learning
12//!
13//! The learning module contains all of the machine learning modules.
14//! This means the algorithms, models and related tools.
15//!
16//! The currently supported techniques are:
17//!
18//! - Linear Regression
19//! - Logistic Regression
20//! - Generalized Linear Models
21//! - K-Means Clustering
22//! - Neural Networks
23//! - Gaussian Process Regression
24//! - Support Vector Machines
25//! - Gaussian Mixture Models
26//! - Naive Bayes Classifiers
27//! - DBSCAN
28//!
29//! ### linalg
30//!
31//! The linalg module reexports some structs and traits from the
32//! [rulinalg](https://crates.io/crates/rulinalg) crate. This is to provide
33//! easy access to common linear algebra tools within this library.
34//!
35//! ---
36//!
37//! ## Usage
38//!
39//! Specific usage of modules is described within the modules themselves. This section
40//! will focus on the general workflow for this library.
41//!
42//! The models contained within the learning module should implement either
43//! `SupModel` or `UnSupModel`. These both provide a `train` and a `predict`
44//! function which provide an interface to the model.
45//!
46//! You should instantiate the model, with your chosen options and then train using
47//! the training data. Followed by predicting with your test data. *For now*
48//! cross-validation, data handling, and many other things are left explicitly
49//! to the user.
50//!
51//! Here is an example usage for Gaussian Process Regression:
52//!
53//! ```
54//! use rusty_machine::linalg::Matrix;
55//! use rusty_machine::linalg::Vector;
56//! use rusty_machine::learning::gp::GaussianProcess;
57//! use rusty_machine::learning::gp::ConstMean;
58//! use rusty_machine::learning::toolkit::kernel;
59//! use rusty_machine::learning::SupModel;
60//!
61//! // First we'll get some data.
62//!
63//! // Some example training data.
64//! let inputs = Matrix::new(3,3,vec![1.,1.,1.,2.,2.,2.,3.,3.,3.]);
65//! let targets = Vector::new(vec![0.,1.,0.]);
66//!
67//! // Some example test data.
68//! let test_inputs = Matrix::new(2,3, vec![1.5,1.5,1.5,2.5,2.5,2.5]);
69//!
70//! // Now we'll set up our model.
71//! // This is close to the most complicated a model in rusty-machine gets!
72//!
73//! // A squared exponential kernel with lengthscale 2, and amplitude 1.
74//! let ker = kernel::SquaredExp::new(2., 1.);
75//!
76//! // The zero function
77//! let zero_mean = ConstMean::default();
78//!
79//! // Construct a GP with the specified kernel, mean, and a noise of 0.5.
80//! let mut gp = GaussianProcess::new(ker, zero_mean, 0.5);
81//!
82//!
83//! // Now we can train and predict from the model.
84//!
85//! // Train the model!
86//! gp.train(&inputs, &targets).unwrap();
87//!
88//! // Predict output from test datae]
89//! let outputs = gp.predict(&test_inputs).unwrap();
90//! ```
91//!
92//! This code could have been a lot simpler if we had simply adopted
93//! `let mut gp = GaussianProcess::default();`. Conversely, you could also implement
94//! your own kernels and mean functions by using the appropriate traits.
95//!
96//! Additionally you'll notice there's quite a few `use` statements at the top of this code.
97//! We can remove some of these by utilizing the `prelude`:
98//!
99//! ```
100//! use rusty_machine::prelude::*;
101//!
102//! let _ = Matrix::new(2,2,vec![2.0;4]);
103//! ```
104
105#![deny(missing_docs)]
106#![warn(missing_debug_implementations)]
107
108#[macro_use]
109extern crate rulinalg;
110extern crate num as libnum;
111extern crate rand;
112
113pub mod prelude;
114
115/// The linear algebra module
116///
117/// This module contains reexports of common tools from the rulinalg crate.
118pub mod linalg {
119 pub use rulinalg::matrix::{Axes, Matrix, MatrixSlice, MatrixSliceMut, BaseMatrix, BaseMatrixMut};
120 pub use rulinalg::vector::Vector;
121 pub use rulinalg::Metric;
122}
123
124/// Module for data handling
125pub mod data {
126 pub mod transforms;
127}
128
129/// Module for machine learning.
130pub mod learning {
131 pub mod dbscan;
132 pub mod glm;
133 pub mod gmm;
134 pub mod lin_reg;
135 pub mod logistic_reg;
136 pub mod k_means;
137 pub mod nnet;
138 pub mod gp;
139 pub mod svm;
140 pub mod naive_bayes;
141
142 pub mod error;
143
144 /// A new type which provides clean access to the learning errors
145 pub type LearningResult<T> = Result<T, error::Error>;
146
147 /// Trait for supervised model.
148 pub trait SupModel<T, U> {
149 /// Predict output from inputs.
150 fn predict(&self, inputs: &T) -> LearningResult<U>;
151
152 /// Train the model using inputs and targets.
153 fn train(&mut self, inputs: &T, targets: &U) -> LearningResult<()>;
154 }
155
156 /// Trait for unsupervised model.
157 pub trait UnSupModel<T, U> {
158 /// Predict output from inputs.
159 fn predict(&self, inputs: &T) -> LearningResult<U>;
160
161 /// Train the model using inputs.
162 fn train(&mut self, inputs: &T) -> LearningResult<()>;
163 }
164
165 /// Module for optimization in machine learning setting.
166 pub mod optim {
167
168 /// Trait for models which can be gradient-optimized.
169 pub trait Optimizable {
170 /// The input data type to the model.
171 type Inputs;
172 /// The target data type to the model.
173 type Targets;
174
175 /// Compute the gradient for the model.
176 fn compute_grad(&self,
177 params: &[f64],
178 inputs: &Self::Inputs,
179 targets: &Self::Targets)
180 -> (f64, Vec<f64>);
181 }
182
183 /// Trait for optimization algorithms.
184 pub trait OptimAlgorithm<M: Optimizable> {
185 /// Return the optimized parameter using gradient optimization.
186 ///
187 /// Takes in a set of starting parameters and related model data.
188 fn optimize(&self,
189 model: &M,
190 start: &[f64],
191 inputs: &M::Inputs,
192 targets: &M::Targets)
193 -> Vec<f64>;
194 }
195
196 pub mod grad_desc;
197 pub mod fmincg;
198 }
199
200 /// Module for learning tools.
201 pub mod toolkit {
202 pub mod activ_fn;
203 pub mod cost_fn;
204 pub mod kernel;
205 pub mod rand_utils;
206 pub mod regularization;
207 }
208}
209
210#[cfg(feature = "stats")]
211/// Module for computational statistics
212pub mod stats {
213
214 /// Module for statistical distributions.
215 pub mod dist;
216}
217
218/// Module for evaluating models.
219pub mod analysis {
220 pub mod confusion_matrix;
221 pub mod cross_validation;
222 pub mod score;
223}
224
225#[cfg(feature = "datasets")]
226/// Module for datasets.
227pub mod datasets;