Permalink
Browse files

predict neighbors refactored

  • Loading branch information...
paolo-losi committed Mar 31, 2012
1 parent 9c1dcc7 commit 2cdfa812c18ae3e6551b2b058b5485afdefe9620

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -13,7 +13,6 @@ from cextratrees cimport (ET_problem, ET_problem_destroy, ET_load_libsvm_file,
ET_forest_neighbors, ET_params,
ET_forest_predict_class_bayes,
class_probability_vec, class_probability,
- neighbour_weight, neighbour_weight_vec,
double_vec, ET_forest_feature_importance)
@@ -172,13 +171,12 @@ cdef class Forest:
@cython.wraparound(False)
def neighbors(self, np.ndarray[np.float32_t, ndim=2] X, curtail=1):
cdef float *vector
+ cdef double *weights
cdef int sample_idx, feature_idx
cdef uint32_t _curtail = curtail
- cdef neighbour_weight_vec *nwv
- cdef neighbour_weight *nw
cdef np.ndarray[np.float64_t, ndim=2] adiacency
- adiacency = numpy.zeros(shape=(X.shape[0], self._forest.n_samples),
+ adiacency = numpy.empty(shape=(X.shape[0], self._forest.n_samples),
dtype=numpy.float64)
vector = <float *> malloc(sizeof(float) * X.shape[1])
@@ -189,16 +187,14 @@ cdef class Forest:
for feature_idx in xrange(X.shape[1]):
vector[feature_idx] = X[sample_idx, feature_idx]
- nwv = ET_forest_neighbors(self._forest, vector, _curtail)
- if not nwv:
+ weights = ET_forest_neighbors(self._forest, vector, _curtail)
+ if not weights:
raise MemoryError()
- for i in xrange(nwv.n):
- nw = &nwv.a[i]
- adiacency[sample_idx, nw.key] = nw.weight
+ for feature_idx in xrange(X.shape[1]):
+ adiacency[sample_idx, feature_idx] = weights[feature_idx]
- free(nwv.a)
- free(nwv)
+ free(weights)
free(vector)
return adiacency

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -51,16 +51,7 @@ cdef extern from "extratrees.h":
float *vector,
uint32_t curtail_min_size,
bool smooth)
-
- ctypedef struct neighbour_weight:
- uint32_t key
- double weight
-
- ctypedef struct neighbour_weight_vec:
- size_t n, m
- neighbour_weight *a
-
- cdef neighbour_weight_vec *ET_forest_neighbors(ET_forest *forest,
+ cdef double *ET_forest_neighbors(ET_forest *forest,
float *vector,
uint32_t curtail_min_size)
View
@@ -94,14 +94,6 @@ typedef struct {
// --- predict types ---
-typedef struct {
- uint32_t key;
- double weight;
-} neighbour_weight;
-
-typedef kvec_t(neighbour_weight) neighbour_weight_vec;
-
-
typedef struct {
double label;
double probability;
@@ -127,7 +119,7 @@ double ET_forest_predict_class_majority(ET_forest *forest, float *v,
uint32_t curtail_min_size);
double ET_forest_predict_class_bayes(ET_forest *forest, float *v,
uint32_t curtail_min_size, bool smooth);
-neighbour_weight_vec *ET_forest_neighbors(ET_forest *forest, float *vector,
+double *ET_forest_neighbors(ET_forest *forest, float *vector,
uint32_t curtail_min_size);
class_probability_vec *ET_forest_predict_probability(ET_forest *forest,
float *vector,
View
@@ -189,15 +189,14 @@ uint_vec **ET_forest_neighbors_detail(ET_forest *forest, float *vector,
}
-neighbour_weight_vec *ET_forest_neighbors(ET_forest *forest, float *vector,
- uint32_t curtail_min_size) {
+double *ET_forest_neighbors(ET_forest *forest, float *vector,
+ uint32_t curtail_min_size) {
uint_vec **neigh_detail;
- neighbour_weight_vec *nwvec;
size_t n_trees = kv_size(forest->trees);
+ double *nwa = NULL;
- nwvec = malloc(sizeof(neighbour_weight_vec));
- check_mem(nwvec);
- kv_init(*nwvec);
+ nwa = calloc(sizeof(double), forest->n_samples);
+ check_mem(nwa);
neigh_detail = ET_forest_neighbors_detail(forest, vector, curtail_min_size);
check_mem(neigh_detail);
@@ -207,24 +206,16 @@ neighbour_weight_vec *ET_forest_neighbors(ET_forest *forest, float *vector,
double incr = 1.0 / (double) (kv_size(*tree_neighs) * n_trees);
for(size_t j = 0; j < kv_size(*tree_neighs); j++) {
- neighbour_weight *nw = NULL;
uint32_t sample_idx = kv_A(*tree_neighs, j);
-
- kal_getp(*nwvec, sample_idx, nw);
- if (nw == NULL) {
- kv_push(neighbour_weight, *nwvec,
- ((neighbour_weight) { sample_idx, incr }));
- } else {
- nw->weight += incr;
- }
+ nwa[sample_idx] += incr;
}
kv_destroy(*tree_neighs);
free(tree_neighs);
}
free(neigh_detail);
exit:
- return nwvec;
+ return nwa;
}
@@ -319,7 +310,6 @@ class_probability_vec *ET_forest_predict_probability(ET_forest *forest,
bool smooth) {
bool error = true;
class_probability_vec *prob_vec = NULL;
- neighbour_weight_vec *nwvec = NULL;
double n_trees = kv_size(forest->trees);
prob_vec = malloc(sizeof(class_probability_vec));
@@ -380,10 +370,6 @@ class_probability_vec *ET_forest_predict_probability(ET_forest *forest,
kv_destroy(*prob_vec);
free(prob_vec);
}
- if (nwvec != NULL) {
- kv_destroy(*nwvec);
- free(nwvec);
- }
return prob_vec;
}
Oops, something went wrong.

0 comments on commit 2cdfa81

Please sign in to comment.