#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "svm.h"
#include "mex.h"
#include "svm_model_matlab.h"
#ifdef MX_API_VER
#if MX_API_VER < 0x07030000
typedef int mwIndex;
#endif
#endif
#define CMD_LEN 2048
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
void print_null(const char *s) {}
void print_string_matlab(const char *s) {mexPrintf(s);}
void exit_with_help()
{
mexPrintf(
"Usage: model = svmtrain(training_label_vector, training_instance_matrix, 'libsvm_options');\n"
"libsvm_options:\n"
"-s svm_type : set type of SVM (default 0)\n"
" 0 -- C-SVC (multi-class classification)\n"
" 1 -- nu-SVC (multi-class classification)\n"
" 2 -- one-class SVM\n"
" 3 -- epsilon-SVR (regression)\n"
" 4 -- nu-SVR (regression)\n"
"-t kernel_type : set type of kernel function (default 2)\n"
" 0 -- linear: u'*v\n"
" 1 -- polynomial: (gamma*u'*v + coef0)^degree\n"
" 2 -- radial basis function: exp(-gamma*|u-v|^2)\n"
" 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n"
" 4 -- precomputed kernel (kernel values in training_instance_matrix)\n"
"-d degree : set degree in kernel function (default 3)\n"
"-g gamma : set gamma in kernel function (default 1/num_features)\n"
"-r coef0 : set coef0 in kernel function (default 0)\n"
"-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n"
"-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n"
"-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n"
"-m cachesize : set cache memory size in MB (default 100)\n"
"-e epsilon : set tolerance of termination criterion (default 0.001)\n"
"-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n"
"-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n"
"-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n"
"-v n : n-fold cross validation mode\n"
"-q : quiet mode (no outputs)\n"
);
}
// svm arguments
struct svm_parameter param; // set by parse_command_line
struct svm_problem prob; // set by read_problem
struct svm_model *model;
struct svm_node *x_space;
int cross_validation;
int nr_fold;
double do_cross_validation()
{
int i;
int total_correct = 0;
double total_error = 0;
double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
double *target = Malloc(double,prob.l);
double retval = 0.0;
svm_cross_validation(&prob,¶m,nr_fold,target);
if(param.svm_type == EPSILON_SVR ||
param.svm_type == NU_SVR)
{
for(i=0;i<prob.l;i++)
{
double y = prob.y[i];
double v = target[i];
total_error += (v-y)*(v-y);
sumv += v;
sumy += y;
sumvv += v*v;
sumyy += y*y;
sumvy += v*y;
}
mexPrintf("Cross Validation Mean squared error = %g\n",total_error/prob.l);
mexPrintf("Cross Validation Squared correlation coefficient = %g\n",
((prob.l*sumvy-sumv*sumy)*(prob.l*sumvy-sumv*sumy))/
((prob.l*sumvv-sumv*sumv)*(prob.l*sumyy-sumy*sumy))
);
retval = total_error/prob.l;
}
else
{
for(i=0;i<prob.l;i++)
if(target[i] == prob.y[i])
++total_correct;
mexPrintf("Cross Validation Accuracy = %g%%\n",100.0*total_correct/prob.l);
retval = 100.0*total_correct/prob.l;
}
free(target);
return retval;
}
// nrhs should be 3
int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name)
{
int i, argc = 1;
char cmd[CMD_LEN];
char *argv[CMD_LEN/2];
void (*print_func)(const char *) = print_string_matlab; // default printing to matlab display
// default values
param.svm_type = C_SVC;
param.kernel_type = RBF;
param.degree = 3;
param.gamma = 0; // 1/num_features
param.coef0 = 0;
param.nu = 0.5;
param.cache_size = 100;
param.C = 1;
param.eps = 1e-3;
param.p = 0.1;
param.shrinking = 1;
param.probability = 0;
param.nr_weight = 0;
param.weight_label = NULL;
param.weight = NULL;
cross_validation = 0;
if(nrhs <= 1)
return 1;
if(nrhs > 2)
{
// put options in argv[]
mxGetString(prhs[2], cmd, mxGetN(prhs[2]) + 1);
if((argv[argc] = strtok(cmd, " ")) != NULL)
while((argv[++argc] = strtok(NULL, " ")) != NULL)
;
}
// parse options
for(i=1;i<argc;i++)
{
if(argv[i][0] != '-') break;
++i;
if(i>=argc && argv[i-1][1] != 'q') // since option -q has no parameter
return 1;
switch(argv[i-1][1])
{
case 's':
param.svm_type = atoi(argv[i]);
break;
case 't':
param.kernel_type = atoi(argv[i]);
break;
case 'd':
param.degree = atoi(argv[i]);
break;
case 'g':
param.gamma = atof(argv[i]);
break;
case 'r':
param.coef0 = atof(argv[i]);
break;
case 'n':
param.nu = atof(argv[i]);
break;
case 'm':
param.cache_size = atof(argv[i]);
break;
case 'c':
param.C = atof(argv[i]);
break;
case 'e':
param.eps = atof(argv[i]);
break;
case 'p':
param.p = atof(argv[i]);
break;
case 'h':
param.shrinking = atoi(argv[i]);
break;
case 'b':
param.probability = atoi(argv[i]);
break;
case 'q':
print_func = &print_null;
i--;
break;
case 'v':
cross_validation = 1;
nr_fold = atoi(argv[i]);
if(nr_fold < 2)
{
mexPrintf("n-fold cross validation: n must >= 2\n");
return 1;
}
break;
case 'w':
++param.nr_weight;
param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight);
param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight);
param.weight_label[param.nr_weight-1] = atoi(&argv[i-1][2]);
param.weight[param.nr_weight-1] = atof(argv[i]);
break;
default:
mexPrintf("Unknown option -%c\n", argv[i-1][1]);
return 1;
}
}
svm_set_print_string_function(print_func);
return 0;
}
// read in a problem (in svmlight format)
int read_problem_dense(const mxArray *label_vec, const mxArray *instance_mat)
{
// using size_t due to the output type of matlab functions
size_t i, j, k, l;
size_t elements, max_index, sc, label_vector_row_num;
double *samples, *labels;
prob.x = NULL;
prob.y = NULL;
x_space = NULL;
labels = mxGetPr(label_vec);
samples = mxGetPr(instance_mat);
sc = mxGetN(instance_mat);
elements = 0;
// number of instances
l = mxGetM(instance_mat);
label_vector_row_num = mxGetM(label_vec);
prob.l = (int)l;
if(label_vector_row_num!=l)
{
mexPrintf("Length of label vector does not match # of instances.\n");
return -1;
}
if(param.kernel_type == PRECOMPUTED)
elements = l * (sc + 1);
else
{
for(i = 0; i < l; i++)
{
for(k = 0; k < sc; k++)
if(samples[k * l + i] != 0)
elements++;
// count the '-1' element
elements++;
}
}
prob.y = Malloc(double,l);
prob.x = Malloc(struct svm_node *,l);
x_space = Malloc(struct svm_node, elements);
max_index = sc;
j = 0;
for(i = 0; i < l; i++)
{
prob.x[i] = &x_space[j];
prob.y[i] = labels[i];
for(k = 0; k < sc; k++)
{
if(param.kernel_type == PRECOMPUTED || samples[k * l + i] != 0)
{
x_space[j].index = (int)k + 1;
x_space[j].value = samples[k * l + i];
j++;
}
}
x_space[j++].index = -1;
}
if(param.gamma == 0 && max_index > 0)
param.gamma = (double)(1.0/max_index);
if(param.kernel_type == PRECOMPUTED)
for(i=0;i<l;i++)
{
if((int)prob.x[i][0].value <= 0 || (int)prob.x[i][0].value > (int)max_index)
{
mexPrintf("Wrong input format: sample_serial_number out of range\n");
return -1;
}
}
return 0;
}
int read_problem_sparse(const mxArray *label_vec, const mxArray *instance_mat)
{
mwIndex *ir, *jc, low, high, k;
// using size_t due to the output type of matlab functions
size_t i, j, l, elements, max_index, label_vector_row_num;
mwSize num_samples;
double *samples, *labels;
mxArray *instance_mat_col; // transposed instance sparse matrix
prob.x = NULL;
prob.y = NULL;
x_space = NULL;
// transpose instance matrix
{
mxArray *prhs[1], *plhs[1];
prhs[0] = mxDuplicateArray(instance_mat);
if(mexCallMATLA
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该项目是一个基于C++和Python的地质识别系统源码,包含413个文件,涵盖278个头文件、39个文本文件、22个C++源文件、12个C源文件、8个Python脚本、4个CUDA源文件、2个JPG图像文件、2个MATLAB脚本、1个许可文件和1个Markdown文档。该系统专注于地质识别领域,适用于相关研究和应用开发。
资源推荐
资源详情
资源评论
收起资源包目录
基于C++和Python的地质识别travel_mod模块设计源码 (413个子文件)
Array 304B
svmtrain.c 12KB
svmpredict.c 10KB
svm-train.c 9KB
svm-train.c 9KB
svm-scale.c 8KB
svm_model_matlab.c 8KB
svm_model_matlab.c 8KB
svm-predict.c 5KB
libsvmread - 副本.c 4KB
libsvmread.c 4KB
libsvmwrite.c 2KB
loadmodel.c 1KB
Cholesky 775B
CholmodSupport 2KB
Core 12KB
svm.cpp 63KB
svm.cpp 63KB
svm.cpp 63KB
tr_detect.cpp 41KB
slic.cpp 26KB
ransac_plain.cpp 13KB
cvlabel.cpp 12KB
cvtrack.cpp 11KB
cvcontour.cpp 11KB
cvblob.cpp 10KB
elm.cpp 10KB
svm-predict.cpp 5KB
call1.cpp 5KB
main.cpp 4KB
FastImgSeg.cpp 4KB
cvblob2.cpp 4KB
cvcolor.cpp 3KB
cvaux.cpp 2KB
lbp.cpp 2KB
train.cpp 2KB
savemodel.cpp 1KB
outputVote.cpp 1020B
cudaSegSLIC.cu 11KB
cudaUtil.cu 5KB
cudaSegEngine.cu 3KB
cudaImgTrans.cu 2KB
svm.def 477B
Dense 122B
readme.docx 203KB
Eigen 37B
Eigen2Support 3KB
Eigenvalues 1KB
Geometry 2KB
Eigen_Colamd.h 60KB
Transform.h 54KB
GeneralBlockPanelKernel.h 45KB
SparseMatrix.h 42KB
CoreEvaluators.h 38KB
Memory.h 37KB
Functors.h 36KB
JacobiSVD.h 35KB
PlainObjectBase.h 34KB
blas.h 33KB
SuperLUSupport.h 32KB
AssignEvaluator.h 29KB
TriangularMatrix.h 29KB
SelfAdjointEigenSolver.h 29KB
CwiseNullaryOp.h 29KB
Quaternion.h 28KB
Transform.h 28KB
GeneralProduct.h 28KB
FullPivLU.h 27KB
DenseCoeffsBase.h 27KB
VectorwiseOp.h 26KB
PacketMath.h 25KB
BlockMethods.h 24KB
cvblob.h 24KB
PermutationMatrix.h 23KB
PaStiXSupport.h 23KB
GeneralMatrixVector.h 23KB
Assign.h 22KB
MatrixBase.h 22KB
FullPivHouseholderQR.h 22KB
SimplicialCholesky.h 22KB
RealQZ.h 22KB
DenseBase.h 22KB
SparseLU.h 22KB
Tridiagonalization.h 22KB
EigenSolver.h 21KB
MathFunctions.h 21KB
ColPivHouseholderQR.h 21KB
SparseQR.h 21KB
LDLT.h 20KB
PardisoSupport.h 20KB
CholmodSupport.h 20KB
RealSchur.h 19KB
HouseholderSequence.h 19KB
PacketMath.h 18KB
SVD.h 18KB
CoeffBasedProduct.h 18KB
TriangularMatrixMatrix.h 18KB
Complex.h 18KB
PartialPivLU.h 18KB
SparseMatrixBase.h 18KB
共 413 条
- 1
- 2
- 3
- 4
- 5
资源评论
wjs2024
- 粉丝: 2338
- 资源: 5466
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- bdwptqmxgj11.zip
- onnxruntime-win-x86
- onnxruntime-win-x64-gpu-1.20.1.zip
- vs2019 c++20 语法规范 头文件 <ratio> 的源码阅读与注释,处理分数的存储,加减乘除,以及大小比较等运算
- 首次尝试使用 Win,DirectX C++ 中的形状渲染套件.zip
- 预乘混合模式是一种用途广泛的三合一混合模式 它已经存在很长时间了,但似乎每隔几年就会被重新发现 该项目包括使用预乘 alpha 的描述,示例和工具 .zip
- 项目描述 DirectX 引擎支持版本 9、10、11 库 Microsoft SDK 功能相机视图、照明、加载网格、动画、蒙皮、层次结构界面、动画控制器、网格容器、碰撞系统 .zip
- 项目 wiki 文档中使用的代码教程的源代码库.zip
- 面向对象的通用GUI框架.zip
- 基于Java语言的PlayerBase游戏角色设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功