# ml-ransac

> Fit a model to noisy data by excluding outliers. This is an implementation of the RANSAC algorithm.

Latest version **1.0.0** (published 2023-06-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install ml-ransac
pnpm add ml-ransac
yarn add ml-ransac
bun add ml-ransac
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2023-06-08 |
| First published | 2023-06-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 53.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Océane Patiny |
| Maintainers | mljs-bot |

## Links

- npm: https://www.npmjs.com/package/ml-ransac
- Repository: https://github.com/mljs/ransac
- Homepage: https://github.com/mljs/ransac#readme
- Issues: https://github.com/mljs/ransac/issues
- npm.io page: https://npm.io/package/ml-ransac

## Dependencies (3)

- [ml-matrix](https://npm.io/package/ml-matrix.md) ^6.10.4
- [ml-random](https://npm.io/package/ml-random.md) ^0.5.0
- [ml-array-median](https://npm.io/package/ml-array-median.md) ^1.1.6

## Recent versions

- 1.0.0 (latest) — 2023-06-08

## README

# ml-ransac

[![NPM version][npm-image]][npm-url]
[![build status][ci-image]][ci-url]
[![Test coverage][codecov-image]][codecov-url]
[![npm download][download-image]][download-url]

Fit a model to noisy data by excluding outliers. This is an implementation of the RANSAC algorithm..

## Installation

`$ npm i ml-ransac`

## Examples

Finding the best linear regression which doesn't take the outliers into account:

```js
import { levenbergMarquardt } from 'ml-levenberg-marquardt'; // library allowing you to find a regression of any type
import { ransac } from 'ml-ransac';

// defining the model function: a line
export type LineFunction = (x: number) => number;

export function line([a, b]: number[]): LineFunction {
  return (x: number) => a * x + b;
}

// defining the fit function: we create a linear regression using levenberg-maquard
export interface LinearRegressionOptions {
  /**
   * Initial parameter values.
   *
   * @default [0,0]
   */
  initialValues?: number[];
}

export function linearRegression(
  source: number[],
  destination: number[],
  options: LinearRegressionOptions = {},
): number[] {
  const { initialValues = [0, 0] } = options;
  const result = levenbergMarquardt({ x: source, y: destination }, line, {
    initialValues,
  });

  return result.parameterValues;
}

// data
const source = [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const destination = [-6, -2, 0, 5, 0, 0, -1, 1, 0, 0, -1, 0, 3];

// finding the best model
const result = ransac(source, destination, {
  distanceFunction,
  fitFunction,
  modelFunction,
  threshold: 1.1,
  sampleSize: 2,
  maxNbIterations: 10,
  seed: 0,
});

// result is :
//   {
//     nbIterations: 10,
//     modelParameters: [0, 0],
//     inliers: [2, 4, 5, 6, 7, 8, 9, 10, 11],
//     error: 1,
//   }
```

## License

[MIT](./LICENSE)

[npm-image]: https://img.shields.io/npm/v/ml-ransac.svg
[npm-url]: https://www.npmjs.com/package/ml-ransac
[ci-image]: https://github.com/mljs/ransac/workflows/Node.js%20CI/badge.svg?branch=main
[ci-url]: https://github.com/mljs/ransac/actions?query=workflow%3A%22Node.js+CI%22
[codecov-image]: https://img.shields.io/codecov/c/github/mljs/ransac.svg
[codecov-url]: https://codecov.io/gh/mljs/ransac
[download-image]: https://img.shields.io/npm/dm/ml-ransac.svg
[download-url]: https://www.npmjs.com/package/ml-ransac

---
_Source: https://npm.io/package/ml-ransac · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
