本文整理汇总了C++中rcpp::NumericMatrix::column方法的典型用法代码示例。如果您正苦于以下问题:C++ NumericMatrix::column方法的具体用法?C++ NumericMatrix::column怎么用?C++ NumericMatrix::column使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rcpp::NumericMatrix
的用法示例。
在下文中一共展示了NumericMatrix::column方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: svd_cov
// gradient-descent ------------------------------------------------------------
//' @title Regular gradient descent for latent factor model with covariates
//' @param X The ratings matrix. Unobserved entries must be marked NA. Users
//' must be along rows, and tracks must be along columns.
//' @param Z The covariates associated with each pair. This must be shaped as an
//' array with users along rows, tracks along columns, and features along
//' slices.
//' @param k_factors The number of latent factors for the problem.
//' @param lambdas The regularization parameters for P, Q, and beta, respectively.
//' @param n_iter The number of gradient descent iterations to run.
//' @param batch_samples The proportion of the observed entries to sample in the
//' gradient for each factor in the gradient descent.
//' @param batch_factors The proportion of latent factors to update at each
//' iteration.
//' @param gamma The step-size in the gradient descent.
//' @param verbose Print the objective at each iteration of the descent?
//' @return A list with the following elements, \cr
//' $P The learned user latent factors. \cr
//' $Q The learned track latent factors. \cr
//' $beta The learned regression coefficients.
//' @export
// [[Rcpp::export]]
Rcpp::List svd_cov(Rcpp::NumericMatrix X, Rcpp::NumericVector Z_vec,
int k_factors, Rcpp::NumericVector lambdas, int n_iter,
double batch_samples, double batch_factors,
double gamma_pq, double gamma_beta, bool verbose) {
// convert to arma
Rcpp::IntegerVector Z_dim = Z_vec.attr("dim");
arma::cube Z(Z_vec.begin(), Z_dim[0], Z_dim[1], Z_dim[2], false);
// initialize results
int m = X.nrow();
int n = X.ncol();
int l = Z_dim[2];
arma::mat P = 1.0 / sqrt(m) * arma::randn(m, k_factors);
arma::mat Q = 1.0 / sqrt(n) * arma::randn(n, k_factors);
arma::vec beta = 1.0 / sqrt(l) * arma::randn(l);
arma::vec objs = arma::zeros(n_iter);
// perform gradient descent steps
for(int cur_iter = 0; cur_iter < n_iter; cur_iter++) {
// update user factors
arma::uvec cur_factors = get_batch_ix(m, batch_factors);
for(int i = 0; i < cur_factors.n_elem; i++) {
int cur_ix = cur_factors(i);
P.row(cur_ix) = update_factor(X.row(cur_ix),
Z(arma::span(cur_ix), arma::span(), arma::span()),
arma::conv_to<arma::vec>::from(P.row(cur_ix)), Q, beta,
batch_samples, lambdas[0], gamma_pq).t();
}
// update track factors
cur_factors = get_batch_ix(n, batch_factors);
for(int j = 0; j < cur_factors.n_elem; j++) {
int cur_ix = cur_factors(j);
Q.row(cur_ix) = update_factor(X.column(cur_ix),
Z(arma::span(), arma::span(cur_ix), arma::span()),
arma::conv_to<arma::vec>::from(Q.row(cur_ix)), P, beta,
batch_samples, lambdas[1], gamma_pq).t();
}
// update regression coefficients
beta = update_beta(Rcpp::as<arma::mat>(X), Z, P, Q, beta, lambdas[2], gamma_beta);
objs[cur_iter] = objective_fun(Rcpp::as<arma::mat>(X), Z, P, Q, beta, lambdas);
if(verbose) {
printf("iteration %d \n", cur_iter);
printf("Objective: %g \n", objs[cur_iter]);
}
}
return Rcpp::List::create(Rcpp::Named("P") = P,
Rcpp::Named("Q") = Q,
Rcpp::Named("beta") = beta,
Rcpp::Named("objs") = objs);
}
示例2: sampleRho
void sampleRho(Rcpp::NumericVector& rho, Rcpp::NumericMatrix& A, arma::mat A_restrict, double rhoa, double rhob){
int k = A.ncol();
int p = A.nrow();
arma::rowvec A_maxnnz_col = arma::sum(A_restrict, 0);
for (int i=0; i<k; i++) {
NumericMatrix::Column col = A.column(i) ;
NumericVector As = ifelse(abs(col)<1e-10, 0.0, 1.0);
double nnz = std::accumulate(As.begin(), As.end(), 0.0);
double maxnnz = A_maxnnz_col(i);
rho(i) = Rf_rbeta(rhoa + nnz, rhob + maxnnz-nnz);
}
}
示例3: tam_rcpp_rowCumsums
///********************************************************************
///** tam_rcpp_rowCumsums
// [[Rcpp::export]]
Rcpp::NumericMatrix tam_rcpp_rowCumsums( Rcpp::NumericMatrix input )
{
Rcpp::NumericMatrix output = Rcpp::clone< Rcpp::NumericMatrix >(input);
int nr = input.nrow();
int nc = input.ncol();
Rcpp::NumericVector tmp(nr);
for( int i=0; i<nc; i++){
tmp = tmp + input.column(i);
Rcpp::NumericMatrix::Column target( output, i );
std::copy( tmp.begin(), tmp.end(), target.begin() );
}
return output;
}