SVM svm_predict_values详解

void svm_predict_values(const svm_model *model, const svm_node *x, double* dec_values)
{
if(model->param.svm_type == ONE_CLASS ||
  model->param.svm_type == EPSILON_SVR ||
  model->param.svm_type == NU_SVR)
{
double *sv_coef = model->sv_coef[0];
double sum = 0;
for(int i=0;il;i++)
sum += sv_coef[i] * Kernel::k_function(x,model->SV[i],model->param);
sum -= model->rho[0];
*dec_values = sum;
}
else
{
int i;
int nr_class = model->nr_class;
int l = model->l;
double *kvalue = Malloc(double,l);
for(i=0;i
{
kvalue[i] = Kernel::k_function(x,model->SV[i],model->param);
// printf("kvalue[%d]: %lf \n",i,kvalue[i]) ;
}

int *start = Malloc(int,nr_class);
start[0] = 0;
for(i=1;i
start[i] = start[i-1]+model->nSV[i-1];   //正负支持向量的个数

int p=0;
for(i=0;i
for(int j=i+1;j
{
double sum = 0;
int si = start[i];
int sj = start[j];
int ci = model->nSV[i];
int cj = model->nSV[j];
int k;
double *coef1 = model->sv_coef[j-1];

double *coef2 = model->sv_coef[i];
for(k=0;k
sum += coef1[si+k] * kvalue[si+k];
for(k=0;k
sum += coef2[sj+k] * kvalue[sj+k];
sum -= model->rho[p];
dec_values[p] = sum;
p++;
}

free(kvalue);
free(start);
}
}

假如有n类,则上述循环每次乘的系数如下表所示,其中(1,2)代表当第一类和第二类比较时,与第一类对应的向量乘的系数,(1,2)代表当第一类和第二类比较时,与第二类对应的向量乘的系数,依次类推。且(1,2)是一个列向量,如果第一类有m个向量与之对应,则(1,2)就是一个m维的列向量。

(1,2)

(1,3)

(1,4)

(1,5)

(1,n)

(2,1)

(2,3)

(2,4)

(2,5)

(3,n)

(3,1)

(3,2)

(3,4)

(3,5)

(4,n)

(4,1)

(4,2)

(4,3)

(4,5)

(5,n)

(5,1)

(5,2)

(5,3)

(5,4)

(6,n)

(n,1)

(n,2)

(n,3)

(n,4)

(n,n-1)


你可能感兴趣的:(SVM svm_predict_values详解)