Good evening. I am trying to run one sample t tests across each element of multiple arrays.
Ultimately, i would like to create two new 3d arrays of the exact dimensions of the data in which is analyzed, 1 3d array for T values and 1 3d array for their associated p values
The problem I am running into is I don't know how to handle the arrays.
Consider,
a = np.array([[[1,2,3,4], [5,6,7,8], [9,10,11,12]]])
b = np.array([[[13,14,15,4], [1,6,9,10], [1,0,.5, .2]]])
c = np.array([[[0,0,1,2], [1,1,3,4], [4,1,11,17]]])
If I run something like np.vstack, stack, hstack, the dimensions of the new array change from the original. It was my thought that If i could stack the arrays somehow, then i could run the t tests, but I don't think this is the way to go.
Ultimately, id like each element to follow the format below, which I created for the first dimension of the 3 arrays.
this code will definitely not work, but i thought illustrated my desired outcome well
new = np.array([[[res.ttest(1,13,0), res.ttest(2,14,0), res.ttest(3,15,1), res.ttest(4,4,2)], [next dimension of t tests], [next dimension of t tests])
It appears you want to take three arrays with the same dimensions and size and apply some function across one element from each in identical positions, creating another array with the same dimensions and size as the original three, with the results.
numpy.vectorize()
will allow you to apply a function just like that.
Here's an example:
import numpy as np
# Define the function to apply to the arrays
def some_function(x, y, z):
return f'result: {x} {y} {z}'
# Your test data
a = np.array([[[1,2,3,4], [5,6,7,8], [9,10,11,12]]])
b = np.array([[[13,14,15,4], [1,6,9,10], [1,0,.5, .2]]])
c = np.array([[[0,0,1,2], [1,1,3,4], [4,1,11,17]]])
# Apply the function to the arrays using numpy.vectorize()
result = np.vectorize(some_function)(a, b, c)
# Print the result
print(result)
The output:
[[['result: 1 13.0 0' 'result: 2 14.0 0' 'result: 3 15.0 1' 'result: 4 4.0 2']
['result: 5 1.0 1' 'result: 6 6.0 1' 'result: 7 9.0 3' 'result: 8 10.0 4']
['result: 9 1.0 4' 'result: 10 0.0 1' 'result: 11 0.5 11' 'result: 12 0.2 17']]]
Of course, instead of calling it with some_function
, you can just call it with res.ttest
.
It's unclear what you meant by "their associated p values", as I don't think you provided an example of what you want that to contain. But I assume there's another way to use vectorize()
that would allow you to get that array as well.