虽然我自己从来没有做过,但可以在 MATLAB 中运行 python 代码。我出于与您自己相同的原因进行了调查,发现正如@Mihia Todor 所说,编写自己的grid.py版本会更容易。这是我编写的基本代码,用于在 MATLAB 中使用 LIBSVM 进行网格搜索和交叉验证:
gamma=1;
cost=1;
J=10;
K=12;
kernal=2; %RBF
besterr=[];
bestc=[];
bestg=[];
for j=1:J;
gamma=2^(2*(j-round(J/3))); %Calculates a nice spread of search numbers centred above zero
for k=1:K;
cost=2^(2*(k-round(K/3)));
err=svmtrain(y,x,sprintf('-s 4 -t %g -v 5 -c %g -g %g -q', kernal, cost, gamma)); %Nu-SVR change -s if you want SVC
if isempty(besterr)|err<besterr;
besterr=err;
bestc=cost;
bestg=gamma;
end
end
end
besterr=sqrt(besterr) %Prints the average RMSD of the 5-fold cross-validation
bestg %Prints best gamma
bestc %Prints best cost
model=svmtrain(y,x,sprintf('-s 4 -t %g -c %g -g %g -q', kernal, bestc, bestg)); %Retrain using new c and g
假设您已经扩展了稀疏的 x 数据,这应该可以开箱即用。
如果您确实希望继续使用 grid.py 并拥有 2014b,这可能是一个有用的起点:
Call Python Libraries.
如果您没有 2014b 或更新版本,请使用 Call Python function from MATLAB。
如果您让这两种方法中的任何一种起作用,请写下您自己的问题答案。我很乐意看到它们工作,我相信其他人会发现它非常有用!