Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
futtery opened this issue Aug 30, 2022 · 1 comment
Open

Comments

@futtery
Copy link

futtery commented Aug 30, 2022

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.

@AtanasGruev
Copy link

AtanasGruev commented Oct 22, 2024

Hello, although the issue is already dated and the problem might have been solved by the author, I'd like to point out a possible hint for future users. Most probably the cause is that either S1 or S2 in the _cut() method is computed to be a False-only mask; in other words, either the left or right split resulting from the operation (X[:, q] <= p) is empty.

Check whether X in the constructor

X = np.around(X, decimals=precision)

and p in the _cut() method
p = self.rng.uniform(xmin[q], xmax[q])

are both cast to the same type / dtype. I had the case where e.g. the samples used to initialize instances of RCTree had dtype np.float32 whereas p was of dtype np.float64. Similar discrepancies have been reported w.r.t. numpy broadcasting, refer to e.g. numpy issue #10982.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants