简体   繁体   中英

T-Test on between groups of numpy arrays

I have two groups of numpy arrays (A,B) and I want compare both groups using a statistical t-test (Two sample t-test). The result should again be an array of the same dimensions providing eg the p-value or another statistical index.

Here an two groups of example arrays I want to compare:

import numpy as np

A1= numpy.random.normal(1,1,100)
A2= numpy.random.normal(1,1,100)
A3= numpy.random.normal(1,1,100)
A4= numpy.random.normal(1,1,100)
A5= numpy.random.normal(1,1,100)


B1= numpy.random.normal(3,1,100)
B2= numpy.random.normal(3,1,100)
B3= numpy.random.normal(3,1,100)
B4= numpy.random.normal(3,1,100)
B5= numpy.random.normal(3,1,100)

Is that possible with a standard function of Numpy/Scipy? Or do I have to loop over each element of the array?

Since the five in each group are iid, you want to compare the concatenated A = [A1 A2 A3 A4 A5] to B = [B1 B2 B3 B4 B5]. You could have equivalently generated A = numpy.random.normal(1,1,500) , B = numpy.random.normal(3,1,500) .

Then calculate the mean and deviation of both ( numpy.mean , numpy.std ), and compute Student's t statistic. Or use scipy.stats.ttest_ind .

I think I found the right solution to get a t-test for each element of the array:

I involves to stack the arrays before performing the t-test:

import numpy
from scipy import stats


A1= numpy.random.normal(1,1,10).reshape(5, 2)
A2= numpy.random.normal(1,1,10).reshape(5, 2)
A3= numpy.random.normal(1,1,10).reshape(5, 2)
A4= numpy.random.normal(1,1,10).reshape(5, 2)
A5= numpy.random.normal(1,1,10).reshape(5, 2)
A = numpy.dstack((A1,A2,A3,A4,A5))

B1= numpy.random.normal(3,1,10).reshape(5, 2)
B2= numpy.random.normal(3,1,10).reshape(5, 2)
B3= numpy.random.normal(3,1,10).reshape(5, 2)
B4= numpy.random.normal(3,1,10).reshape(5, 2)
B5= numpy.random.normal(3,1,10).reshape(5, 2)
B = numpy.dstack((B1,B2,B3,B4,B5))

C = scipy.stats.ttest_ind(B,A,2)[1]

There remains just a small side question how to calculate eg the scipy.stats.mannwhitneyu() - Test (for non-normal data), where it is not possible to specify an array dimension like for the t-test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM