From ec2f9c931e01d69608288f9d3637d518b9e46c83 Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Tue, 11 Apr 2023 08:16:51 +0100 Subject: [PATCH] MAINT: remove `from numpy.math cimport` statements All C99 constants and functions are available from Cython's `libc`, which is cleaner. NumPy would like to get rid of shipping libnpymath, and has also deprecated the (unrelated) `numpy.math` in the Python API. Finally, numpy is shipping its own `__init__.pxd` now, but not `math.pxd` - so that should be considered legacy at this point. So best to not use it at all. --- sklearn/decomposition/_online_lda_fast.pyx | 2 +- sklearn/ensemble/_hist_gradient_boosting/splitting.pyx | 2 +- sklearn/linear_model/_sgd_fast.pyx.tp | 3 +-- sklearn/tree/_criterion.pyx | 3 +-- sklearn/utils/sparsefuncs_fast.pyx | 3 +-- 5 files changed, 5 insertions(+), 8 deletions(-) diff --git a/sklearn/decomposition/_online_lda_fast.pyx b/sklearn/decomposition/_online_lda_fast.pyx index 0ef34dd395990..c710b03a89626 100644 --- a/sklearn/decomposition/_online_lda_fast.pyx +++ b/sklearn/decomposition/_online_lda_fast.pyx @@ -6,7 +6,6 @@ import numpy as np cnp.import_array() from libc.math cimport exp, fabs, log -from numpy.math cimport EULER def mean_change(const floating[:] arr_1, const floating[:] arr_2): @@ -91,6 +90,7 @@ def _dirichlet_expectation_2d(const floating[:, :] arr): # After: J. Bernardo (1976). Algorithm AS 103: Psi (Digamma) Function. # https://www.uv.es/~bernardo/1976AppStatist.pdf cdef floating psi(floating x) noexcept nogil: + cdef double EULER = 0.577215664901532860606512090082402431 if x <= 1e-6: # psi(x) = -EULER - 1/x + O(x) return -EULER - 1. / x diff --git a/sklearn/ensemble/_hist_gradient_boosting/splitting.pyx b/sklearn/ensemble/_hist_gradient_boosting/splitting.pyx index 4166bb5606ba1..1b875af609425 100644 --- a/sklearn/ensemble/_hist_gradient_boosting/splitting.pyx +++ b/sklearn/ensemble/_hist_gradient_boosting/splitting.pyx @@ -10,9 +10,9 @@ cimport cython from cython.parallel import prange import numpy as np +from libc.math cimport INFINITY from libc.stdlib cimport malloc, free, qsort from libc.string cimport memcpy -from numpy.math cimport INFINITY from .common cimport X_BINNED_DTYPE_C from .common cimport Y_DTYPE_C diff --git a/sklearn/linear_model/_sgd_fast.pyx.tp b/sklearn/linear_model/_sgd_fast.pyx.tp index 2938bd0daf3f3..bcd2bd7e5576e 100644 --- a/sklearn/linear_model/_sgd_fast.pyx.tp +++ b/sklearn/linear_model/_sgd_fast.pyx.tp @@ -30,9 +30,8 @@ from cython cimport floating import numpy as np from time import time -from libc.math cimport exp, log, pow, fabs +from libc.math cimport exp, log, pow, fabs, INFINITY cimport numpy as cnp -from numpy.math cimport INFINITY cdef extern from "_sgd_fast_helpers.h": bint skl_isfinite32(float) nogil bint skl_isfinite64(double) nogil diff --git a/sklearn/tree/_criterion.pyx b/sklearn/tree/_criterion.pyx index 7cd7bbb0e3c1b..e29db58131ee9 100644 --- a/sklearn/tree/_criterion.pyx +++ b/sklearn/tree/_criterion.pyx @@ -14,13 +14,12 @@ from libc.string cimport memcpy from libc.string cimport memset -from libc.math cimport fabs +from libc.math cimport fabs, INFINITY import numpy as np cimport numpy as cnp cnp.import_array() -from numpy.math cimport INFINITY from scipy.special.cython_special cimport xlogy from ._utils cimport log diff --git a/sklearn/utils/sparsefuncs_fast.pyx b/sklearn/utils/sparsefuncs_fast.pyx index a2e0089250a6d..c06d026ae19c5 100644 --- a/sklearn/utils/sparsefuncs_fast.pyx +++ b/sklearn/utils/sparsefuncs_fast.pyx @@ -8,12 +8,11 @@ #!python -from libc.math cimport fabs, sqrt +from libc.math cimport fabs, sqrt, isnan cimport numpy as cnp import numpy as np from cython cimport floating from cython.parallel cimport prange -from numpy.math cimport isnan from sklearn.utils._openmp_helpers import _openmp_effective_n_threads