KnnClassifier class abstract

A class that performs classification based on k nearest neighbours (KNN) algorithm

K nearest neighbours algorithm is an algorithm that is targeted to search for the most similar k observations to the given one.

It is possible to use the majority class among the k found observations as a prediction for the given unlabelled observation, but it may lead to an imprecise result. To overcome this, the weighted version of KNN algorithm is used in the classifier. To get the weight of a particular observation one may use a kernel function.


KnnClassifier(DataFrame trainData, String targetName, int k, {KernelType kernel = KernelType.gaussian, Distance distance = Distance.euclidean, String classLabelPrefix = 'Class label', DType dtype = dTypeDefaultValue})
KnnClassifier.fromJson(String json)
Restores previously fitted classifier instance from the given json


distanceType Distance
A distance type that is used to measure how far are observations from each other
no setter
dtype DType
A type for all numeric values using by the Predictor
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
k int
A number of nearest neighbours
no setter
kernelType KernelType
A kernel function type
no setter
negativeLabel num
A value using to encode negative class.
no setterinherited
positiveLabel num
A value using to encode positive class.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
schemaVersion int?
Contains a version of the current json schema
no setterinherited
targetNames Iterable<String>
A collection of target column names of a dataset which was used to learn the ML model
no setterinherited


assess(DataFrame observations, MetricType metricType) double
Assesses model performance according to provided metricType
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
predict(DataFrame testFeatures) → DataFrame
Returns prediction, based on the learned coefficients
predictProbabilities(DataFrame testFeatures) → DataFrame
Returns predicted distribution of probabilities for each observation in the passed testFeatures
retrain(DataFrame data) KnnClassifier
Re-runs the learning process on the new training data. The features, model algorithm, and hyperparameters remain the same.
saveAsJson(String filePath) Future<File>
Saves a json-serializable map into a newly created file with the path filePath
toJson() Map<String, dynamic>
Returns a json-serializable map
toString() String
A string representation of this object.


operator ==(Object other) bool
The equality operator.