问题描述
我正在尝试将站点LARS中MATLAB中的lars代码转换为犰狳。我实现了lars部分,它可以正常工作。但是套索修改部分有一些问题。删除变量后,我的代码无法正确运行。代码的相关部分如下。
在代码中,我用C ++实现了cholinsert
和choldelete
。此外,add
,add1
和delete
函数可以将变量插入或删除有效集和无效集。
当我在套索中必须放置变量的设置上运行代码时,出现以下错误消息:
Step Add Drop Active set size
1 19 x 1
2 29 x 2
3 39 x 3
4 28 x 4
5 25 x 5
6 16 x 6
7 38 x 7
8 5 x 8
9 3 x 9
10 6 x 10
11 13 x 11
12 8 x 12
13 30 x 13
14 31 x 14
15 15 x 15
16 21 x 16
17 26 x 17
18 11 x 18
19 10 x 19
19 x 2 18
error: element-wise multiplication: incompatible matrix dimensions: 19x19 and 18x19
Error in lars_lasso(X,y,"lasso",T,T) :
element-wise multiplication: incompatible matrix dimensions: 19x19 and 18x19
那么...我的错误在哪里?
在MATLAB中:
if ~lassocond % if a variable has been dropped,do one iteration with this configuration (don't add new one right away)
if ~useGram
R = cholinsert(R,X(:,j),A));
end
A = [A j];
I(I == j) = [];
vars = vars + 1;
if trace
disp(sprintf('%d\t\t%d\t\t\t\t\t%d',k,j,vars));
end
end
% LASSO modification
if lasso
lassocond = 0;
temp = -beta(k,A)./w';
[gamma_tilde] = min([temp(temp > 0) gamma]);
j = find(temp == gamma_tilde);
if gamma_tilde < gamma,gamma = gamma_tilde;
lassocond = 1;
end
end
% If LASSO condition satisfied,drop variable from active set
if lassocond == 1
if ~useGram
R = choldelete(R,j);
end
I = [I A(j)];
A(j) = [];
vars = vars - 1;
if trace
disp(sprintf('%d\t\t\t\t%d\t\t\t%d',vars));
end
end
在C ++(Armadillo)中:
if (!lassocond){ // if a variable has been dropped,do one iteration with this configuration (don't add new one right away)
if (!useGram){
R = cholinsert(R,X.col(j),X.cols(A));
}
A = add(A,j);
I = delete(I,j);
vars = vars + 1;
if (trace){
Rcpp::Rcout << k+1 << "\t" << j+1 << "\t" << "x" << "\t" << vars << std::endl;
}
}
// LASSO modification
if (lasso){
lassocond = false;
kvec = {k};
temp = -beta.submat(kvec,A)/w.t();
arma::vec tempff = temp.elem(arma::find(temp > 0));
double gamma_tilde = add1(tempff,gamma).min();
arma::uvec indj = arma::find(tempff == gamma_tilde);
j = indj(0);
if (gamma_tilde < gamma){
gamma = gamma_tilde;
lassocond = true;
}
}
// If LASSO condition satisfied,drop variable from active set
if (lassocond){
if (!useGram){
R = choldelete(R,j);
}
I = add(I,A(j));
A = delete(A,j);
vars = vars - 1;
if (trace){
Rcpp::Rcout << k+1 << "\t" << "x" << "\t" << j+1 << "\t" << vars << std::endl;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)