votes up 1

Rankings must be of equal length.

Package:
scipy
github stars 8546
Exception Class:
ValueError

Raise code

""" 
    alternative hypothesis that the true value of Somers' D for the population
    is nonzero.

    """
    x, y = np.array(x), np.array(y)
    if x.ndim == 1:
        if x.size != y.size:
            raise ValueError("Rankings must be of equal length.")
        table = scipy.stats.contingency.crosstab(x, y)[1]
    elif x.ndim == 2:
        if np.any(x < 0):
            raise ValueError("All elements of the contingency table must be "
                             "non-negative.")
        if np.any(x != x.astype(int)):
            raise ValueError("All elements of the contingency table must be "

Ways to fix

votes up 1 votes down

If X and Y are 1D shape they should have the same length

How to recreate it:

$ pipenv install scipy

$ pipenv shell

from scipy.stats import somersd
import numpy as np

x =np.array([1,2,3,4,5])
y = np.array([1,3,2,4])
res = somersd(x,y)
print(res)

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-12-e2bad5363e17> in <module>()  5 y = np.array([1,3,2,4])  6  ----> 7 res = somersd(x,y)  8 print(res) 
/usr/local/lib/python3.7/dist-packages/scipy/stats/_hypotests.py in somersd(x, y)  649 if x.ndim == 1:  650 if x.size != y.size: --> 651 raise ValueError("Rankings must be of equal length.")  652 table = scipy.stats.contingency.crosstab(x, y)[1]  653 elif x.ndim == 2: 
ValueError: Rankings must be of equal length.

Fixed:

from scipy.stats import somersd
import numpy as np


x =np.array([1,2,3,4,5])
y = np.array([1,3,2,4,5])


res = somersd(x,y)
print(res)

SomersDResult(statistic=0.8, pvalue=0.0002607296328553156, table=array([[1, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]))

Oct 13, 2021 kellemnegasi answer
kellemnegasi 13.5k

Add a possible fix

Please authorize to post fix