Commit e95badb7 authored by mzed's avatar mzed
Browse files

modernizing

parent 7863b142
......@@ -86,7 +86,7 @@ T knnClassification<T>::run(const std::vector<T> &inputVector) {
for (int i = 0; i < currentK; ++i) {
nearestNeighbours[i] = {0, 0.};
};
std::pair<int, T> farthestNN = {0, 0.};
std::pair<int, T> farthestNN {0, 0.};
std::vector<T> pattern;
for (int h = 0; h < numInputs; h++) {
......@@ -95,7 +95,7 @@ T knnClassification<T>::run(const std::vector<T> &inputVector) {
//Find k nearest neighbours
int index = 0;
for (typename std::vector<trainingExampleTemplate<T> >::iterator it = neighbours.begin(); it != neighbours.end(); ++it) {
for (auto it = neighbours.cbegin(); it != neighbours.cend(); ++it) {
//find Euclidian distance for this neighbor
T euclidianDistance = 0;
for(int j = 0; j < numInputs ; ++j){
......@@ -126,19 +126,18 @@ T knnClassification<T>::run(const std::vector<T> &inputVector) {
//majority vote on nearest neighbours
std::map<int, int> classVoteMap;
typedef std::pair<int, int> classVotePair;
using classVotePair = std::pair<int, int>;
for (int i = 0; i < currentK; ++i){
int classNum = (int) std::round(neighbours[nearestNeighbours[i].first].output[0]);
if ( classVoteMap.find(classNum) == classVoteMap.end() ) {
classVoteMap.insert(classVotePair(classNum, 1));
} else {
classVoteMap[classNum]++;
++classVoteMap[classNum];
}
}
T foundClass = 0;
int mostVotes = 0;
std::map<int, int>::iterator p;
for(p = classVoteMap.begin(); p != classVoteMap.end(); ++p)
for (auto p = classVoteMap.cbegin(); p != classVoteMap.cend(); ++p)
{
if (p->second > mostVotes) {
mostVotes = p->second;
......@@ -156,7 +155,7 @@ void knnClassification<T>::getJSONDescription(Json::Value &jsonModelDescription)
jsonModelDescription["whichInputs"] = this->vector2json(whichInputs);
jsonModelDescription["k"] = desiredK;
Json::Value examples;
for (typename std::vector<trainingExampleTemplate<T> >::iterator it = neighbours.begin(); it != neighbours.end(); ++it) {
for (auto it = neighbours.cbegin(); it != neighbours.cend(); ++it) {
Json::Value oneExample;
oneExample["class"] = it->output[0];
oneExample["features"] = this->vector2json(it->input);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment