Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
Algorithms
NearestNeighbors
AbstractNearestNeighbors.h
Go to the documentation of this file.
1
//===========================================================================
2
/*!
3
*
4
*
5
* \brief Interface for nearest Neighbor queries
6
*
7
*
8
*
9
* \author O.Krause
10
* \date 2012-2014
11
*
12
*
13
* \par Copyright 1995-2017 Shark Development Team
14
*
15
* <BR><HR>
16
* This file is part of Shark.
17
* <https://shark-ml.github.io/Shark/>
18
*
19
* Shark is free software: you can redistribute it and/or modify
20
* it under the terms of the GNU Lesser General Public License as published
21
* by the Free Software Foundation, either version 3 of the License, or
22
* (at your option) any later version.
23
*
24
* Shark is distributed in the hope that it will be useful,
25
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
* GNU Lesser General Public License for more details.
28
*
29
* You should have received a copy of the GNU Lesser General Public License
30
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
31
*
32
*/
33
//===========================================================================
34
35
#ifndef SHARK_ALGORITHMS_NEARESTNEIGHBORS_ABSTRACTNEARESTNEIGHBORS_H
36
#define SHARK_ALGORITHMS_NEARESTNEIGHBORS_ABSTRACTNEARESTNEIGHBORS_H
37
38
#include <
shark/Core/utility/KeyValuePair.h
>
39
#include <
shark/Data/Dataset.h
>
40
namespace
shark
{
41
42
43
/// \brief Interface for Nearest Neighbor queries.
44
///
45
/// Defines the abstract interface for query of nearest neighbors. This is used to generalize over the different algorithms
46
/// to query for nearest neighbors.
47
template
<
class
InputType,
class
LabelType>
48
class
AbstractNearestNeighbors
{
49
public
:
50
typedef
KeyValuePair<double,LabelType>
DistancePair
;
51
typedef
typename
Batch<InputType>::type
BatchInputType
;
52
53
///\brief Returns the k-nearest neighbors of a batch of points and returns them as linearized array.
54
///
55
///Given a batch of size n, a array with nxk values is returned where each entry is a key-value pair of distance and label.
56
///the first k entries are the neighbors of point 1, the next k of point 2 and so on.
57
virtual
std::vector<DistancePair>
getNeighbors
(
BatchInputType
const
& batch, std::size_t k)
const
= 0;
58
59
/// \brief Returns the expected shape of the inputs
60
Shape
const
&
inputShape
()
const
{
61
return
m_inputShape
;
62
}
63
64
///\brief returns a const reference to the dataset used by the algorithm
65
virtual
LabeledData<InputType,LabelType>
const
&
dataset
()
const
= 0;
66
67
virtual
~AbstractNearestNeighbors
() {}
68
protected
:
69
Shape
m_inputShape
;
70
};
71
72
73
}
74
75
#endif