如何使用scipy执行Levene的测试

我一直试图使用
scipy.stats.levene但没有成功.

我有一个形状为numpy矩阵(2128,45100).每行都是一个样本,属于3个集群中的一个.

我想测试集群之间是否存在同方差性.

我已经尝试按群集过滤我的矩阵并像这样发送参数:

from scipy.stats import levene

levene(matrixAudioData[np.ix_((cutTree == 0).ravel()),:][0],
       matrixAudioData[np.ix_((cutTree == 1).ravel()),:][0],
       matrixAudioData[np.ix_((cutTree == 2).ravel()),:][0])

ValueError: setting an array element with a sequence.

甚至

levene(matrixAudioData)

ValueError: Must enter at least two input sample vectors.

这有效:

levene([1,2,3],[2,3,4])

但是,如果每个样本不只是一个数字怎么办?

请注意,我用作参数的每个matrixAudioData [np.ix _((cutTree == 0).ravel()),:] [0]都有形状(1048,45100)所以应该没问题.

你们能指点我吗?

谢谢 !

最佳答案 基于
Box’s M Test formula,这是一个Python程序,用于在两个相等大小的协方差矩阵X0和X1上进行Box的M测试(即每个具有相同的行和列数),使用np.cov()函数存储为numpy数组.这已针对SPSS输出进行了测试.

Numpy是一个依赖项,缩写为np.

    def box_m(X0,X1):

        global Xp

        m = 2
        k = len(np.cov(X0))
        n_1 = len(X0[0])
        n_2 = len(X1[0])
        n = len(X0[0])+len(X1[0])

        Xp = ( ((n_1-1)*np.cov(X0)) + ((n_2-1)*np.cov(X1)) ) / (n-m)

        M = ((n-m)*np.log(np.linalg.det(Xp))) \
         - (n_1-1)*(np.log(np.linalg.det(np.cov(X0)))) - (n_2-1)*(np.log(np.linalg.det(np.cov(X1))))

        c = ( ( 2*(k**2) + (3*k) - 1 ) / ( (6*(k+1)*(m-1)) ) ) \
            * ( (1/(n_1-1)) + (1/(n_2-1)) - (1/(n-m)) )

        df = (k*(k+1)*(m-1))/2

        c2 = ( ((k-1)*(k+2)) / (6*(m-1)) ) \
            * ( (1/((n_1-1)**2)) + (1/((n_2-1)**2)) - (1/((n-m)**2)) )

        df2 = (df+2) / (np.abs(c2-c**2))

        if (c2>c**2):

            a_plus = df / (1-c-(df/df2))

            F = M / a_plus

        else:

            a_minus = df2 / (1-c+(2/df2))

            F = (df2*M) / (df*(a_minus-M))

        print('M = {}'.format(M))
        print('c = {}'.format(c))
        print('c2 = {}'.format(c2))
        print('-------------------')
        print('df = {}'.format(df))
        print('df2 = {}'.format(df2))
        print('-------------------')
        print('F = {}'.format(F)) 
点赞