def test(self, *args, **kwargs): ''' Usage: Y_label[, ap][, complexity][, depths] = test(X, Y=None, param=None, return_complexity=False, return_depth=False, verbosity=0) ''' X, Y, return_complexity, return_depth, verbosity \ = parse_args(args, (np.ndarray, np.ndarray, bool, bool, int), (None, None, False, False, 0)) X = kwargs.get('X', X) Y = kwargs.get('Y', Y) return_complexity = kwargs.get('return_complexity', return_complexity) return_depth = kwargs.get('return_depth', return_depth) verbosity = kwargs.get('verbosity', verbosity) Y_predict = None ap = None complexity = None depths = None if X is not None: result = self.test_proba(X, return_complexity, return_depth, verbosity) if return_complexity and return_depth: Y_proba, complexity, depths = result elif not return_complexity and return_depth: Y_proba, depths = result elif return_complexity and not return_depth: Y_proba, complexity = result else: Y_proba = result Y_predict = np.argmax(Y_proba, axis=-1) + 1 if Y is not None and Y.shape[0] == X.shape[0]: ap = average_precision(Y, Y_proba) return pack_seq(Y_predict, ap, complexity, depths)
def test_proba(self, *args, **kwargs): ''' Usage: Y_proba[, complexity][, depths] = test_proba(X, return_complexity=False, return_depth=False, verbosity=0) ''' X, return_complexity, return_depth, verbosity \ = parse_args(args, (np.ndarray, bool, bool, int), (None, False, False, 0)) X = kwargs.get('X', X) return_complexity = kwargs.get('return_complexity', return_complexity) return_depth = kwargs.get('return_depth', return_depth) verbosity = kwargs.get('verbosity', verbosity) num_of_parallel_jobs = self.forest_param.num_of_parallel_jobs if num_of_parallel_jobs > 1 and len(self) > 1: if num_of_parallel_jobs > len(self): num_of_parallel_jobs = len(self) test_result = Parallel(n_jobs=num_of_parallel_jobs, verbose=verbosity) \ (delayed(_parallel_test)(sltree, X, return_complexity, return_depth) for sltree in self) else: test_result = [None] * len(self) for i, sltree in enumerate(self): if verbosity > 0: print '\rTesting SoftLabelTree {}/{}.' \ .format(i + 1, len(self)), test_result[i] = _parallel_test(sltree, X, return_complexity, return_depth) complexity_all_trees = None depths_all_trees = None if return_complexity or return_depth: unzipped_result = zip(*test_result) if return_complexity and not return_depth: Y_proba_all_trees, complexity_all_trees = unzipped_result elif not return_complexity and return_depth: Y_proba_all_trees, depths_all_trees = unzipped_result else: Y_proba_all_trees, complexity_all_trees, depths_all_trees = \ unzipped_result else: Y_proba_all_trees = test_result Y_proba = np.zeros(Y_proba_all_trees[0].shape) complexity = None depths = None for Y_proba_each_tree in Y_proba_all_trees: Y_proba += Y_proba_each_tree if complexity_all_trees is not None: complexity = np.vstack(complexity_all_trees).T if depths_all_trees is not None: depths = np.vstack(depths_all_trees).T return pack_seq(Y_proba, complexity, depths)
def test_proba(self, *args, **kwargs): ''' Usage: Y_proba[, complexity][, depths] = test_proba(X, param=None, return_complexity=False, return_depth=False, verbose=0) ''' X, param, return_complexity, return_depth, verbose \ = parse_args(args, (np.ndarray, TreeParam, bool, bool, int), (None, self.tree_param, False, False, 0)) X = kwargs.get('X', X) param = kwargs.get('param', param) return_complexity = kwargs.get('return_complexity', return_complexity) return_depth = kwargs.get('return_depth', return_depth) verbose = kwargs.get('verbose', verbose) Y_proba = np.empty((X.shape[0], self.root.data.num_of_labels), dtype=X.dtype) complexity = None depths = None Y_proba[...] = -np.inf if return_depth: depths = np.zeros(X.shape[0], dtype=np.int) if return_complexity: complexity = np.zeros(X.shape[0], dtype=np.int) for i in xrange(X.shape[0]): if verbose > 0: print '\rTesting sample {}/{} ...'.format(i+1, X.shape[0]), x = X[i] node = self.root while not (node.is_leaf() or self._should_be_leaf(node, param)): if return_complexity: complexity[i] += node.model.n_nonzeros if node.model.test_one(x) > 0: node = node.lchild else: node = node.rchild if node.model is None or not param.ovr_in_leaves: num_distrib = node.data.num_of_samples_of_each_label \ .astype(X.dtype) proba = num_distrib / np.sum(num_distrib) Y_proba[i, node.data.labels-1] = proba else: proba = node.model.decision_function(x).ravel() num_model_classes = node.model.classes_.shape[0] if num_model_classes <= 2: proba = np.r_[-proba, proba] num_model_classes -= 1 Y_proba[i, node.model.classes_-1] = proba if return_complexity: complexity[i] += np.count_nonzero(node.model.coef_) if return_depth: depths[i] = node.depth if verbose > 0: print '\rDone.' return pack_seq(Y_proba, complexity, depths)