Commit b2ff1268 authored by mzed's avatar mzed
Browse files

Fixes #42 kNN serialization seems to be working properly in both directions

parent 267f5dd3
...@@ -46,10 +46,12 @@ int main(int argc, const char * argv[]) { ...@@ -46,10 +46,12 @@ int main(int argc, const char * argv[]) {
classification myKnnFromString; classification myKnnFromString;
myKnnFromString.putJSON(myKnn.getJSON()); myKnnFromString.putJSON(myKnn.getJSON());
classification myKnnfromFile;
myKnnfromFile.readJSON(filepath2);
std::cout << "before: " << myKnn.process(inputVec)[0] << std::endl; std::cout << "knn before: " << myKnn.process(inputVec)[0] << std::endl;
std::cout << "from string: " << myKnnFromString.process(inputVec)[0] << std::endl; std::cout << "knn from string: " << myKnnFromString.process(inputVec)[0] << std::endl;
//std::cout << "from file: " << myKnnfromFile.process(inputVec)[0] << std::endl; std::cout << "knn from file: " << myKnnfromFile.process(inputVec)[0] << std::endl;
return 0; return 0;
} }
...@@ -135,7 +135,6 @@ void modelSet::json2modelSet(Json::Value root) { ...@@ -135,7 +135,6 @@ void modelSet::json2modelSet(Json::Value root) {
for (int i = 0; i < model["whichInputs"].size(); ++i) { //TODO: factor these for (int i = 0; i < model["whichInputs"].size(); ++i) { //TODO: factor these
whichInputs.push_back(model["whichInputs"][i].asDouble()); whichInputs.push_back(model["whichInputs"][i].asDouble());
} }
if (model["modelType"].asString() == "Neural Network") { if (model["modelType"].asString() == "Neural Network") {
int numHiddenLayers = model["numHiddenLayers"].asInt(); int numHiddenLayers = model["numHiddenLayers"].asInt();
int numHiddenNodes = model["numHiddenNodes"].asInt(); int numHiddenNodes = model["numHiddenNodes"].asInt();
...@@ -147,16 +146,15 @@ void modelSet::json2modelSet(Json::Value root) { ...@@ -147,16 +146,15 @@ void modelSet::json2modelSet(Json::Value root) {
double outBase = model["outBase"].asDouble(); double outBase = model["outBase"].asDouble();
myModelSet.push_back(new neuralNetwork(modelNumInputs, whichInputs, numHiddenLayers, numHiddenNodes, weights, wHiddenOutput, inRanges, inBases, outRange, outBase)); myModelSet.push_back(new neuralNetwork(modelNumInputs, whichInputs, numHiddenLayers, numHiddenNodes, weights, wHiddenOutput, inRanges, inBases, outRange, outBase));
} else if (model["modelType"].asString() == "kNN Classification") { } else if (model["modelType"].asString() == "kNN Classificiation") {
std::vector<trainingExample> trainingSet; std::vector<trainingExample> trainingSet;
const Json::Value examples = model["examples"]; const Json::Value examples = model["examples"];
for (int i = 0; i < examples.size(); ++i) { for (int i = 0; i < examples.size(); ++i) {
trainingExample tempExample; trainingExample tempExample;
tempExample.input = json2vector(examples[i]["features"]); tempExample.input = json2vector(examples[i]["features"]);
tempExample.output = json2vector(examples[i]["class"]); tempExample.output.push_back(examples[i]["class"].asDouble());
trainingSet.push_back(tempExample); trainingSet.push_back(tempExample);
} }
int k = model["k"].asInt(); int k = model["k"].asInt();
myModelSet.push_back(new knnClassification(modelNumInputs, whichInputs, trainingSet, k)); myModelSet.push_back(new knnClassification(modelNumInputs, whichInputs, trainingSet, k));
} }
......
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