kLabUM / rrcf

🌲 Implementation of the Robust Random Cut Forest algorithm for anomaly detection on streams
https://klabum.github.io/rrcf/
MIT License
495 stars 112 forks source link

ValueError: can only convert an array of size 1 to a Python scalar #92

Open futtery opened 2 years ago

futtery commented 2 years ago

Hi,I have found a problem in building the tree when dealing with some datasets, but I don't know why. When I run the initial code as following,

_# Construct forest forest = [] while len(forest) < num_trees:

Select random subsets of points uniformly

                ixs = np.random.choice(n, size=sample_size_range,
                                       replace=False)
                # Add sampled trees to forest
                trees = [rrcf.RCTree(X[ix], index_labels=ix)
                         for ix in ixs]
                forest.extend(trees)_

the error happens ,and it display like this .

ValueError Traceback (most recent call last)

in 116 replace=False) 117 # Add sampled trees to forest --> 118 trees = [rrcf.RCTree(X[ix], index_labels=ix) 119 for ix in ixs] 120 forest.extend(trees) in (.0) 116 replace=False) 117 # Add sampled trees to forest --> 118 trees = [rrcf.RCTree(X[ix], index_labels=ix) 119 for ix in ixs] 120 forest.extend(trees) ~/anaconda3/lib/python3.8/site-packages/rrcf/rrcf.py in __init__(self, X, index_labels, precision, random_state) 104 # Create RRC Tree 105 S = np.ones(n, dtype=np.bool) --> 106 self._mktree(X, S, N, I, parent=self) 107 # Remove parent of root 108 self.root.u = None ~/anaconda3/lib/python3.8/site-packages/rrcf/rrcf.py in _mktree(self, X, S, N, I, parent, side, depth) 196 if S2.sum() > 1: 197 # Recursively construct tree on S2 --> 198 self._mktree(X, S2, N, I, parent=branch, side='r', depth=depth) 199 # Otherwise... 200 else: ~/anaconda3/lib/python3.8/site-packages/rrcf/rrcf.py in _mktree(self, X, S, N, I, parent, side, depth) 174 if S1.sum() > 1: 175 # Recursively construct tree on S1 --> 176 self._mktree(X, S1, N, I, parent=branch, side='l', depth=depth) 177 # Otherwise... 178 else: ~/anaconda3/lib/python3.8/site-packages/rrcf/rrcf.py in _mktree(self, X, S, N, I, parent, side, depth) 174 if S1.sum() > 1: 175 # Recursively construct tree on S1 --> 176 self._mktree(X, S1, N, I, parent=branch, side='l', depth=depth) 177 # Otherwise... 178 else: ~/anaconda3/lib/python3.8/site-packages/rrcf/rrcf.py in _mktree(self, X, S, N, I, parent, side, depth) 174 if S1.sum() > 1: 175 # Recursively construct tree on S1 --> 176 self._mktree(X, S1, N, I, parent=branch, side='l', depth=depth) 177 # Otherwise... 178 else: ~/anaconda3/lib/python3.8/site-packages/rrcf/rrcf.py in _mktree(self, X, S, N, I, parent, side, depth) 200 else: 201 # Create a leaf node from isolated point --> 202 i = np.asscalar(np.flatnonzero(S2)) 203 leaf = Leaf(i=i, d=depth, u=branch, x=X[i, :], n=N[i]) 204 # Link leaf node to parent <__array_function__ internals> in asscalar(*args, **kwargs) ~/anaconda3/lib/python3.8/site-packages/numpy/lib/type_check.py in asscalar(a) 579 24 580 """ --> 581 return a.item() 582 583 #----------------------------------------------------------------------------- ValueError: can only convert an array of size 1 to a Python scalar However, when I try to run the X[ix] and build again ,it run.