资源简介
matlab中实现支持向量机SVM完整工具包,可直接导入使用!
代码片段和文件信息
#include
#include
#include
#include
#include
#include “svm.h“
int print_null(const char *s...) {return 0;}
static int (*info)(const char *fmt...) = &printf;
struct svm_node *x;
int max_nr_attr = 64;
struct svm_model* model;
int predict_probability=0;
static char *line = NULL;
static int max_line_len;
static char* readline(FILE *input)
{
int len;
if(fgets(linemax_line_leninput) == NULL)
return NULL;
while(strrchr(line‘\n‘) == NULL)
{
max_line_len *= 2;
line = (char *) realloc(linemax_line_len);
len = (int) strlen(line);
if(fgets(line+lenmax_line_len-leninput) == NULL)
break;
}
return line;
}
void exit_input_error(int line_num)
{
fprintf(stderr“Wrong input format at line %d\n“ line_num);
exit(1);
}
void predict(FILE *input FILE *output)
{
int correct = 0;
int total = 0;
double error = 0;
double sump = 0 sumt = 0 sumpp = 0 sumtt = 0 sumpt = 0;
int svm_type=svm_get_svm_type(model);
int nr_class=svm_get_nr_class(model);
double *prob_estimates=NULL;
int j;
if(predict_probability)
{
if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
info(“Prob. model for test data: target value = predicted value + z\nz: Laplace distribution e^(-|z|/sigma)/(2sigma)sigma=%g\n“svm_get_svr_probability(model));
else
{
int *labels=(int *) malloc(nr_class*sizeof(int));
svm_get_labels(modellabels);
prob_estimates = (double *) malloc(nr_class*sizeof(double));
fprintf(output“labels“);
for(j=0;j fprintf(output“ %d“labels[j]);
fprintf(output“\n“);
free(labels);
}
}
max_line_len = 1024;
line = (char *)malloc(max_line_len*sizeof(char));
while(readline(input) != NULL)
{
int i = 0;
double target_label predict_label;
char *idx *val *label *endptr;
int inst_max_index = -1; // strtol gives 0 if wrong format and precomputed kernel has start from 0
label = strtok(line“ \t\n“);
if(label == NULL) // empty line
exit_input_error(total+1);
target_label = strtod(label&endptr);
if(endptr == label || *endptr != ‘\0‘)
exit_input_error(total+1);
while(1)
{
if(i>=max_nr_attr-1) // need one more for index = -1
{
max_nr_attr *= 2;
x = (struct svm_node *) realloc(xmax_nr_attr*sizeof(struct svm_node));
}
idx = strtok(NULL“:“);
val = strtok(NULL“ \t“);
if(val == NULL)
break;
errno = 0;
x[i].index = (int) strtol(idx&endptr10);
if(endptr == idx || errno != 0 || *endptr != ‘\0‘ || x[i].index <= inst_max_index)
exit_input_error(total+1);
else
inst_max_index = x[i].index;
errno = 0;
x[i].value = strtod(val&endptr);
if(endptr == val || errno != 0 || (*endptr != ‘\0‘ && !isspace(*endptr)))
exit_input_error(total+1);
++i;
}
x[i].index = -1;
if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC))
{
predict_label = svm_predict_probability(modelxprob_estimates);
fprintf(output“%g“predict_label);
for
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-07-28 09:53 libsvm-3.22\
文件 1497 2016-12-22 02:58 libsvm-3.22\COPYRIGHT
文件 83089 2016-12-22 02:58 libsvm-3.22\FAQ.html
文件 732 2016-12-22 02:58 libsvm-3.22\Makefile
文件 1136 2016-12-22 02:58 libsvm-3.22\Makefile.win
文件 28644 2016-12-22 02:58 libsvm-3.22\README
文件 27670 2016-12-22 02:58 libsvm-3.22\heart_scale
目录 0 2017-07-28 09:53 libsvm-3.22\java\
文件 659 2016-12-22 02:58 libsvm-3.22\java\Makefile
目录 0 2017-07-28 09:53 libsvm-3.22\java\libsvm\
文件 64242 2016-12-22 02:59 libsvm-3.22\java\libsvm\svm.java
文件 63439 2016-12-22 02:58 libsvm-3.22\java\libsvm\svm.m4
文件 868 2016-12-22 02:58 libsvm-3.22\java\libsvm\svm_model.java
文件 115 2016-12-22 02:58 libsvm-3.22\java\libsvm\svm_node.java
文件 1288 2016-12-22 02:58 libsvm-3.22\java\libsvm\svm_parameter.java
文件 87 2016-12-22 02:58 libsvm-3.22\java\libsvm\svm_print_interface.java
文件 136 2016-12-22 02:58 libsvm-3.22\java\libsvm\svm_problem.java
文件 55185 2016-12-22 02:59 libsvm-3.22\java\libsvm.jar
文件 4950 2016-12-22 02:58 libsvm-3.22\java\svm_predict.java
文件 8944 2016-12-22 02:58 libsvm-3.22\java\svm_scale.java
文件 12269 2016-12-22 02:58 libsvm-3.22\java\svm_toy.java
文件 8355 2016-12-22 02:58 libsvm-3.22\java\svm_train.java
文件 81 2016-12-22 02:58 libsvm-3.22\java\test_ap
目录 0 2017-07-28 09:53 libsvm-3.22\matlab\
文件 1240 2016-12-22 02:58 libsvm-3.22\matlab\Makefile
文件 9826 2016-12-22 02:58 libsvm-3.22\matlab\README
文件 4063 2016-12-22 02:58 libsvm-3.22\matlab\libsvmread.c
文件 10752 2017-05-19 10:35 libsvm-3.22\matlab\libsvmread.mexw64
文件 2341 2016-12-22 02:58 libsvm-3.22\matlab\libsvmwrite.c
文件 9728 2017-05-19 10:35 libsvm-3.22\matlab\libsvmwrite.mexw64
文件 888 2016-12-22 02:58 libsvm-3.22\matlab\make.m
............此处省略47个文件信息
评论
共有 条评论