void KNN::train(Examples& exs){ TRACE_V(TAG,"train"); //Maybe we didnt calculate this before... stats->calculateIDF(); for(int i = 0; i < exs.getNumberOfNumericalAttibutes(); i++){ maxv[i] = numeric_limits<double>::min(); minv[i] = numeric_limits<double>::max(); } for(ExampleIterator e = exs.getBegin(); e != exs.getEnd(); e++){ vector<string> textTokens = (e)->getTextTokens(); vector<int> textFrequencyTokens = (e)->getTextFrequency(); string exampleClass = (e)->getClass(); string eId = (e)->getId(); double docSize = 0.0; // cout<<" Tokens categoricos = " << tokens.size() << endl; for(unsigned int i = 3; i < textTokens.size(); i++){ int tf = textFrequencyTokens[i-3]; string termId = textTokens[i]; double tfidf = tf * stats->getIDF(termId); docSize += (tfidf * tfidf); docWeighted dw(eId, tfidf); termDocWset[termId].insert(dw); } vector<double> numTokens = (e)->getNumericalTokens(); for(unsigned int i = 0; i < numTokens.size(); i++){ if(greaterThan(numTokens[i], maxv[i])){ maxv[i] = numTokens[i]; } if(lesserThan(numTokens[i], minv[i])){ minv[i] = numTokens[i]; } } exNumTrain[eId] = numTokens; exCatTrain[eId] = (e)->getCategoricalTokens(); docTrainSizes[eId] = docSize; } }
void KNN::test(Examples& exs){ TRACE_V(TAG,"test"); //Statistics: map<string,unsigned long long> classHits; map<string,unsigned long long> classMiss; map<string,unsigned long long> mappedDocs; map<string,unsigned long long> docsPerClass; int numExamples = 0; for(ExampleIterator it = exs.getBegin(); it != exs.getEnd(); it++){ numExamples++; if(numExamples % 100 == 0) cout<<"Evaluated: " << numExamples<<endl; Example ex = *it; vector<string> textTokens = ex.getTextTokens(); vector<int> textFreqTokens = ex.getTextFrequency(); vector<double> numTokens = ex.getNumericalTokens(); vector<string> catTokens = ex.getCategoricalTokens(); string eId = ex.getId(); string classId = ex.getClass(); map<string, double> examplesTestSize; //credibility to each class if((usingKNNOptimize && !valuesSaved ) || !usingKNNOptimize){ for(unsigned int i = 3; i < textTokens.size(); i++){ string termId = textTokens[i]; int tf = textFreqTokens[i-3]; for(set<string>::iterator classIt = stats->getClasses().begin(); classIt != stats->getClasses().end(); classIt++) { double tfidf = tf * getContentCredibility(termId, *classIt); examplesTestSize[*classIt] += (tfidf * tfidf); } } } map<string, double> similarity; if(usingKNNOptimize && valuesSaved){ similarity = saveValues[eId]; } else{ for(unsigned int i = 3; i < textTokens.size();i++){ string termId = textTokens[i]; int tf = textFreqTokens[1-3]; for(set<docWeighted, docWeightedCmp>::iterator termIt = termDocWset[termId].begin(); termIt != termDocWset[termId].end(); termIt++){ string trainClass = stats-> getTrainClass(termIt->docId); double trainDocSize = docTrainSizes[termIt->docId]; double trainTermWeight = termIt->weight; double testTermWeight = tf * getContentCredibility(termId, trainClass); similarity[termIt->docId] += ( - ( trainTermWeight / sqrt(trainDocSize) * testTermWeight / sqrt(examplesTestSize[trainClass]) )); // cout<<"sim = " << similarity[termIt->docId] <<endl; } } //numerical KNN for(map<string, vector<double> >::iterator trainIt = exNumTrain.begin(); trainIt != exNumTrain.end(); trainIt++){ double dist = 0.0; for(unsigned int i = 0; i < numTokens.size(); i++){ double a = minMaxNorm(numTokens[i],i); double b = minMaxNorm(exNumTrain[trainIt->first][i],i); double val = (a-b)*(a-b); //double val = (numTokens[i] - exNumTrain[trainIt->first][i]) * ( numTokens[i] - exNumTrain[trainIt->first][i]); // cout<<numTokens[i] << " - " <<exNumTrain[trainIt->first][i] <<endl; // cout<<"a = " << a << " b = " << b << " val =" << val<<endl; if( greaterThan(dist + val, numeric_limits<double>::max())){ // cerr<<"OOOOOOOOOOOOOOOPA!!!"<<endl; // exit(0); dist = numeric_limits<double>::max() - 1.0; break; } dist += val; // cout<<"dist =" << dist<<endl; } similarity[trainIt->first] += dist; } //categorical KNN for(map<string, vector<string> >::iterator trainIt = exCatTrain.begin(); trainIt != exCatTrain.end(); trainIt++){ double dist = 0.0; for(unsigned int i = 0; i < catTokens.size(); i++){ string trainTok = exCatTrain[trainIt->first][i]; string testTok = catTokens[i]; double catCred = getCategoricalCredibility(i, testTok, stats->getTrainClass(trainIt->first)); // cout<<"catCred = " <<catCred<<endl; // cout<<" i = " << i << "teste = " << testTok<<" treino = " << trainTok<<endl; if(trainTok != testTok){ // dist+= 1.0/(catCred+ 1.0) + 1.0; dist+= 1.0/(catCred+ 1.0); // cout<<"dist = " << dist<<endl; } } similarity[trainIt->first] += dist; } // cout<<"class = " << classId << " doc = " << trainIt->first<< " docClass = " << stats->getTrainClass(trainIt->first) << " dist="<<dist<< " 1/dist = " <<1.0/dist<< " sqrt = "<<sqrt(dist)<<endl; } if(!valuesSaved && usingKNNOptimize){ saveValues[eId] = similarity; } //sim of each example in test set set<docWeighted, docWeightedCmp> sim; for(map<string, double>::iterator testIt = similarity.begin(); testIt != similarity.end(); testIt++){ //calculating graph credibility....if so vector<double> graphsCreds(graphsCredibility.size()); double similarityValue = testIt->second; // cout<< " eid = " << eId << " eclass = " << classId << " traindocclass = " << stats->getTrainClass(testIt->first) << " similarit = " << similarityValue<< endl; for(unsigned int g = 0 ; g < graphsCredibility.size(); g++){ double gsim = getGraphCredibility(g, eId, stats->getTrainClass(testIt->first)); similarityValue /= (0.5+gsim); } //never change this, it is necessary docWeighted dw(testIt->first, similarityValue); sim.insert(dw); } string predictedLabel = getPredictedClass(sim); computeConfusionMatrix(classId, predictedLabel); // if(io->usingPredictionsFile) savePrediction(eId, classId, predictedLabel); if(predictedLabel == classId){ classHits[classId] ++; } else{ classMiss[classId]++; } mappedDocs[predictedLabel]++; docsPerClass[classId]++; } if(valuesSaved == false){ valuesSaved = true; } calculateF1(classHits,classMiss,docsPerClass, mappedDocs); // showConfusionMatrix(); }