// Alpha-nDCG Computation static arma::rowvec compute_andcg_discount(arma::rowvec subtopics, double alpha){ // Iterate through the elements in the vector and compute (1- alpha)^x arma::rowvec discount = arma::zeros<arma::rowvec>(subtopics.n_cols); int i =0; for(arma::rowvec::iterator it=subtopics.begin(); it!=subtopics.end(); ++it){ discount(i++) = pow((1 - alpha),(*it)); } return discount; }
/** For debugging reasons*/ void checkAllComponents() { arma::vec rowSumA = arma::sum(A_, 1); rowSumA.print("rowSumA"); double sumPi = arma::sum(pi_); std::cout << "sumPi: " << sumPi << std::endl; arma::vec weights = arma::zeros((unsigned int)BModels_.size()); for (unsigned int i = 0; i < (unsigned int) BModels_.size(); ++i) { weights(i) = arma::accu(BModels_[i].getWeights()); } weights.print("bCumWeights"); arma::rowvec checksum = arma::sum(gamma_); checksum.print("checksum"); arma::uvec checksumIndices = arma::find(checksum < 1.0 - 1E-2); if (checksumIndices.n_elem >= 1) { arma::rowvec checksumAlpha = arma::sum(alpha_); checksumAlpha.print("checkAlpha"); //alpha_.print("alpha"); arma::rowvec checksumBeta = arma::sum(beta_); checksumBeta.print("checkBeta"); //beta_.print("beta"); c_.print("c"); throw std::runtime_error("data going wonky"); } if (!arma::is_finite(A_)) { A_.print("A Fail"); throw std::runtime_error("A has invalid entries"); } if (!arma::is_finite(pi_)) { pi_.print("pi Fail"); throw std::runtime_error("pi has invalid entries"); } if (!arma::is_finite(alpha_)) { alpha_.print("alpha Fail"); throw std::runtime_error("alpha has invalid entries"); } if (!arma::is_finite(beta_)) { beta_.print("beta Fail"); throw std::runtime_error("beta has invalid entries"); } if (!arma::is_finite(gamma_)) { gamma_.print("gamma Fail"); throw std::runtime_error("gamma has invalid entries"); } if (!arma::is_finite(xi_)) { xi_.print("xi Fail"); throw std::runtime_error("xi has invalid entries"); } }
void print(std::string header = "") { A_.print("A"); for (size_t i = 0; i < BModels_.size(); ++i) { BModels_[i].print("B"); } pi_.print("pi"); }
void Adaboost<MatType, WeakLearner>::buildWeightMatrix( const arma::mat& D, arma::rowvec& weights) { int i, j; weights.fill(0.0); for (i = 0;i < D.n_rows; i++) { for (j = 0;j < D.n_cols; j++) weights(i) += D(i,j); } }
void pca::read_armvct (const char * fname, arma::rowvec & q) { int n; std::ifstream myfileq(fname, std::ios::binary); myfileq.read((char*)&(n), sizeof(n)); q.set_size(n); for (int i=0; i<(int)q.n_cols; i++) { double v; myfileq.read((char*)&v, sizeof(v)); q(i) = v; } myfileq.close(); }
double DecisionStump<MatType>::SetupSplitDimension( const arma::rowvec& dimension, const arma::Row<size_t>& labels, const arma::rowvec& weights) { size_t i, count, begin, end; double entropy = 0.0; // Sort the dimension in order to calculate splitting ranges. arma::rowvec sortedDim = arma::sort(dimension); // Store the indices of the sorted dimension to build a vector of sorted // labels. This sort is stable. arma::uvec sortedIndexDim = arma::stable_sort_index(dimension.t()); arma::Row<size_t> sortedLabels(dimension.n_elem); arma::rowvec sortedWeights(dimension.n_elem); for (i = 0; i < dimension.n_elem; i++) { sortedLabels(i) = labels(sortedIndexDim(i)); // Apply weights if necessary. if (UseWeights) sortedWeights(i) = weights(sortedIndexDim(i)); } i = 0; count = 0; // This splits the sorted data into buckets of size greater than or equal to // bucketSize. while (i < sortedLabels.n_elem) { count++; if (i == sortedLabels.n_elem - 1) { // If we're at the end, then don't worry about the bucket size; just take // this as the last bin. begin = i - count + 1; end = i; // Use ratioEl to calculate the ratio of elements in this split. const double ratioEl = ((double) (end - begin + 1) / sortedLabels.n_elem); entropy += ratioEl * CalculateEntropy<UseWeights>( sortedLabels.subvec(begin, end), sortedWeights.subvec(begin, end)); i++; } else if (sortedLabels(i) != sortedLabels(i + 1)) { // If we're not at the last element of sortedLabels, then check whether // count is less than the current bucket size. if (count < bucketSize) { // If it is, then take the minimum bucket size anyways. // This is where the inpBucketSize comes into use. // This makes sure there isn't a bucket for every change in labels. begin = i - count + 1; end = begin + bucketSize - 1; if (end > sortedLabels.n_elem - 1) end = sortedLabels.n_elem - 1; } else { // If it is not, then take the bucket size as the value of count. begin = i - count + 1; end = i; } const double ratioEl = ((double) (end - begin + 1) / sortedLabels.n_elem); entropy += ratioEl * CalculateEntropy<UseWeights>( sortedLabels.subvec(begin, end), sortedWeights.subvec(begin, end)); i = end + 1; count = 0; } else i++; } return entropy; }
double mahalanobis(const arma::rowvec& x, const arma::rowvec& mu, const arma::mat& sigma) { const arma::rowvec err = x - mu; return arma::as_scalar(err * sigma.i() * err.t()); }
double mahalanobis_chol(const arma::rowvec& x, const arma::rowvec& mu, const arma::mat& R) { const arma::rowvec err = x - mu; const arma::mat Rinv(inv(trimatl(R))); return arma::as_scalar(err * Rinv * Rinv.t() * err.t()); }
void DecisionStump<MatType>::TrainOnAtt(const arma::rowvec& attribute, const arma::Row<size_t>& labels) { size_t i, count, begin, end; arma::rowvec sortedSplitAtt = arma::sort(attribute); arma::uvec sortedSplitIndexAtt = arma::stable_sort_index(attribute.t()); arma::Row<size_t> sortedLabels(attribute.n_elem); sortedLabels.fill(0); arma::vec tempSplit; arma::Row<size_t> tempLabel; for (i = 0; i < attribute.n_elem; i++) sortedLabels(i) = labels(sortedSplitIndexAtt(i)); arma::rowvec subCols; rType mostFreq; i = 0; count = 0; while (i < sortedLabels.n_elem) { count++; if (i == sortedLabels.n_elem - 1) { begin = i - count + 1; end = i; arma::rowvec zSubCols((sortedLabels.cols(begin, end)).n_elem); zSubCols.fill(0.0); subCols = sortedLabels.cols(begin, end) + zSubCols; mostFreq = CountMostFreq<double>(subCols); split.resize(split.n_elem + 1); split(split.n_elem - 1) = sortedSplitAtt(begin); binLabels.resize(binLabels.n_elem + 1); binLabels(binLabels.n_elem - 1) = mostFreq; i++; } else if (sortedLabels(i) != sortedLabels(i + 1)) { if (count < bucketSize) { // Test for different values of bucketSize, especially extreme cases. begin = i - count + 1; end = begin + bucketSize - 1; if (end > sortedLabels.n_elem - 1) end = sortedLabels.n_elem - 1; } else { begin = i - count + 1; end = i; } arma::rowvec zSubCols((sortedLabels.cols(begin, end)).n_elem); zSubCols.fill(0.0); subCols = sortedLabels.cols(begin, end) + zSubCols; // Find the most frequent element in subCols so as to assign a label to // the bucket of subCols. mostFreq = CountMostFreq<double>(subCols); split.resize(split.n_elem + 1); split(split.n_elem - 1) = sortedSplitAtt(begin); binLabels.resize(binLabels.n_elem + 1); binLabels(binLabels.n_elem - 1) = mostFreq; i = end + 1; count = 0; } else i++; } // Now trim the split matrix so that buckets one after the after which point // to the same classLabel are merged as one big bucket. MergeRanges(); }