src_c/util.c File Reference

Source file for utility functions. More...

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <float.h>
#include "mmio.h"
#include "def.h"
#include "util.h"
#include "l1_logreg.h"
#include "dmatrix.h"

Defines

#define BIN_EXP_BEGIN   (-7)
 Shows histogram of coefficients in 10-base logscale.
#define BIN_EXP_END   (+2)
#define BIN_NUM_PER_DECADE   (8)
#define BIN_IDX_NUM   ((BIN_EXP_END-BIN_EXP_BEGIN)*BIN_NUM_PER_DECADE)
#define BIN_IDX_BEGIN   (0)
#define BIN_IDX_END   (BIN_IDX_BEGIN+BIN_IDX_NUM-1)

Functions

double find_lambdamax (const dmatrix *X, const double *b, const int sflag)
 Returns the maximum value of the regularization parameter lambda that gives a non-zero solution.
void standardize_data (dmatrix *X, const double *b, double **average, double **stddev, double **acol, double **arow)
 Standardizes the data.
int read_mm_new_matrix (const char *file, dmatrix **out_mat)
 Read a matrix file.
int read_mm_new_matrix_transpose (const char *file, dmatrix **out_mat)
int read_mm_new_vector (const char *file, double **out_vec)
 Read a vector file.
int write_mm_vector (const char *file, const int m, const double *vec, const char *comments, const int type)
int write_mm_matrix (const char *file, dmatrix *mat, const char *comments, const int type)
int write_mm_matrix_crd_header (FILE *fp, const int m, const int n, const int nnz, const char *comments)
int write_mm_matrix_crd_column (FILE *fp, const int len, const int col, const double *vec)
int write_mm_matrix_crd_column_threshold (FILE *fp, const int len, const int col, const double *vec, const double threshold)
int get_mm_info (const char *file, int *m, int *n, int *nz)
int show_histogram (const int n, const double *coeff)
double userinput_threshold (void)
 Gets a threshold value from standard input.
int thresholding (const int n, double *coeff, double threshold)
 Thresholds coefficients.
void buffer_new (buffer_t **buf, int elemsize, int length)
void buffer_free (buffer_t *buf)
int buffer_write (buffer_t *buf, void *src, int n)
int condense_solution (const int len, double *sol, int *idx)
 Condense solution by removing zeros.


Detailed Description

Source file for utility functions.


Define Documentation

#define BIN_EXP_BEGIN   (-7)

Shows histogram of coefficients in 10-base logscale.

Parameters:
n number of coefficients
coeff vector of coefficients


Function Documentation

int condense_solution ( const int  len,
double *  sol,
int *  idx 
)

Condense solution by removing zeros.

Truncates coefficients if their absolute values are below the threshold.

Parameters:
len size of the original solution vector
sol solution vector
idx array of non-zero index in solution
ex: initial sol: [1 0 0 6 0 0 0 7 1] final sol: [1 6 7 1] idx: [1 4 8 9]

double find_lambdamax ( const dmatrix X,
const double *  b,
const int  sflag 
)

Returns the maximum value of the regularization parameter lambda that gives a non-zero solution.

Parameters:
X feature matrix
b class vector
sflag standardization flag
  • If sflag is 0, compute the maximum value of lambda without standardization.
  • If sflag is 1, given matrix is standardized first and then the maximum value of lambda is computed.
Returns:
maximum value of lambda

References dmat_copy(), dmat_diagscale(), dmat_duplicate(), dmat_free(), dmat_norminf(), dmat_yAmpqTx(), dmatrix::m, dmatrix::n, and standardize_data().

int read_mm_new_matrix ( const char *  file,
dmatrix **  out_mat 
)

Read a matrix file.

Reads a Matrix Market formatted matrix from a file.

Parameters:
file matrix file name
out_mat matrix data
Returns:
result

References dmatrix::idx, dmatrix::jdx, dmatrix::m, dmatrix::n, dmatrix::nz, dmatrix::rdx, and dmatrix::val.

Referenced by main().

int read_mm_new_vector ( const char *  file,
double **  out_vec 
)

Read a vector file.

Reads a Matrix Market formatted vector from a file. Returns pointer to dense vector.

Parameters:
file vector file name
out_vec vector data
Returns:
result

Referenced by main().

void standardize_data ( dmatrix X,
const double *  b,
double **  average,
double **  stddev,
double **  acol,
double **  arow 
)

Standardizes the data.

Standardizes the feature matrix.

  • If b is not given, compute $ X := (X-1^T\mu)\mbox{diag}(\sigma)^{-1} $
  • If b is given, compute $ X := \mbox{diag}(b)(X-1^T\mu)\mbox{diag}(\sigma)^{-1} $ where $ \mu $ is column average and $ \sigma $ is column deviation.

Parameters:
X feature matrix
b class vector
average column average
stddev column standard deviation
acol column vector used for implicit standardization
arow row vector used for implicit standardization

References dmat_colavg(), dmat_colstd(), dmat_diagscale(), dmat_elemdivi(), dmat_vcopy(), dmat_vset(), dmatrix::m, dmatrix::n, dmatrix::nz, and dmatrix::val.

Referenced by find_lambdamax(), and l1_logreg_train().

int thresholding ( const int  n,
double *  coeff,
double  threshold 
)

Thresholds coefficients.

Truncates coefficients if their absolute values are below the threshold.

Parameters:
n number of coefficients
coeff vector of coefficients
threshold threshold value

double userinput_threshold ( void   ) 

Gets a threshold value from standard input.

  • If input value is positive, return it.
  • If input value is negative, assume it as a 10-based log value. For example, -3 is converted to 10^(-3).

Returns:
threshold value


Generated on Mon May 25 19:15:19 2009 for l1_logreg by Doxygen 1.5.5