Last active
August 24, 2022 03:58
-
-
Save mu578/c811d7b6efb3ad8cc4dc6fc8b759a657 to your computer and use it in GitHub Desktop.
pseudo_rdforest represented as vector for very small dataset not recommended due to memory...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define BINARYTREE_MAXDEPTH 1024 | |
# define RDFOREST_MAXTREE 216 | |
# define RDFOREST_MAXSELECTION 512 | |
# define RDFOREST_MAXBAG 128 | |
struct btree | |
{ | |
int32_t u_data[BINARYTREE_MAXDEPTH]; | |
int32_t u_length; | |
}; | |
typedef struct btree btree; | |
static | |
void btree_init(btree * tree) | |
{ | |
tree->u_length = 1; | |
tree->u_data = { 0 }; | |
} | |
static | |
int32_t btree_length(const btree * tree) | |
{ return (tree->u_length - 1); } | |
static | |
void btree_swapelement(btree * tree, int32_t pos_key1, int32_t pos_key2) | |
{ | |
int32_t element = u_data[pos_key1]; | |
tree->u_data[pos_key2] = u_data[pos_key1]; | |
tree->u_data[pos_key1] = element; | |
} | |
static | |
void btree_peplaceelement(btree * tree, int32_t pos_key, int32_t v) | |
{ tree->u_data[pos_key] = v; } | |
static | |
int32_t btree_root(const btree * tree) | |
{ return tree->u_data[1]; } | |
static | |
int32_t btree_isroot(const btree * tree, int32_t pos_key) | |
{ return pos_key == 1; } | |
static | |
int32_t btree_isleft(const btree * tree,int32_t pos_key) | |
{ return (pos_key % 2) == 0; } | |
static | |
int32_t btree_isright(const btree * tree, int32_t pos_key) | |
{ return !btree_isleft(tree, pos_key); } | |
static | |
int32_t btree_parent(const btree * tree, int32_t pos_key) | |
{ return tree->u_data[pos_key / 2]; } | |
static | |
int32_t btree_leftchild(const btree * tree, int32_t pos_key) | |
{ return tree->u_data[pos_key * 2]; } | |
static | |
int32_t btree_rightchild(const btree * tree, int32_t pos_key) | |
{ return tree->u_data[(pos_key * 2) + 1]; } | |
static | |
int32_t btree_sibling(const btree * tree, int32_t pos_key) | |
{ | |
if (btree_isleft(tree, pos_key)) { | |
return tree->u_data[pos_key + 1]; | |
} | |
return tree->u_data[pos_key - 1]; | |
} | |
static | |
int32_t btree_isexternal(const btree * tree, int32_t pos_key) | |
{ return (pos_key * 2) > btree_length(tree); } | |
static | |
int32_t btree_isinternal(const btree * tree, int32_t pos_key) | |
{ return !btree_isexternal(tree, pos_key); } | |
static | |
int32_t btree_insert(btree * tree, int32_t v) | |
{ | |
tree->u_data[tree->u_length] = v; | |
++tree->u_length; | |
return CAST(int, btree_length(tree)); | |
} | |
static | |
int32_t btree_elementof(const btree * tree, int32_t pos_key) | |
{ return tree->u_data[pos_key]; } | |
static | |
int32_t btree_indexof(const btree * tree, int32_t pos_key) | |
{ return (pos_key - 1); } | |
static | |
void pseudo_rdforest_randbag(int32_t min, int32_t max | |
, int32_t n | |
, int32_t * dest | |
) { | |
int32_t range = (max - min); | |
int32_t i; | |
for (i = 0; i < n; i++) { | |
dest[i] = rand_int32(range) + min; | |
} | |
} | |
static | |
void pseudo_rdforest_randpermutation(int32_t n, int32_t k | |
, int32_t * set | |
, int32_t * dest | |
) { | |
int32_t i; | |
int32_t j; | |
for (i = 0; i < n; ++i) { | |
set[i] = 1; | |
} | |
for (i = 0; i < k; i++) { | |
j = rand_int32((n - 1)); | |
if (set[j] != -1) { | |
set[j] = -1; | |
dest[i] = j; | |
} else { | |
--i; | |
} | |
} | |
} | |
static | |
int32_t pseudo_rdforest_validatetrainy(int32_t n | |
, const int32_t * indexes | |
, const int16_t * train_y | |
) { | |
int32_t sum = 0; | |
int32_t i; | |
for (i = 0; i < n; i++) { | |
sum += train_y[indexes[i]]; | |
} | |
return (sum == 0 || sum == n) ? 0 : -1; | |
} | |
static | |
float pseudo_rdforest_gini(int32_t n | |
, const int32_t * indexes | |
, const int16_t * train_y | |
) { | |
float p_zero = 0.0f, p_one = 0.0f; | |
int32_t i = 0; | |
if (n > 0) { | |
for (; i < n; i++) { | |
p_zero += (1 - train_y[indexes[i]]); | |
p_one += train_y[indexes[i]]; | |
} | |
p_zero /= n; | |
p_one /= n; | |
return p_one * (1 - p_one) + p_zero * (1 - p_zero); | |
} | |
return -1.0f; | |
} | |
static | |
float pseudo_rdforest_giniscore(int32_t n, int32_t nleft, int32_t nright | |
, const int32_t * lindexes | |
, const int32_t * rindexes | |
, const int32_t * train_y_idx | |
, const int16_t * train_y | |
) { | |
return | |
( | |
pseudo_rdforest_gini(n, train_y_idx, train_y) | |
- pseudo_rdforest_gini(nleft, left_indexes, train_y) | |
- pseudo_rdforest_gini(nright, right_indexes, train_y) | |
); | |
} | |
static | |
int32_t pseudo_rdforest_treeparent(const pseudo_rdforest_tree * tree, int32_t pos_key) | |
{ /* overload-rules of mapping to avoid (2^k - 1) */ } | |
static | |
int32_t pseudo_rdforest_treeleftchild(const pseudo_rdforest_tree * tree, int32_t pos_key) | |
{ /* overload-rules of mapping to avoid (2^k - 1) */ } | |
static | |
int32_t pseudo_rdforest_treerightchild(const pseudo_rdforest_tree * tree, int32_t pos_key) | |
{ /* overload-rules of mapping to avoid (2^k - 1) */ } | |
struct pseudo_rdforest_tree | |
{ | |
struct btree u_tree; | |
float u_splitvals[BINARYTREE_MAXDEPTH]; | |
int32_t u_splitvars[BINARYTREE_MAXDEPTH]; | |
int16_t u_votes[BINARYTREE_MAXDEPTH]; | |
}; | |
typedef struct pseudo_rdforest_tree pseudo_rdforest_tree; | |
struct pseudo_rdforest_classifier | |
{ | |
struct | |
{ | |
int32_t u_idx1[RDFOREST_MAXSELECTION]; | |
int32_t u_idx2[RDFOREST_MAXSELECTION]; | |
int32_t u_idx3[RDFOREST_MAXSELECTION]; | |
int32_t u_bag1[RDFOREST_MAXBAG]; | |
int32_t u_bag2[RDFOREST_MAXBAG]; | |
} u_buffer; | |
pseudo_rdforest_tree u_forest[RDFOREST_MAXTREE]; | |
int32_t u_nensenmble; | |
} | |
/* EOF */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment