?? libsvmaux.c
字號:
mexPrintf("SVM type: EPS-SVR\n"); mexPrintf("Bound C: %g\n",C); mexPrintf("Epsilon: %g\n",e); } else { mexPrintf("SVM type: NU-SVR\n"); mexPrintf("Bound C: %g\n",C); mexPrintf("Nu: %g\n",e); } break; case 1: if (opt->style == 0) { mexPrintf("SVM type: C-SVC\n"); mexPrintf("Bound C: %g\n",C); } else { mexPrintf("SVM type: NU-SVC\n"); mexPrintf("Nu: %g\n",C); } break; case 2: mexPrintf("SVM type: ONE-CLASS\n"); mexPrintf("Nu: %g\n",C); break; } /* --- Kernel section --- */ switch ( ker->type ) { case KERNEL_LINEAR: mexPrintf("Kernel: Linear\n"); break; case KERNEL_POLY: mexPrintf("Kernel: Polynomial\n"); mexPrintf("Degree: %d\n",ker->degree); mexPrintf("Offset: %g\n",ker->offset); break; case KERNEL_RADIAL: mexPrintf("Kernel: Gauss\n"); /* 'radial' only for internal use */ mexPrintf("Width: %g\n",1/sqrt(2*ker->gamma)); break; case KERNEL_GAUSS: mexPrintf("Kernel: Gauss\n"); mexPrintf("Width: Vector, size %d\n", ker->gamman); break; case KERNEL_TANH: mexPrintf("Kernel: Tanh\n"); mexPrintf("Gamma: %g\n",ker->gamma); mexPrintf("Offset: %g\n",ker->offset); break; } /* --- Options section --- */ if ( svmproblem==1 && opt->style==0 ) { mexPrintf("Weight: %g\n",opt->weight); mexPrintf("Wght. Label: %d\n",opt->wlabel); } mexPrintf("Tolerance: %g\n",opt->tol); mexPrintf("Shrinking: %s\n",(opt->shrink ? "Yes" : "No")); mexPrintf("Cache: %2.2g MB\n",opt->cache); if ( svmproblem==1 ) mexPrintf("Probability: %s\n",(opt->prob ? "Yes" : "No")); mexPrintf("--------------------\n"); return 0;}/*----------------------------------------------------------------------------*\| Dump information about solution into MATLAB window. |\*----------------------------------------------------------------------------*/int dumpSolution( /* Return: always 0 */ options *opt, /* [in] option struct */ struct svm_model *model, /* [in] the model calculated by LIBSVM */ struct svm_problem *prob, /* [in] LIBSVM problem struct */ double *y, /* [in] output data */ double C, /* [in] C or nu */ int svmproblem, /* [in] 0: SVR, 1: SVC, 2: One-Class */ long N, /* [in] number of data */ int adjust ) /* [in] 0: -, 1: adjust to labels +1/-1 */{ mexPrintf("SOLUTION\n"); mexPrintf("#SVs: %d\n",model->l); /* --- determin bounded support vectors --- */ if ( model->sv_coef != NULL && (model->param.svm_type == C_SVC || model->param.svm_type == EPSILON_SVR || model->param.svm_type == NU_SVR) ) { long i; /* counter */ long bsv = 0; /* number of bounded SVs */ for (i=0; i<model->l; i++) { /* determine bounded SVs */ if ( fabs(model->sv_coef[0][i]) == C ) bsv++; } mexPrintf("#BSVs: %d\n",bsv); } /* --- compute bias term --- */ if (model->rho != NULL) { double f = 1; /* correction factor, see buildSolution */ if (adjust>0 && model->label != NULL) f = model->label[0]; mexPrintf("Bias: %g\n",-f*model->rho[0]); } /* --- compute epsilon for NU-SVR --- */ if (opt->style == 1 && svmproblem == 0) { double eps = 0; /* computed epsilon value */ long c = 0; /* number of free SVs */ long k,g; /* counters */ for (k=0; k<model->l; k++) { if (fabs(model->sv_coef[0][k]) < C && model->sv_coef[0][k] != 0.0) { for (g=0; g<N; g++) { if (prob->x[g] == model->SV[k]) { eps += fabs(y[g]-svm_predict(model,model->SV[k])); c++; break; } } } } mexPrintf("Epsilon: %g\n",eps/c); } mexPrintf("--------------------\n"); return 0;}/*----------------------------------------------------------------------------*\| Convert options to LIBSVM parameter struct. |\*----------------------------------------------------------------------------*/int convOptions( /* Return: always 0 */ struct svm_parameter *param, /* [out] LIBSVM parameter struct */ double C, /* [in] upper bound for Lagrange multipliers */ double e, /* [in] epsilon for regression case */ kernel *ker, /* [in] kernel struct */ options *opt, /* [in] options struct */ int svmproblem ) /* [in] 0: SVR, 1: SVC, 2: One-Class */{ param->eps = opt->tol; param->cache_size = opt->cache; param->shrinking = opt->shrink; param->probability = opt->prob; /* --- set SVM type, C, epsilon and nu --- */ if (svmproblem == 1) { /* classification case */ if (opt->style == 1) { /* the 'nu' case check */ param->nu = C; param->svm_type = NU_SVC; } else { param->C = C; param->svm_type = C_SVC; } } else if (svmproblem == 0 ) { /* regression case */ param->C = C; if (opt->style == 1) { /* the 'nu' case check */ param->nu = e; param->svm_type = NU_SVR; } else { param->p = e; param->svm_type = EPSILON_SVR; } } else { /* one-class case */ param->nu = C; param->svm_type = ONE_CLASS; } /* --- acivate weighting if needed --- */ if (opt->weight == 1) { param->nr_weight = 0; param->weight_label = NULL; param->weight = NULL; } else { param->nr_weight = 1; param->weight_label = (int*)malloc(1*sizeof(int)); param->weight_label[0] = opt->wlabel; param->weight = (double*)malloc(sizeof(double)); param->weight[0] = opt->weight; } /* --- get parameters from given MATLAB option struct --- */ if (ker->type == KERNEL_LINEAR) { /* if it shall be a linear kernel */ param->kernel_type = LINEAR; /* make it a linear one */ } else if (ker->type == KERNEL_POLY) { /* if it shall be a polyl kernel */ param->kernel_type = POLY; /* make it a polynomial one */ param->gamma = 1; /* k(x,y)=(gamma*x'*y+coef0)^degree */ param->degree = ker->degree; /* degree of polynomial kernel */ param->coef0 = ker->offset; /* coef0 is offset in svmas */ } else if (ker->type == KERNEL_TANH) { /* if it shall be a tanh kernel */ param->kernel_type = SIGMOID; /* make it a sigmoid one */ param->gamma = ker->gamma; /* k(x,y)=(gamma*x'*y+coef0)^degree */ param->coef0 = ker->offset; /* coef0 is offset in svmas */ } else if (ker->type == KERNEL_RADIAL) { /* if it shall be a Gauss kernel */ param->kernel_type = RBF; /* use the RBF one, it's the same */ param->gamma = ker->gamma; /* and use the same gamma-value */ } return 0;}/*----------------------------------------------------------------------------*\| Convert data to LIBSVM problem struct. |\*----------------------------------------------------------------------------*/int convData( /* Return: always 0 */ struct svm_problem *prob, /* [out] LIBSVM problem struct */ double *x, /* [in] input data */ double *y, /* [in] output data */ int n, /* [in] input dimension */ long N ) /* [in] number of data */{ struct svm_node *x_space; /* svm_nodes needed to create the svm-problem */ int j1; /* rows in x-matrix */ int i; /* coloumns in x-matrix */ int j2; /* counter for svm_nodes*/ /* --- allocate needed memory --- */ prob->l = N; prob->x = mxMalloc(N*sizeof(struct svm_node *)); prob->y = mxMalloc(N*sizeof(double)); x_space = mxMalloc(N*(n+1)*sizeof(struct svm_node)); /* --- build the SVM nodes --- */ j1 = 0; for(i=0;i<N;i++) { /* iterate rows */ prob->x[i] = x_space+j1; /* set pointer to the svm_node */ if (y==NULL) prob->y[i] = 1; else prob->y[i] = y[i]; for(j2=0;j2<n;j2++) { /* iterate columns in current row */ x_space[j1].index=j2; /* create index-value of svm_node */ x_space[j1].value=x[i+N*j2]; /* create value-value of svm_node */ j1++; } x_space[j1].index = -1; } return 0;}/*----------------------------------------------------------------------------*\| Build MATLAB solution from LIBSVM model. |\*----------------------------------------------------------------------------*/int buildSolution( /* Return: always 0 */ mxArray **solut, /* [out] solution struct (empty on entry) */ const mxArray *mlker, /* [in] kernel as provided by MATLAB */ struct svm_model *model, /* [in] the model calculated by LIBSVM */ options *opt, /* [in] options struct */ int nlhs, /* [in] number of output arguments */ int svmproblem, /* [in] 0: SVR, 1: SVC, 2: One-Class */ long N ) /* [in] number of input data */{ long i,j,k,g; /* counters */ long Nsv; /* number of support vectors */ long n; /* input dimension */ double f; /* correction factor */ double *a; /* coefficients in MATLAB struct */ double *x; /* support vectors in MATLAB struct */ double **sv_coef; /* computed SVM coefficients */ struct svm_node **SV; /* support vectors */ struct svm_node *row; /* row of SV */ const char *fields[] = {FNAME_COEF, FNAME_VECT, FNAME_BIAS, FNAME_KER}; /* --- determine correction factor --- */ f = 1; /* regression, and class. with arbitrary labels */ if (nlhs>1 && model->label != NULL) /* class. with labels +1 and -1 */ f = model->label[0]; /* first label is regarded as +1 (!) */ /* --- return reduced coef vector, indices, and bias --- */ if (nlhs>2) { solut[0] = mxCreateDoubleMatrix(model->l,1,mxREAL); solut[1] = mxCreateDoubleMatrix(model->l,1,mxREAL); solut[2] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(solut[2])[0] = -f*model->rho[0]; for (g=0;g<model->l;g++) { for (k=0;k<N;k++) { if (prob->x[k] == model->SV[g] ) { mxGetPr(solut[0])[g] = f*model->sv_coef[0][g]; mxGetPr(solut[1])[g] = k+1; break; } } } } /* --- return complete coef vector, and bias --- */ else if (nlhs>1) { solut[0] = mxCreateDoubleMatrix(N,1,mxREAL); solut[1] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(solut[1])[0] = -f*model->rho[0]; for (g=0;g<model->l;g++) { for (k=0;k<N;k++) { if (model->SV[g] == prob->x[k]) { mxGetPr(solut[0])[k] = f*model->sv_coef[0][g]; break; } } } } /* --- return SVM struct --- */ else { Nsv = model->l; sv_coef = model->sv_coef; SV = model->SV; /* find the input dimension */ n = 0; for (i=0; i<Nsv; i++) { row = SV[i]; j = 0; while (row[j].index != -1) { if (row[j].index > n) n = row[j].index; j++; } } n++; /* because indices start from 0 */ /* prepare the solution struct */ *solut = mxCreateStructMatrix(1,1,4,fields); mxSetField(*solut,0,FNAME_COEF,mxCreateDoubleMatrix(Nsv,1,mxREAL)); mxSetField(*solut,0,FNAME_VECT,mxCreateDoubleMatrix(Nsv,n,mxREAL)); a = mxGetPr(mxGetField(*solut,0,FNAME_COEF)); x = mxGetPr(mxGetField(*solut,0,FNAME_VECT)); mxSetField(*solut,0,FNAME_BIAS,mxCreateDoubleMatrix(1,1,mxREAL)); mxGetPr(mxGetField(*solut,0,FNAME_BIAS))[0] = -f * *(model->rho); mxSetField(*solut,0,FNAME_KER,mxDuplicateArray(mlker)); if ( svmproblem == 1 && model->label != NULL ) { mxAddField(*solut,FNAME_LABEL); mxSetField(*solut,0,FNAME_LABEL,mxCreateDoubleMatrix(1,2,mxREAL)); mxGetPr(mxGetField(*solut,0,FNAME_LABEL))[0] = model->label[0]; mxGetPr(mxGetField(*solut,0,FNAME_LABEL))[1] = model->label[1]; } if ( svmproblem == 1 && opt->prob != 0 && model->probA != NULL && model->probB != NULL) { mxAddField(*solut,FNAME_PROB); mxSetField(*solut,0,FNAME_PROB,mxCreateDoubleMatrix(1,2,mxREAL)); mxGetPr(mxGetField(*solut,0,FNAME_PROB))[0] = model->probA[0]; mxGetPr(mxGetField(*solut,0,FNAME_PROB))[1] = model->probB[0]; } /* copy LIBSVM model to MATLAB */ for (i=0; i<Nsv; i++) { a[i] = f * sv_coef[0][i]; /* we are using only output #0 */ row = SV[i]; j = 0; while (row[j].index != -1) { /* x points to a row of the MATLAB matrix */ x[Nsv*(row[j].index)] = row[j].value; j++; } x++; } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -