📄 svm.h
字号:
#ifndef _LIBSVM_H#define _LIBSVM_H#ifdef __cplusplusextern "C" {#endifstruct svm_node{ int index;//index = -1 indicates the end of one vector double value;};struct svm_problem{ int l;//the number of training data double *y;//an array containing their target values. (integers in classification, real numbers in regression) struct svm_node **x;//an array of pointers, each of which points to a sparse
//representation (array of svm_node) of one training vector};enum { C_SVC, NU_SVC, ONE_CLASS, ONE_CLASS_MY,EPSILON_SVR, NU_SVR }; /* svm_type */enum { LINEAR, POLY, RBF, SIGMOID,MY }; /* kernel_type */struct svm_parameter{ int svm_type; int kernel_type; double degree; /* for poly */ double gamma; /* for poly/rbf/sigmoid */ double coef0; /* for poly/sigmoid */ /* these are for training only */ double cache_size; /* in MB */ double eps; /* stopping criteria */ double C; /* for C_SVC, EPSILON_SVR and NU_SVR */ int nr_weight; /* for C_SVC */ int *weight_label; /* for C_SVC */ double* weight; /* for C_SVC */ double nu; /* for NU_SVC, ONE_CLASS, and NU_SVR */ double p; /* for EPSILON_SVR */ int shrinking; /* use the shrinking heuristics */ int probability; /* do probability estimates */};
struct svm_model
{
svm_parameter param; // parameter
int nr_class; // number of classes, = 2 in regression/one class svm
int l; // total #SV
svm_node **SV; // SVs (SV[l])
svm_node **SV2; // SVs (SV[l])
double **sv_coef; // coefficients for SVs in decision functions (sv_coef[n-1][l])
double *rho; // constants in decision functions (rho[n*(n-1)/2])
double *probA; // pariwise probability information
double *probB;
// for classification only
int *label; // label of each class (label[n])
int *nSV; // number of SVs for each class (nSV[n])
// nSV[0] + nSV[1] + ... + nSV[n-1] = l
// XXX
int free_sv; // 1 if svm_model is created by svm_load_model
// 0 if svm_model is created by svm_train
};/*This function constructs and returns an SVM model according to
the given training data and parameters*/
struct svm_model *svm_trainmy(const svm_problem *prob,const struct svm_problem *prob2, const svm_parameter *param);
int svm_save_model(const char *model_file_name, const struct svm_model *model);//保存模型
struct svm_model *svm_load_model(const char *model_file_name);//登陆模型
void svm_cross_validation(const struct svm_problem *prob, const struct svm_parameter *param, int nr_fold, double *target);/*This function conducts cross validation. Data are separated to
nr_fold folds. Under given parameters, sequentially each fold is
validated using the model from training the remaining. Predicted
labels in the validation process are stored in the array called
target*/
int svm_get_svm_type(const struct svm_model *model);
int svm_get_nr_class(const struct svm_model *model);
/* For a classification model, this function gives the number of
classes. For a regression or an one-class model, 2 is returned*/
void svm_get_labels(const struct svm_model *model, int *label);
/* For a classification model, this function outputs the name of
labels into an array called label. For regression and one-class
models, label is unchanged*/
double svm_get_svr_probability(const struct svm_model *model);/* For a regression model with probability information, this function
outputs a value sigma > 0. For test data, we consider the
probability model: target value = predicted value + z, z: Laplace
distribution e^(-|z|/sigma)/(2sigma)
If the model is not for svr or does not contain required
information, 0 is returned*/
void svm_predict_values(const struct svm_model *model, const struct svm_node *x,const struct svm_node *x2, double* dec_values);
/* This function gives decision values on a test vector x given a
model.
For a classification model with nr_class classes, this function
gives nr_class*(nr_class-1)/2 decision values in the array
dec_values, where nr_class can be obtained from the function
svm_get_nr_class. The order is label[0] vs. label[1], ...,
label[0] vs. label[nr_class-1], label[1] vs. label[2], ...,
label[nr_class-2] vs. label[nr_class-1], where label can be
obtained from the function svm_get_labels.
For a regression model, label[0] is the function value of x
calculated using the model. For one-class model, label[0] is +1 or
-1.*/
double svm_predict(const struct svm_model *model, const struct svm_node *x, const struct svm_node *x2);
/*This function does classification or regression on a test vector x
given a model.
For a classification model, the predicted class for x is returned.
For a regression model, the function value of x calculated using
the model is returned. For an one-class model, +1 or -1 is
returned*/
double svm_predict_probability(const struct svm_model *model, const struct svm_node *x, double* prob_estimates);/*This function does classification or regression on a test vector x
given a model with probability information.
For a classification model with probability information, this
function gives nr_class probability estimates in the array
prob_estimates. nr_class can be obtained from the function
svm_get_nr_class. The class with the highest probability is
returned. For all other situations, the array prob_estimates is
unchanged and the returned value is the same as that of
svm_predict */
void svm_destroy_model(struct svm_model *model);//释放内存void svm_destroy_param(struct svm_parameter *param);//释放内存const char *svm_check_parameter(const struct svm_problem *prob, const struct svm_parameter *param);
/*This function checks whether the parameters are within the feasible
range of the problem. This function should be called before calling
svm_train(). It returns NULL if the parameters are feasible,
otherwise an error message is returned */
int svm_check_probability_model(const struct svm_model *model);/* This function checks whether the model contains required
information to do probability estimates. If so, it returns
+1. Otherwise, 0 is returned. This function should be called
before calling svm_get_svr_probability and
svm_predict_probability*/
#ifdef __cplusplus}#endif#endif /* _LIBSVM_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -