double MultiClassExp::getConfidence(const FeatureVec& feature_vec, const std::vector<double>& weights_plus, const std::vector<double>& weights_minus) { double confidence_plus = std::inner_product(feature_vec.begin(), feature_vec.end(), weights_plus.begin(), 0.0); double confidence_minus = std::inner_product(feature_vec.begin(), feature_vec.end(), weights_minus.begin(), 0.0); double confidence = confidence_plus-confidence_minus; return confidence; }
/*the document is index:value format because there is less features,centorid is array format for quick access *now it's changed into cosine!2011-07-22 * */ float KNN::InnerProduct(FeatureVec & document,vector<float> & centorid, int catIndex) { /*part of cosine*/ float similarity=0.0; float finalScore=0.0; float totalDoc=0.0; FeatVecIte it; for(it=document.begin();it!=document.end();it++) { similarity+=((*it).weight*centorid[(*it).index]);//direct access the vector } //float docLen=inner_product(centorid.begin(),centorid.end(),centorid.begin(),0.0);//tip:可以提前计算好。@zhqm return similarity; }
int KNN::MaxProbCat(FeatureVec & document,vector<vector<float> > & Centoroid,float & score) { float MaxScoreNow=0.0; int MaxCatIndex=-1; int currentCatIndex=0; float docDotProduct=0.0; FeatVecIte dit; for(dit=document.begin();dit!=document.end();dit++) { docDotProduct+=(*dit).weight*(*dit).weight; //tip:@zhqm totalDoc += } docDotProduct = sqrt(docDotProduct); if (docDotProduct == 0){ score = 0.0; return MaxCatIndex; } vector<vector<float> >::iterator it; for(it=Centoroid.begin();it!=Centoroid.end();it++,currentCatIndex++) { float score=InnerProduct(document,(*it), currentCatIndex); score=(float)score/(docDotProduct * m_centoroidFeatureDotProduct.at(currentCatIndex)); //cout<<"cat="<<currentCatIndex<<"\tscore="<<score<<endl; if(score>MaxScoreNow)//a better class { MaxScoreNow=score; MaxCatIndex=currentCatIndex; } } score=MaxScoreNow; return MaxCatIndex; }