Algorithm-SVM
view release on metacpan or search on metacpan
Revision history for Perl extension Algorithm::SVM.
0.01 Thu Jun 27 11:16:17 2002
- original version; created by h2xs 1.21 with options
-n Algorithm::SVM svm.h
0.02 Mon Sep 23 13:12:51 EDT 2002
- compilation errors on Mac OS X fixed, other bug fixes
and code cleanups.
0.05 Tue Nov 26 10:46:46 PST 2002
- fixed build error for perl-5.8.0 with ithreads. Thanks
to Mike Castle for this report and patch.
0.06 Tue Jan 14 16:59:50 PST 2003
- fixed a build error for gcc version 3.x
0.07 Tue Jun 3 14:46:26 PDT 2003
- added a cygwin target to Makefile.PL - Algorithm::SVM
should now compile under Windows.
- Algorithm::SVM now has a new maintainer. As of version
0.07, all inquiries, patches and comments should be
sent to Matthew Laird <matt@brinkman.mbb.sfu.ca>
0.08 Mon May 17 15:09:21 PDT 2004
- Upgraded libsvm to 2.6, added new bindings for most
4) BUGS
-------
If you find a bug, please report it to the author along with the
following information:
* version of Perl (output of 'perl -V' is best)
* version of Algorithm::SVM
* operating system type and version
* exact text of error message or description of problem
* example model files/data being classified
If we don't have access to a system similar to yours, you may be asked
to insert some debugging lines and report back on the results.
The more help and information you can provide, the better.
5) SVM COPYRIGHT AND LICENCE
----------------------------
The Perl Algorithm::SVM module is Copyright (C) 2002 Cory Spencer and
bindings.cpp view on Meta::CPAN
idx-=2; while (idx >= 0 && x_space[idx].index!=-1) { idx--; }
idx++;
}
}
assert(idx==0);
free(x_space); x_space=NULL;
}
}
int SVM::train(int retrain) {
const char *error;
// Free any old model we have.
if(model != NULL) {
svm_destroy_model(model);
model = NULL;
}
if(retrain) {
if(prob == NULL) return 0;
model = svm_train(prob, ¶m);
bindings.cpp view on Meta::CPAN
prob->y = (double *)malloc(sizeof(double) * prob->l);
prob->x = (struct svm_node **)malloc(sizeof(struct svm_node *) * prob->l);
if((prob->y == NULL) || (prob->x == NULL)) {
if(prob->y != NULL) free(prob->y);
if(prob->x != NULL) free(prob->x);
free(prob);
return 0;
}
// Check for errors with the parameters.
error = svm_check_parameter(prob, ¶m);
if(error) { free(prob->x); free (prob->y); free(prob); return 0; }
// Allocate x_space and successively release dataset memory
// (realigning the dataset memory to x_space)
nelem=0;
for (unsigned int i=0; i<dataset.size(); i++) {
nelem+=dataset[i]->n+1;
}
x_space = (struct svm_node *)malloc(sizeof(struct svm_node)*nelem);
long idx=0;
for (unsigned int i=0; i<dataset.size(); i++) {
bindings.cpp view on Meta::CPAN
if((tmodel = svm_load_model(filename)) != NULL) {
model = tmodel;
return 1;
}
return 0;
}
double SVM::crossValidate(int nfolds) {
double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
double total_error = 0;
int total_correct = 0;
int i;
if(! prob) return 0;
if(! randomized) {
// random shuffle
for(i=0;i<prob->l;i++) {
int j = i+rand()%(prob->l-i);
struct svm_node *tx;
bindings.cpp view on Meta::CPAN
}
for(j=end;j<prob->l;j++) {
subprob.x[k] = prob->x[j];
subprob.y[k] = prob->y[j];
++k;
}
if(param.svm_type == EPSILON_SVR || param.svm_type == NU_SVR) {
struct svm_model *submodel = svm_train(&subprob,¶m);
double error = 0;
for(j=begin;j<end;j++) {
double v = svm_predict(submodel,prob->x[j]);
double y = prob->y[j];
error += (v-y)*(v-y);
sumv += v;
sumy += y;
sumvv += v*v;
sumyy += y*y;
sumvy += v*y;
}
svm_destroy_model(submodel);
// cout << "Mean squared error = %g\n", error/(end-begin));
total_error += error;
} else {
struct svm_model *submodel = svm_train(&subprob,¶m);
int correct = 0;
for(j=begin;j<end;j++) {
double v = svm_predict(submodel,prob->x[j]);
if(v == prob->y[j]) ++correct;
}
svm_destroy_model(submodel);
//cout << "Accuracy = " << 100.0*correct/(end-begin) << " (" <<
lib/Algorithm/SVM/DataSet.pm view on Meta::CPAN
=head1 SEE ALSO
Algorithm::SVM
=cut
sub new {
my ($class, %args) = @_;
# Do some quick error checking on the values we've been passed.
croak("No label specified for DataSet") if(! exists($args{Label}));
my $self = _new_dataset($args{Label} + 0);
if(exists($args{Data})) {
croak("Data must be an array ref") if(ref($args{Data}) ne "ARRAY");
for(my $i = 0; $i < @{$args{Data}}; $i++) {
$self->attribute($i, (@{$args{Data}})[$i] + 0);
}
}
{
// stopping condition, recalculate QP,pQP for numerical accuracy
pQp=0;
for (t=0;t<k;t++)
{
Qp[t]=0;
for (j=0;j<k;j++)
Qp[t]+=Q[t][j]*p[j];
pQp+=p[t]*Qp[t];
}
double max_error=0;
for (t=0;t<k;t++)
{
double error=fabs(Qp[t]-pQp);
if (error>max_error)
max_error=error;
}
if (max_error<eps) break;
for (t=0;t<k;t++)
{
double diff=(-Qp[t]+pQp)/Q[t][t];
p[t]+=diff;
pQp=(pQp+diff*(diff*Q[t][t]+2*Qp[t]))/(1+diff)/(1+diff);
for (j=0;j<k;j++)
{
Qp[j]=(Qp[j]+diff*Q[t][j])/(1+diff);
p[j]/=(1+diff);
if(param.kernel_type == PRECOMPUTED)
fprintf(fp,"0:%d ",(int)(p->value));
else
while(p->index != -1)
{
fprintf(fp,"%d:%.8g ",p->index,p->value);
p++;
}
fprintf(fp, "\n");
}
if (ferror(fp) != 0 || fclose(fp) != 0) return -1;
else return 0;
}
svm_model *svm_load_model(const char *model_file_name)
{
FILE *fp = fopen(model_file_name,"r");
if(fp==NULL) return NULL;
// read parameters
c = getc(fp);
if(c=='\n') goto out2;
} while(isspace(c));
ungetc(c,fp);
fscanf(fp,"%d:%lf",&(x_space[j].index),&(x_space[j].value));
++j;
}
out2:
x_space[j++].index = -1;
}
if (ferror(fp) != 0 || fclose(fp) != 0) return NULL;
model->free_sv = 1; // XXX
return model;
}
void svm_destroy_model(svm_model* model)
{
if(model->free_sv && model->l > 0)
free((void *)(model->SV[0]));
for(int i=0;i<model->nr_class-1;i++)
( run in 0.290 second using v1.01-cache-2.11-cpan-65fba6d93b7 )