[−][src]Module smartcore::neighbors::knn_classifier
K Nearest Neighbors Classifier
K Nearest Neighbors Classifier
SmartCore relies on 2 backend algorithms to speedup KNN queries:
The parameter k
controls the stability of the KNN estimate: when k
is small the algorithm is sensitive to the noise in data. When k
increases the estimator becomes more stable.
In terms of the bias variance trade-off the variance decreases with k
and the bias is likely to increase with k
.
When you don't know which search algorithm and k
value to use go with default parameters defined by Default::default()
To fit the model to a 4 x 2 matrix with 4 training samples, 2 features per sample:
use smartcore::linalg::naive::dense_matrix::*; use smartcore::neighbors::knn_classifier::*; use smartcore::math::distance::*; //your explanatory variables. Each row is a training sample with 2 numerical features let x = DenseMatrix::from_2d_array(&[ &[1., 2.], &[3., 4.], &[5., 6.], &[7., 8.], &[9., 10.]]); let y = vec![2., 2., 2., 3., 3.]; //your class labels let knn = KNNClassifier::fit(&x, &y, Default::default()).unwrap(); let y_hat = knn.predict(&x).unwrap();
variable y_hat
will hold a vector with estimates of class labels
Structs
KNNClassifier | K Nearest Neighbors Classifier |
KNNClassifierParameters |
|