TKNearestNeighbors Class

TComponent→TClassifier→TKNearestNeighbors

type TKNearestNeighbors = class(TClassifier)

Implementation of the K-NN (K Nearest Neighbors) classification algorithm.

K-NN searches for K nearest neighbors, to the example that we want to classify, and then classifies to the majority class of the nearest neighbors. The algorithm works very well especially for real valued attributes. (real domains). The algorithm gives very good results, but its main disadvantage is slow performance (computational complexity O(n2)).

Constructors

NameDescription
Create

Properties

NameTypeDescription
AttributeCountintegerTotal number of attributes.
AttributeEnabledbooleanSet the value at index to True/False, to enable/disable the corresponding attribute.
ClassesTStringListA list of recognized classes.
DiscreteAttributeCountintegerTotal number of discrete valued attributes or fields holding discreting values in the dataset.
DistanceModelTDistanceModelDefines the distance model used, when calculating the distance between examples.
ExampleCountintegerTotal number of learned examples.
FloatAttributeCountintegerTotal number of real valued attributes or fields holding real values in the dataset.
IndexesZeroBasedbooleanSet to True, if Class indexes are zero based.
KNeighborsintegerDefines the K parameter for the K-NN algorithm.
LeaveOneOutintegerSet the value of this property to the index of the example that you want to be ignored during the classification.
MissingDiscreteIndexintegerSpecifies the value indicating a "missing value" (no entry) for discrete attributes in the dataset.
MissingFloatValuestringSpecifies the value indicating a "missing value" (no entry) for real valued attributes in the dataset.
NormalizeAttributeCountbooleanIf true, the number of attributes compared will be normalized between comparisons.
OnClassifyTestTOnClassifyTestCalled by PrePrune, PostPrune and Prune methods and ClassifyTest methods.
OnFirstRecordTNotifyEventCalled by LearnData method and K-NN to request the positioning to the first record.
OnIndexNextRecordTIndexNextRecordCalled by LearnData method and K-NN to browse through the learn dataset.
OnLastRecordTNotifyEventCalled by LearnData method and K-NN when the last record is fetched.
OnNameNextRecordTNameNextRecordCalled by LearnData method and K-NN to browse through the learn data set.
OnPopPositionTPopBookmarkCalled by LearnData method and K-NN to restore the current position of the dataset.
OnPushPositionTPushBookmarkCalled by LearnData method and K-NN to save the current position of the dataset.
RejectProbabilitydoubleIf the response of all classes is below RejectProbability the example will not be classified to any of the known classes.
StoreExamplesbooleanSet this property to true, to store all learned examples.

Methods

NameDescription
AssignAttributeStateCopies the attribute Enabled fields from Source.
AttributeQualityReturns an array of attribute indexes sorted by quality.
ClassesName
Classify
ClassifyResponse
ClassifyTest
ClassIndex
Clear
Destroy
DisableAttributesDisable real valued and discrete attributes for all classes.
DisableDiscreteAttributesDisable discrete attributes for all classes.
DisableFloatAttributesDisable real valued attributes for all classes.
EnableAttributesEnable real valued and discrete attributes for all classes.
EnableDiscreteAttributesEnable discrete attributes for all classes.
EnableFloatAttributesEnable real valued attributes for all classes.
InsertNeighborsInsert a record holding in to the K nearest neighbors array.
Learn
LearnDataCall this method to perform the learn operation on the learn data.
LearnIndex
LoadFromFileLoad the component from file named FileName.
LoadFromStream
MaxPriorProbabilityReturns the index of the class, which has the highest count of examples and thus the highest prior probability, with ClassIndex.
MissingFloatNumberReturns numerical representation of MissingFloatValue.
PostPrunePruning disables some attributes, to improve classification accuracy.
PrePrunePruning disables some attributes, to improve classificaiton accuracy.
PrunePerforms pre-pruning and post-pruning and enables only those attributes giving best classification accuracy towards the test dataset.
Reset
SaveToFileSave the component to file named FileName.
SaveToStream
SoftReset
SortIndexRecordSort an array of TIndexRecords.