https://github.com/mljs/ransac
https://github.com/mljs/ransac
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mljs/ransac
- Owner: mljs
- License: mit
- Created: 2023-03-23T15:40:20.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-08T14:59:38.000Z (about 3 years ago)
- Last Synced: 2025-08-09T10:58:46.574Z (11 months ago)
- Language: TypeScript
- Size: 99.6 KB
- Stars: 0
- Watchers: 10
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
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