scikit-learn: Test failure in i686 with version 1.3.1
Describe the bug
During the build of scikit-learn for Fedora Linux, I’m obtaining an error runing the tests in i686. The test that fails is:
sklearn/tree/tests/test_export.py::test_graphviz_toy
Steps/Code to Reproduce
In a i686 machine
pytest sklearn/tree/tests/test_export.py
Expected Results
Test passes
Actual Results
sklearn/tree/tests/test_export.py::test_graphviz_toy FAILED [ 93%]
=================================== FAILURES ===================================
______________________________ test_graphviz_toy _______________________________
def test_graphviz_toy():
# Check correctness of export_graphviz
clf = DecisionTreeClassifier(
max_depth=3, min_samples_split=2, criterion="gini", random_state=2
)
clf.fit(X, y)
# Test export code
contents1 = export_graphviz(clf, out_file=None)
contents2 = (
"digraph Tree {\n"
'node [shape=box, fontname="helvetica"] ;\n'
'edge [fontname="helvetica"] ;\n'
'0 [label="x[0] <= 0.0\\ngini = 0.5\\nsamples = 6\\n'
'value = [3, 3]"] ;\n'
'1 [label="gini = 0.0\\nsamples = 3\\nvalue = [3, 0]"] ;\n'
"0 -> 1 [labeldistance=2.5, labelangle=45, "
'headlabel="True"] ;\n'
'2 [label="gini = 0.0\\nsamples = 3\\nvalue = [0, 3]"] ;\n'
"0 -> 2 [labeldistance=2.5, labelangle=-45, "
'headlabel="False"] ;\n'
"}"
)
assert contents1 == contents2
# Test plot_options
contents1 = export_graphviz(
clf,
filled=True,
impurity=False,
proportion=True,
special_characters=True,
rounded=True,
out_file=None,
fontname="sans",
)
contents2 = (
"digraph Tree {\n"
'node [shape=box, style="filled, rounded", color="black", '
'fontname="sans"] ;\n'
'edge [fontname="sans"] ;\n'
"0 [label=<x<SUB>0</SUB> ≤ 0.0<br/>samples = 100.0%<br/>"
'value = [0.5, 0.5]>, fillcolor="#ffffff"] ;\n'
"1 [label=<samples = 50.0%<br/>value = [1.0, 0.0]>, "
'fillcolor="#e58139"] ;\n'
"0 -> 1 [labeldistance=2.5, labelangle=45, "
'headlabel="True"] ;\n'
"2 [label=<samples = 50.0%<br/>value = [0.0, 1.0]>, "
'fillcolor="#399de5"] ;\n'
"0 -> 2 [labeldistance=2.5, labelangle=-45, "
'headlabel="False"] ;\n'
"}"
)
assert contents1 == contents2
# Test max_depth
contents1 = export_graphviz(clf, max_depth=0, class_names=True, out_file=None)
contents2 = (
"digraph Tree {\n"
'node [shape=box, fontname="helvetica"] ;\n'
'edge [fontname="helvetica"] ;\n'
'0 [label="x[0] <= 0.0\\ngini = 0.5\\nsamples = 6\\n'
'value = [3, 3]\\nclass = y[0]"] ;\n'
'1 [label="(...)"] ;\n'
"0 -> 1 ;\n"
'2 [label="(...)"] ;\n'
"0 -> 2 ;\n"
"}"
)
assert contents1 == contents2
# Test max_depth with plot_options
contents1 = export_graphviz(
clf, max_depth=0, filled=True, out_file=None, node_ids=True
)
contents2 = (
"digraph Tree {\n"
'node [shape=box, style="filled", color="black", '
'fontname="helvetica"] ;\n'
'edge [fontname="helvetica"] ;\n'
'0 [label="node #0\\nx[0] <= 0.0\\ngini = 0.5\\n'
'samples = 6\\nvalue = [3, 3]", fillcolor="#ffffff"] ;\n'
'1 [label="(...)", fillcolor="#C0C0C0"] ;\n'
"0 -> 1 ;\n"
'2 [label="(...)", fillcolor="#C0C0C0"] ;\n'
"0 -> 2 ;\n"
"}"
)
assert contents1 == contents2
# Test multi-output with weighted samples
clf = DecisionTreeClassifier(
max_depth=2, min_samples_split=2, criterion="gini", random_state=2
)
clf = clf.fit(X, y2, sample_weight=w)
> contents1 = export_graphviz(clf, filled=True, impurity=False, out_file=None)
sklearn/tree/tests/test_export.py:131:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
sklearn/utils/_param_validation.py:211: in wrapper
return func(*args, **kwargs)
sklearn/tree/_export.py:905: in export_graphviz
exporter.export(decision_tree)
sklearn/tree/_export.py:465: in export
self.recurse(decision_tree.tree_, 0, criterion=decision_tree.criterion)
sklearn/tree/_export.py:528: in recurse
', fillcolor="%s"' % self.get_fill_color(tree, node_id)
sklearn/tree/_export.py:285: in get_fill_color
return self.get_color(node_val)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <sklearn.tree._export._DOTTreeExporter object at 0xc5080408>
value = -0.4691358024691358
def get_color(self, value):
# Find the appropriate color & intensity for a node
if self.colors["bounds"] is None:
# Classification tree
color = list(self.colors["rgb"][np.argmax(value)])
sorted_values = sorted(value, reverse=True)
if len(sorted_values) == 1:
alpha = 0.0
else:
alpha = (sorted_values[0] - sorted_values[1]) / (1 - sorted_values[1])
else:
# Regression tree or multi-output
color = list(self.colors["rgb"][0])
alpha = (value - self.colors["bounds"][0]) / (
self.colors["bounds"][1] - self.colors["bounds"][0]
)
# compute the color as alpha against white
> color = [int(round(alpha * c + (1 - alpha) * 255, 0)) for c in color]
E ValueError: cannot convert float NaN to integer
Versions
Version is 1.3.1
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (16 by maintainers)
Commits related to this issue
- Fix i686 test failure Forwarded: https://github.com/scikit-learn/scikit-learn/issues/27506 Bug-Debian: https://bugs.debian.org/1070201 Gbp-Pq: Name i386-impurity-nan.patch — committed to raspbian-packages/scikit-learn by lesteve 2 years ago
- Fix i686 test failure Forwarded: https://github.com/scikit-learn/scikit-learn/issues/27506 Bug-Debian: https://bugs.debian.org/1070201 Gbp-Pq: Name i386-impurity-nan.patch — committed to raspbian-packages/scikit-learn by lesteve 2 years ago
- Fix i686 test failure Forwarded: https://github.com/scikit-learn/scikit-learn/issues/27506 Bug-Debian: https://bugs.debian.org/1070201 Gbp-Pq: Name i386-impurity-nan.patch — committed to raspbian-packages/scikit-learn by lesteve 2 years ago
- Fix i686 test failure Forwarded: https://github.com/scikit-learn/scikit-learn/issues/27506 Bug-Debian: https://bugs.debian.org/1070201 Gbp-Pq: Name i386-impurity-nan.patch — committed to raspbian-packages/scikit-learn by lesteve 2 years ago
I opened an issue in Numpy: https://github.com/numpy/numpy/issues/26775. Let’s see what they say!
Great thanks a lot @mr-c for the reproducer inside the Docker image, I confirm I can reproduce indeed.
About the patch, rereading https://github.com/scikit-learn/scikit-learn/issues/27506#issuecomment-2154762231 more carefully I think we could even do something simpler by multiplying by
-1.instead of using array negation (or themin(-arr) == -max(arr)trick I originally thought of). Go ahead if you want to open a a PR with something like the following otherwise I’ll try to pick it up in the not too far future:I’ll also try to open an issue in Numpy, because they will probably more insights than me/us on this kind of tricky topics.
Thanks a lot for the details!
So I would say this bug is a bit of a edge case, since this happens on a 32bit machine, which represent a small portion of our users (for example for Pillow when they had 32bit wheels on Linux, that was less than 0.1% of the Linux downloads, see https://discuss.python.org/t/dropping-32-bit-packages/5476/20) and also happens in some scikit-learn visualization/debugging code.
The simplest fix would probably be to avoid using array negation, using the fact that
np.min(-array) == -np.max(array)i.e. something like this:It could well be the case that a similar issue happens in other places of scikit-learn.
For further reference, could you post the output of
and
I don’t know too much about the SIMD intricacies, but in an ideal world, it would be nice to understand if scikit-learn is to blame for not creating an array on a 64bit boundary (I am not sure at all what the boundary should be by the way) or numpy should be able to deal with unaligned arrays better …
I don’t know either, whether there is some hope to reproduce this kind of issues inside a 32bit Docker image on a 64bit host …