Algorithm-LibLinear
view release on metacpan or search on metacpan
src/liblinear/linear.cpp view on Meta::CPAN
return iter;
}
// transpose matrix X from row format to column format
static void transpose(const problem *prob, feature_node **x_space_ret, problem *prob_col)
{
int i;
int l = prob->l;
int n = prob->n;
size_t nnz = 0;
size_t *col_ptr = new size_t [n+1];
feature_node *x_space;
prob_col->l = l;
prob_col->n = n;
prob_col->y = new double[l];
prob_col->x = new feature_node*[n];
for(i=0; i<l; i++)
prob_col->y[i] = prob->y[i];
for(i=0; i<n+1; i++)
col_ptr[i] = 0;
for(i=0; i<l; i++)
{
feature_node *x = prob->x[i];
while(x->index != -1)
{
nnz++;
col_ptr[x->index]++;
x++;
}
}
for(i=1; i<n+1; i++)
col_ptr[i] += col_ptr[i-1] + 1;
x_space = new feature_node[nnz+n];
for(i=0; i<n; i++)
prob_col->x[i] = &x_space[col_ptr[i]];
for(i=0; i<l; i++)
{
feature_node *x = prob->x[i];
while(x->index != -1)
{
int ind = x->index-1;
x_space[col_ptr[ind]].index = i+1; // starts from 1
x_space[col_ptr[ind]].value = x->value;
col_ptr[ind]++;
x++;
}
}
for(i=0; i<n; i++)
x_space[col_ptr[i]].index = -1;
*x_space_ret = x_space;
delete [] col_ptr;
}
// label: label name, start: begin of each class, count: #data of classes, perm: indices to the original data
// perm, length l, must be allocated before calling this subroutine
static void group_classes(const problem *prob, int *nr_class_ret, int **label_ret, int **start_ret, int **count_ret, int *perm)
{
int l = prob->l;
int max_nr_class = 16;
int nr_class = 0;
int *label = Malloc(int,max_nr_class);
int *count = Malloc(int,max_nr_class);
int *data_label = Malloc(int,l);
int i;
for(i=0;i<l;i++)
{
int this_label = (int)prob->y[i];
int j;
for(j=0;j<nr_class;j++)
{
if(this_label == label[j])
{
++count[j];
break;
}
}
data_label[i] = j;
if(j == nr_class)
{
if(nr_class == max_nr_class)
{
max_nr_class *= 2;
label = (int *)realloc(label,max_nr_class*sizeof(int));
count = (int *)realloc(count,max_nr_class*sizeof(int));
}
label[nr_class] = this_label;
count[nr_class] = 1;
++nr_class;
}
}
//
// Labels are ordered by their first occurrence in the training set.
// However, for two-class sets with -1/+1 labels and -1 appears first,
// we swap labels to ensure that internally the binary SVM has positive data corresponding to the +1 instances.
//
if (nr_class == 2 && label[0] == -1 && label[1] == 1)
{
swap(label[0],label[1]);
swap(count[0],count[1]);
for(i=0;i<l;i++)
{
if(data_label[i] == 0)
data_label[i] = 1;
else
data_label[i] = 0;
}
}
int *start = Malloc(int,nr_class);
start[0] = 0;
for(i=1;i<nr_class;i++)
start[i] = start[i-1]+count[i-1];
for(i=0;i<l;i++)
( run in 0.612 second using v1.01-cache-2.11-cpan-13bb782fe5a )