Skip to content

HMath.MachineLearning.Neighbors

Module index · Numerical conventions

Description

Exact branch-and-bound KD/ball trees and budgeted approximate search. IDs always refer to original training rows. Neighbors sort by distance then ID. LSH is Euclidean p-stable Gaussian projection hashing (Datar et al. 2004). Isolation forest follows Liu et al. ICDM 2008, with seeded subsampling and the harmonic-number correction for unsplit leaves. Larger anomaly scores indicate more isolated observations. One-class SVM returns positive inlier decisions.

On this page

API Purpose
IndexKind Axis-aligned KD partitions or enclosing Euclidean ball partitions.
KDTree Declaration and type contract below.
BallTree Declaration and type contract below.
SpatialIndex Immutable tree retaining original observation IDs and its validated feature dimension.
buildIndex Build an exact search tree with at most leafSize observations per leaf.
NeighborResult Neighbor indices/distances plus visited-leaf and exhaustive-search diagnostics.
NeighborResult Declaration and type contract below.
neighbors Training-row indices paired with returned Euclidean distances, ordered by distance and then index.
visitedLeaves Number of leaves visited against the explicit query budget.
exhaustive True if the neighbor search completed its exact stopping condition; False if unexplored nodes may improve candidates.
nearestNeighbors maxLeaves is a query work budget.
knnRegress Average the k exact nearest training responses for each query row.
knnClassify Majority vote among k exact neighbors; targets follow original training row order.
LSH Seeded p-stable Euclidean hash tables retaining Gaussian projections and offset buckets.
buildLSH Build tables with projections scalar hashes each and positive bucket width.
queryLSH Rank matching-bucket candidates by exact distance; may return fewer than k results, including none.
localOutlierFactor Training-set LOF with ties included at the kth neighbor distance.
IsolationForest Owned random isolation trees with fixed training dimension and subsample size.
fitIsolationForest Seeded independent isolation trees using subsamples of 2..2048 observations.
scoreIsolationForest Return 2^(-mean corrected path length / expected random-tree path length); larger means more anomalous.
OneClassSVM Kernel novelty detector with normalized nonnegative dual weights and learned offset.
fitOneClassSVM nu-SVM dual coordinate descent: sum alpha=1, 0<=alpha<=1/(nu*n).
decisionOneClassSVM Return signed novelty margins: positive indicates an inlier under the fitted kernel boundary.

Documentation

IndexKind

data IndexKind Source #

Axis-aligned KD partitions or enclosing Euclidean ball partitions.

Constructors

KDTree 
BallTree 

Instances

Instances details
Show IndexKind Source # 
Instance details

Defined in HMath.MachineLearning.Neighbors

Eq IndexKind Source # 
Instance details

Defined in HMath.MachineLearning.Neighbors

SpatialIndex

data SpatialIndex Source #

Immutable tree retaining original observation IDs and its validated feature dimension.

buildIndex

buildIndex :: IndexKind -> Int -> Dataset -> Either MathError SpatialIndex Source #

Build an exact search tree with at most leafSize observations per leaf.

NeighborResult

data NeighborResult Source #

Neighbor indices/distances plus visited-leaf and exhaustive-search diagnostics. Budget-limited candidates are explicitly marked approximate.

Constructors

NeighborResult 

Fields

  • neighbors :: [(Int, Double)]

    Training-row indices paired with returned Euclidean distances, ordered by distance and then index.

  • visitedLeaves :: Int

    Number of leaves visited against the explicit query budget.

  • exhaustive :: Bool

    True if the neighbor search completed its exact stopping condition; False if unexplored nodes may improve candidates.

nearestNeighbors

nearestNeighbors :: Int -> Int -> SpatialIndex -> [Double] -> Either MathError NeighborResult Source #

maxLeaves is a query work budget. A False exhaustive flag means unexplored nodes might improve the result. Even approximate results have exact distances.

knnRegress

knnRegress :: Int -> SpatialIndex -> [Double] -> Dataset -> Either MathError [Double] Source #

Average the k exact nearest training responses for each query row.

knnClassify

knnClassify :: Int -> SpatialIndex -> [Int] -> Dataset -> Either MathError [Int] Source #

Majority vote among k exact neighbors; targets follow original training row order.

LSH

data LSH Source #

Seeded p-stable Euclidean hash tables retaining Gaussian projections and offset buckets.

Instances

Instances details
Show LSH Source # 
Instance details

Defined in HMath.MachineLearning.Neighbors

Methods

showsPrec :: Int -> LSH -> ShowS #

show :: LSH -> String #

showList :: [LSH] -> ShowS #

Eq LSH Source # 
Instance details

Defined in HMath.MachineLearning.Neighbors

Methods

(==) :: LSH -> LSH -> Bool #

(/=) :: LSH -> LSH -> Bool #

buildLSH

buildLSH :: Word64 -> Int -> Int -> Double -> Dataset -> Either MathError LSH Source #

Build tables with projections scalar hashes each and positive bucket width. Storage budget is explicit.

queryLSH

queryLSH :: Int -> LSH -> [Double] -> Either MathError [(Int, Double)] Source #

Rank matching-bucket candidates by exact distance; may return fewer than k results, including none.

localOutlierFactor

localOutlierFactor :: Int -> Dataset -> Either MathError [Double] Source #

Training-set LOF with ties included at the kth neighbor distance. Identical neighborhoods have LOF 1, including the infinite-density 0/0 limiting case.

IsolationForest

data IsolationForest Source #

Owned random isolation trees with fixed training dimension and subsample size.

fitIsolationForest

fitIsolationForest :: Word64 -> Int -> Int -> Dataset -> Either MathError IsolationForest Source #

Seeded independent isolation trees using subsamples of 2..2048 observations.

scoreIsolationForest

scoreIsolationForest :: IsolationForest -> Dataset -> Either MathError [Double] Source #

Return 2^(-mean corrected path length / expected random-tree path length); larger means more anomalous.

OneClassSVM

data OneClassSVM Source #

Kernel novelty detector with normalized nonnegative dual weights and learned offset.

Instances

Instances details
Show OneClassSVM Source # 
Instance details

Defined in HMath.MachineLearning.Neighbors

Eq OneClassSVM Source # 
Instance details

Defined in HMath.MachineLearning.Neighbors

fitOneClassSVM

fitOneClassSVM :: MLConfig -> Kernel -> Double -> Dataset -> Either MathError (Fit OneClassSVM) Source #

nu-SVM dual coordinate descent: sum alpha=1, 0<=alpha<=1/(nu*n). The reported gap is the pairwise box-constrained KKT violation.

decisionOneClassSVM

decisionOneClassSVM :: OneClassSVM -> Dataset -> Either MathError [Double] Source #

Return signed novelty margins: positive indicates an inlier under the fitted kernel boundary.