#ndarray #statistics #regression #glm #irls


Performs regression for generalized linear models using IRLS on data stored in arrays

8 releases

new 0.0.8 Feb 21, 2021
0.0.7 Aug 2, 2020
0.0.6 Apr 27, 2020
0.0.1 Mar 20, 2020

#69 in Science

Download history 11/week @ 2020-11-06 1/week @ 2020-11-20 11/week @ 2020-11-27 8/week @ 2020-12-04 7/week @ 2021-01-01 8/week @ 2021-01-15 1/week @ 2021-01-22 121/week @ 2021-02-12 28/week @ 2021-02-19

57 downloads per month

MIT license

1.5K SLoC


Rust library for solving linear, logistic, and generalized linear models through iteratively reweighted least squares, using the ndarray-linalg module.

Crate Documentation Build Status


This package is in early alpha and the interface is likely to undergo many changes. Functionality may change from one release to the next.

The regression algorithm uses iteratively re-weighted least squares (IRLS) with a step-halving procedure applied when the next iteration of guesses does not increase the likelihood.

Much of the logic is done at the type/trait level to avoid compiling code a user does not need and to allow general implementations that the compiler can optimize in trivial cases.


The recommended approach is to use a system BLAS implementation. For instance, to install OpenBLAS on Debian/Ubuntu:

sudo apt update && sudo apt install -y libopenblas-dev

Then use this crate with the openblas-system feature.

To use an alternative backend or to build a static BLAS implementation, refer to the ndarray-linalg documentation. Use this crate with the appropriate feature flag and it will be forwarded to ndarray-linalg.


To use in your crate, add the following to the Cargo.toml:

ndarray = { version = "0.14", features = ["blas"]}
ndarray-glm = { version = "0.0.8", features = ["openblas-system"] }

An example for linear regression is shown below.

use ndarray_glm::{array, Linear, ModelBuilder, standardize};

// define some test data
let data_y = array![0.3, 1.3, 0.7];
let data_x = array![[0.1, 0.2], [-0.4, 0.1], [0.2, 0.4]];
// The design matrix can optionally be standardized, where the mean of each independent
// variable is subtracted and each is then divided by the standard deviation of that variable.
let data_x = standardize(data_x);
// The interface takes `ArrayView`s to allow for efficient passing of slices.
let model = ModelBuilder::<Linear>::data(data_y.view(), data_x.view()).build()?;
// L2 (ridge) regularization can be applied with l2_reg().
let fit = model.fit_options().l2_reg(1e-5).fit()?;
// Currently the result is a simple array of the MLE estimators, including the intercept term.
println!("Fit result: {}", fit.result);

For logistic regression, the y array data must be boolean, and for Poisson regression it must be an unsigned integer.

Custom non-canonical link functions can be defined by the user, although the interface is not particularly ergonomic. See tests/custom_link.rs for examples.


  • Linear regression
  • Logistic regression
  • Generalized linear model IRLS
  • Linear offsets
  • Generic over floating point type
  • Non-float domain types
  • L2 (ridge) Regularization
  • L1 (lasso) Regularization
    • An experimental smoothed version with an epsilon tolerance is WIP
  • Other exponential family distributions
    • Poisson
    • Binomial (nightly only)
    • Exponential
    • Gamma
    • Inverse Gaussian
  • Option for data standardization/normalization
  • Weighted and correlated regressions
  • Non-canonical link functions
  • Goodness-of-fit tests


These notes on generalized linear models summarize many of the relevant concepts and provide some additional references.


~467K SLoC