votes up 1

Sample is not a 2D array

Package:
scipy
github stars 8546
Exception Class:
ValueError

Raise code

""" 
    0.008142039609053513

    """
    sample = np.asarray(sample, dtype=np.float64, order="C")

    # Checking that sample is within the hypercube and 2D
    if not sample.ndim == 2:
        raise ValueError("Sample is not a 2D array")

    if not (np.all(sample >= 0) and np.all(sample <= 1)):
        raise ValueError("Sample is not in unit hypercube")

    workers = int(workers)
    if workers == -1:
        workers = os.cpu_count()  # type: ignore[assignment]
😲 Agile task management is now easier than calling a taxi. #Tracklify

Ways to fix

votes up 2 votes down

scipy.stats.qmc.discrepancy computes the discrepancy of a given sample. The space parameter should be a 2D array.

Reproducing the error:

pipenv install scipy numpy

from scipy.stats import qmc
import numpy as np
space = np.array([[1, 3], [2, 6], [3, 2], [4, 5], [5, 1], [6, 4]])
l_bounds = [0.5, 0.5]
u_bounds = [6.5, 6.5]
space = space.flatten()
space = qmc.scale(space, l_bounds, u_bounds, reverse=True)
qmc.discrepancy(space)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-c588518f83df> in <module>()
      5 u_bounds = [6.5, 6.5]
      6 space = space.flatten()
----> 7 space = qmc.scale(space, l_bounds, u_bounds, reverse=True)
      8 qmc.discrepancy(space)

/usr/local/lib/python3.7/dist-packages/scipy/stats/_qmc.py in scale(sample, l_bounds, u_bounds, reverse)
    158     # Checking bounds and sample
    159     if not sample.ndim == 2:
--> 160         raise ValueError('Sample is not a 2D array')
    161 
    162     lower, upper = np.broadcast_arrays(lower, upper)

ValueError: Sample is not a 2D array

Fixed:

from scipy.stats import qmc
import numpy as np
space = np.array([[1, 3], [2, 6], [3, 2], [4, 5], [5, 1], [6, 4]])
l_bounds = [0.5, 0.5]
u_bounds = [6.5, 6.5]
# space = space.flatten()
space = qmc.scale(space, l_bounds, u_bounds, reverse=True)
qmc.discrepancy(space)
Sep 05, 2021 kellemnegasi answer
kellemnegasi 31.6k

Add a possible fix

Please authorize to post fix