资源简介
决策树C语言实现代码,建立决策树,建立决策树链接
代码片段和文件信息
//决策树算法
enum UINT { INACTIVE OFF ON };
#define LN_2 0.693147180559945309417
#define entropy(x) (x > 0 ? x * log(x) / LN_2 : 0.0)
typedef struct node {
unsigned int idx;
double threshold;
struct node *on;
struct node *off;
struct node *parent;
} NODE;
typedef struct ne_struct {
double ne;
UINT status;
} NEGENTROPY;
typedef struct matrix {
unsigned int width;
unsigned int height;
double **data;
} MATRIX;
MATRIX *build_matrix (unsigned int width unsigned int height)
{
MATRIX *_matrix;
unsigned int i;
_matrix = (MATRIX*) malloc (sizeof (MATRIX));
if (!_matrix)
err_exit (__FILE__ __LINE__);
_matrix->width = width;
_matrix->height = height;
_matrix->data = (double **) malloc (height * sizeof (double *));
if (_matrix->data == NULL)
err_exit(__FILE__ __LINE__);
for (i=0; i {
_matrix->data[i] = (double *) malloc (width * sizeof(double));
if (_matrix->data[i] == NULL)
err_exit(__FILE__ __LINE__);
}
return _matrix;
}
void err_exit (char * file unsigned int line)
{
printf(“\n Fatal error in file %s line %u“ file line);
exit(0);
}
void file_size (char *file_name unsigned int *width unsigned int *height)
{
FILE *f;
unsigned int buf_size = 0xFF _width = 0;
char *buffer *ptr;
*width = *height = 0;
buffer = (char *) malloc (buf_size * sizeof (char));
if (buffer == NULL)
err_exit (__FILE__ __LINE__);
f = fopen(file_name “r“);
if (f == NULL)
{
printf(“\n File not found : %s\n“ file_name);
err_exit (__FILE__ __LINE__);
}
if (fgets(buffer buf_size f) != NULL)
{
++*height;
ptr = strtok (buffer “ “);
while (ptr != NULL)
{
++*width;
ptr = strtok (NULL “ “);
}
}
while (!feof(f))
{
if (fgets(buffer buf_size f) != NULL)
{
if (strlen(buffer) > strlen(“\n“)) /* if line is more than a NL char */
{
++*height;
_width = 0;
ptr = strtok (buffer “ “);
while (ptr != NULL)
{
++_width;
ptr = strtok (NULL “ “);
}
if (*width != _width)
{
printf(“\n Number of entries in file %s did not agree“ file_name);
err_exit (__FILE__ __LINE__);
}
}
}
}
free (buffer);
}
void free_matrix (MATRIX *_matrix)
{
unsigned int i;
for (i=0; i<_matrix->height; i++)
free (_matrix->data[i]);
free (_matrix->data);
free (_matrix);
}
void free_tags (char ** varname unsigned int width)
{
unsigned int i;
for (i=0
评论
共有 条评论