-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Linear regression example #166
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e7c9381
Quick and dirty linear regression
LukeMathWalker 7f1a2e1
Basic version works
LukeMathWalker c4c78e7
Fix warnings
LukeMathWalker ea7b475
Proper generation of data and target
LukeMathWalker 00b9fd4
Tune range
LukeMathWalker 71ceb20
Polishing
LukeMathWalker ddbd5e1
Polish predict function
LukeMathWalker 208f451
Missing reference
LukeMathWalker 53f8124
Spacing
LukeMathWalker d4be0bb
Run cargo fmt
LukeMathWalker 673c41d
Fix typo in println
LukeMathWalker de20e5f
Split in two files, with proper visibility for methods on LinearRegre…
LukeMathWalker cdd3c5d
Add docs.
LukeMathWalker 85e11f8
Quote the original repo, not forks
LukeMathWalker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#![allow(non_snake_case)] | ||
use ndarray::{stack, Array, Array1, ArrayBase, Axis, Data, Ix1, Ix2}; | ||
use ndarray_linalg::Solve; | ||
|
||
/// The simple linear regression model is | ||
/// y = bX + e where e ~ N(0, sigma^2 * I) | ||
/// In probabilistic terms this corresponds to | ||
/// y - bX ~ N(0, sigma^2 * I) | ||
/// y | X, b ~ N(bX, sigma^2 * I) | ||
/// The loss for the model is simply the squared error between the model | ||
/// predictions and the true values: | ||
/// Loss = ||y - bX||^2 | ||
/// The maximum likelihood estimation for the model parameters `beta` can be computed | ||
/// in closed form via the normal equation: | ||
/// b = (X^T X)^{-1} X^T y | ||
/// where (X^T X)^{-1} X^T is known as the pseudoinverse or Moore-Penrose inverse. | ||
/// | ||
/// Adapted from: https://github.com/ddbourgin/numpy-ml | ||
pub struct LinearRegression { | ||
pub beta: Option<Array1<f64>>, | ||
fit_intercept: bool, | ||
} | ||
|
||
impl LinearRegression { | ||
pub fn new(fit_intercept: bool) -> LinearRegression { | ||
LinearRegression { | ||
beta: None, | ||
fit_intercept, | ||
} | ||
} | ||
|
||
/// Given: | ||
/// - an input matrix `X`, with shape `(n_samples, n_features)`; | ||
/// - a target variable `y`, with shape `(n_samples,)`; | ||
/// `fit` tunes the `beta` parameter of the linear regression model | ||
/// to match the training data distribution. | ||
/// | ||
/// `self` is modified in place, nothing is returned. | ||
pub fn fit<A, B>(&mut self, X: ArrayBase<A, Ix2>, y: ArrayBase<B, Ix1>) | ||
where | ||
A: Data<Elem = f64>, | ||
B: Data<Elem = f64>, | ||
{ | ||
let (n_samples, _) = X.dim(); | ||
|
||
// Check that our inputs have compatible shapes | ||
assert_eq!(y.dim(), n_samples); | ||
|
||
// If we are fitting the intercept, we need an additional column | ||
self.beta = if self.fit_intercept { | ||
let dummy_column: Array<f64, _> = Array::ones((n_samples, 1)); | ||
let X = stack(Axis(1), &[dummy_column.view(), X.view()]).unwrap(); | ||
Some(LinearRegression::solve_normal_equation(X, y)) | ||
} else { | ||
Some(LinearRegression::solve_normal_equation(X, y)) | ||
}; | ||
} | ||
|
||
/// Given an input matrix `X`, with shape `(n_samples, n_features)`, | ||
/// `predict` returns the target variable according to linear model | ||
/// learned from the training data distribution. | ||
/// | ||
/// **Panics** if `self` has not be `fit`ted before calling `predict. | ||
pub fn predict<A>(&self, X: &ArrayBase<A, Ix2>) -> Array1<f64> | ||
where | ||
A: Data<Elem = f64>, | ||
{ | ||
let (n_samples, _) = X.dim(); | ||
|
||
// If we are fitting the intercept, we need an additional column | ||
if self.fit_intercept { | ||
let dummy_column: Array<f64, _> = Array::ones((n_samples, 1)); | ||
let X = stack(Axis(1), &[dummy_column.view(), X.view()]).unwrap(); | ||
self._predict(&X) | ||
} else { | ||
self._predict(X) | ||
} | ||
} | ||
|
||
fn solve_normal_equation<A, B>(X: ArrayBase<A, Ix2>, y: ArrayBase<B, Ix1>) -> Array1<f64> | ||
where | ||
A: Data<Elem = f64>, | ||
B: Data<Elem = f64>, | ||
{ | ||
let rhs = X.t().dot(&y); | ||
let linear_operator = X.t().dot(&X); | ||
linear_operator.solve_into(rhs).unwrap() | ||
} | ||
|
||
fn _predict<A>(&self, X: &ArrayBase<A, Ix2>) -> Array1<f64> | ||
where | ||
A: Data<Elem = f64>, | ||
{ | ||
match &self.beta { | ||
None => panic!("The linear regression estimator has to be fitted first!"), | ||
Some(beta) => X.dot(beta), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#![allow(non_snake_case)] | ||
use ndarray::{Array1, Array2, Array, Axis}; | ||
use ndarray_linalg::random; | ||
use ndarray_stats::DeviationExt; | ||
use ndarray_rand::RandomExt; | ||
use rand::distributions::StandardNormal; | ||
|
||
// Import LinearRegression from other file ("module") in this example | ||
mod linear_regression; | ||
use linear_regression::LinearRegression; | ||
|
||
/// It returns a tuple: input data and the associated target variable. | ||
/// | ||
/// The target variable is a linear function of the input, perturbed by gaussian noise. | ||
fn get_data(n_samples: usize, n_features: usize) -> (Array2<f64>, Array1<f64>) { | ||
let shape = (n_samples, n_features); | ||
let noise: Array1<f64> = Array::random(n_samples, StandardNormal); | ||
|
||
let beta: Array1<f64> = random(n_features) * 10.; | ||
println!("Beta used to generate target variable: {:.3}", beta); | ||
|
||
let X: Array2<f64> = random(shape); | ||
let y: Array1<f64> = X.dot(&beta) + noise; | ||
(X, y) | ||
} | ||
|
||
pub fn main() { | ||
let n_train_samples = 5000; | ||
let n_test_samples = 1000; | ||
let n_features = 3; | ||
|
||
let (X, y) = get_data(n_train_samples + n_test_samples, n_features); | ||
let (X_train, X_test) = X.view().split_at(Axis(0), n_train_samples); | ||
let (y_train, y_test) = y.view().split_at(Axis(0), n_train_samples); | ||
|
||
let mut linear_regressor = LinearRegression::new(false); | ||
linear_regressor.fit(X_train, y_train); | ||
|
||
let test_predictions = linear_regressor.predict(&X_test); | ||
let mean_squared_error = test_predictions.mean_sq_err(&y_test.to_owned()).unwrap(); | ||
println!( | ||
"Beta estimated from the training data: {:.3}", | ||
linear_regressor.beta.unwrap() | ||
); | ||
println!( | ||
"The fitted regressor has a mean squared error of {:.3}", | ||
mean_squared_error | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
x
andy
should be refencex: &ArrayBase<A, Ix2>