votes up 1

len(levels) must equal the number of input sequences

Package:
scipy
github stars 8546
Exception Class:
ValueError

Raise code

s is None:
        # Call np.unique with return_inverse=True on each argument.
        actual_levels, indices = zip(*[np.unique(a, return_inverse=True)
                                       for a in args])
    else:
        # `levels` is not None...
        if len(levels) != nargs:
            raise ValueError('len(levels) must equal the number of input '
                             'sequences')

        args = [np.asarray(arg) for arg in args]
        mask = np.zeros((nargs, len0), dtype=np.bool_)
        inv = np.zeros((nargs, len0), dtype=np.intp)
        actual_levels = []
        for k, (levels_list, arg) in enumerate(zip(levels, args)):
            
ūüė≤  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! ūüö∂

Ways to fix

votes up 2 votes down

In the function scipy.stats.contingency.crosstab the length of the parameter levels should be the same as the given args parameter.

How to reproduce the error:

pipenv install scipy

from scipy.stats.contingency import crosstab
q1 = [2, 3, 3, 2, 4, 4, 2, 3, 4, 4, 4, 3, 3, 3, 4]  # 1 does not occur.
q2 = [4, 4, 2, 2, 2, 4, 1, 1, 2, 2, 4, 2, 2, 2, 4]  # 3 does not occur.
options = [1, 2, 3, 4]
vals, count = crosstab(q1, q2, levels=(options))
print(count)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-bb405d4a1ef6> in <module>()
      4 options = [1, 2, 3, 4]
      5 options2 = [1, 2, 3, 4]
----> 6 vals, count = crosstab(q1, q2, levels=(options))
      7 count

/usr/local/lib/python3.7/dist-packages/scipy/stats/_crosstab.py in crosstab(levels, sparse, *args)
    162         # `levels` is not None...
    163         if len(levels) != nargs:
--> 164             raise ValueError('len(levels) must equal the number of input '
    165                              'sequences')
    166 

ValueError: len(levels) must equal the number of input sequences

Fixed version of the code:

from scipy.stats.contingency import crosstab
q1 = [2, 3, 3, 2, 4, 4, 2, 3, 4, 4, 4, 3, 3, 3, 4]  # 1 does not occur.
q2 = [4, 4, 2, 2, 2, 4, 1, 1, 2, 2, 4, 2, 2, 2, 4]  # 3 does not occur.
options = [1, 2, 3, 4]
options2 = [1, 2, 3, 4]
vals, count = crosstab(q1, q2, levels=(options,options2))
count
Sep 04, 2021 kellemnegasi answer
kellemnegasi 31.6k

Add a possible fix

Please authorize to post fix