-
-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* knn Signed-off-by: Andrey Parfenov <[email protected]>
- Loading branch information
1 parent
1fb2096
commit e51ee22
Showing
21 changed files
with
35,539 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ end | |
@enum BrainFlowClassifiers begin | ||
|
||
REGRESSION = 0 | ||
KNN = 1 | ||
|
||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
classdef BrainFlowClassifiers < int32 | ||
enumeration | ||
REGRESSION (0) | ||
KNN (1) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <stdlib.h> | ||
|
||
#include "brainflow_constants.h" | ||
#include "concentration_knn_classifier.h" | ||
#include "focus_dataset.h" | ||
|
||
|
||
int ConcentrationKNNClassifier::prepare () | ||
{ | ||
if (kdtree != NULL) | ||
{ | ||
return (int)BrainFlowExitCodes::ANOTHER_CLASSIFIER_IS_PREPARED_ERROR; | ||
} | ||
if (!params.other_info.empty ()) | ||
{ | ||
try | ||
{ | ||
num_neighbors = std::stoi (params.other_info); | ||
} | ||
catch (const std::exception &e) | ||
{ | ||
return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR; | ||
} | ||
} | ||
if ((num_neighbors < 1) || (num_neighbors > 100)) | ||
{ | ||
return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR; | ||
} | ||
|
||
int dataset_len = sizeof (brainflow_focus_y) / sizeof (brainflow_focus_y[0]); | ||
for (int i = 0; i < dataset_len; i++) | ||
{ | ||
FocusPoint point (brainflow_focus_x[i], 10, brainflow_focus_y[i]); | ||
// decrease weight for stddev, 0.2 - experimental vlaue | ||
for (int j = 5; j < 10; j++) | ||
{ | ||
point[j] *= 0.2; | ||
} | ||
dataset.push_back (point); | ||
} | ||
kdtree = new kdt::KDTree<FocusPoint> (dataset); | ||
return (int)BrainFlowExitCodes::STATUS_OK; | ||
} | ||
|
||
int ConcentrationKNNClassifier::predict (double *data, int data_len, double *output) | ||
{ | ||
if ((data_len < 5) || (data == NULL) || (output == NULL)) | ||
{ | ||
return (int)BrainFlowExitCodes::INVALID_BUFFER_SIZE_ERROR; | ||
} | ||
if (kdtree == NULL) | ||
{ | ||
return (int)BrainFlowExitCodes::CLASSIFIER_IS_NOT_PREPARED_ERROR; | ||
} | ||
|
||
double feature_vector[10] = {0.0}; | ||
for (int i = 0; i < data_len; i++) | ||
{ | ||
if (i >= 5) | ||
{ | ||
feature_vector[i] = data[i] * 0.2; | ||
} | ||
else | ||
{ | ||
feature_vector[i] = data[i]; | ||
} | ||
} | ||
|
||
FocusPoint sample_to_predict (feature_vector, 10, 0.0); | ||
const std::vector<int> knn_ids = kdtree->knnSearch (sample_to_predict, num_neighbors); | ||
int num_ones = 0; | ||
for (int i = 0; i < knn_ids.size (); i++) | ||
{ | ||
if (dataset[knn_ids[i]].value == 1) | ||
{ | ||
num_ones++; | ||
} | ||
} | ||
|
||
double score = ((double)num_ones) / num_neighbors; | ||
*output = score; | ||
|
||
return (int)BrainFlowExitCodes::STATUS_OK; | ||
} | ||
|
||
int ConcentrationKNNClassifier::release () | ||
{ | ||
delete kdtree; | ||
kdtree = NULL; | ||
dataset.clear (); | ||
return (int)BrainFlowExitCodes::STATUS_OK; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
#include "base_classifier.h" | ||
#include "focus_point.h" | ||
|
||
#include "kdtree.h" | ||
|
||
|
||
class ConcentrationKNNClassifier : public BaseClassifier | ||
{ | ||
public: | ||
ConcentrationKNNClassifier (struct BrainFlowModelParams params) : BaseClassifier (params) | ||
{ | ||
num_neighbors = 5; | ||
kdtree = NULL; | ||
} | ||
|
||
virtual ~ConcentrationKNNClassifier () | ||
{ | ||
release (); | ||
} | ||
|
||
virtual int prepare (); | ||
virtual int predict (double *data, int data_len, double *output); | ||
virtual int release (); | ||
|
||
private: | ||
std::vector<FocusPoint> dataset; | ||
kdt::KDTree<FocusPoint> *kdtree; | ||
int num_neighbors; | ||
}; |
Oops, something went wrong.