k-Nearest Neighbors Classification (k-NN)
Operation | Computational methods | Programming Interface |
Mathematical formulation
- Identify the set
of the
kfeature vectors in the training set that are nearest towith respect to the Euclidean distance.
- Estimate the conditional probability for thel-th class as the fraction of vectors in
whose labels
are equal to
l: - Predict the class that has the highest probability for the feature vector
:
Programming Interface
- template<typenameFloat= detail::descriptor_base<>::float_t, typenameMethod= detail::descriptor_base<>::method_t, typenameTask= detail::descriptor_base<>::task_t>classdescriptor
- Template Parameters
- Float– The floating-point type that the algorithm uses for intermediate computations. Can befloatordouble.
- Method– Tag-type that specifies an implementation of algorithm. Can bemethod::bruteforceormethod::kd_tree.
- Task– Tag-type that specifies type of the problem to solve. Can betask::classification.
Constructors- descriptor(std::int64_tclass_count, std::int64_tneighbor_count)
- Creates a new instance of the class with the givenclass_countandneighbor_countproperty values.
Public Methods- auto &set_class_count(std::int64_tvalue)
- auto &set_neighbor_count(std::int64_tvalue)
- structbrute_force
- Tag-type that denotes brute-force computational method.
- structkd_tree
- Tag-type that denotes k-d tree computational method.
- Alias tag-type for brute-force computational method.
- structclassification
- Tag-type that parameterizes entities used for solving classification problem.
- Alias tag-type for classification task.
- template<typenameTask= task::by_default>classmodel
- Template Parameters
- Task– Tag-type that specifies type of the problem to solve. Can betask::classification.
Constructors- model()
- Creates a new instance of the class with the default property values.
- template<typenameTask= task::by_default>classtrain_input
- Template Parameters
- Task– Tag-type that specifies type of the problem to solve. Can betask::classification.
Constructors- train_input(consttable &data,consttable &labels)
- Creates a new instance of the class with the givendataandlabelsproperty values.
Properties- consttable &data= table{}
- The training set
.
- Getter & Setter
const table & get_data() const
auto & set_data(const table &data)
- consttable &labels= table{}
- Vector of labels
for the training set
.
- Getter & Setter
const table & get_labels() const
auto & set_labels(const table &labels)
- template<typenameTask= task::by_default>classtrain_result
- Template Parameters
- Task– Tag-type that specifies type of the problem to solve. Can betask::classification.
Constructors- train_result()
- Creates a new instance of the class with the default property values.
Properties
- template<typenameTask= task::by_default>classinfer_input
- Template Parameters
- Task– Tag-type that specifies type of the problem to solve. Can betask::classification.
Constructors- Creates a new instance of the class with the givenmodelanddataproperty values.
Properties- consttable &data= table{}
- The dataset for inference
.
- Getter & Setter
const table & get_data() const
auto & set_data(const table &data)
- template<typenameTask= task::by_default>classinfer_result
- Template Parameters
- Task– Tag-type that specifies type of the problem to solve. Can betask::classification.
Constructors- infer_result()
- Creates a new instance of the class with the default property values.
Properties- consttable &labels= table{}
- The predicted labels.
- Getter & Setter
const table & get_labels() const
auto & set_labels(const table &value)