How to use ttest with np.vectorize across multiple 3d arrays
Question:
im a bit confused on how to implement a t-test across elements of multiple 3d arrays with np.vectorize.
Goal: So, take three arrays with the same dimensions and size and apply a t test across one element from each in identical positions, creating another array with the same dimensions and size as the original three, with the results
To start road required libraries
import numpy as np
import scipy.stats as stats
Consider,
#create 3 toy 3d arrays
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]]])
In a previous post I learned, about the vectorize function to conduct operations on across the arrays. Like so:
# Define the function to apply to the arrays
def some_function(x, y, z):
return f'result: {x} {y} {z}'
# Apply the function to the arrays using numpy.vectorize()
result = np.vectorize(some_function)(a, b, c)
# Print the result
print(result)
To conduct a t test, a a 1 dimensiuonal array it is pretty trivial.
eg = np.array([1,2,3,4])
t_statistic, p_value = stats.ttest_1samp(a=, popmean=5)
t_statistic
Where i am getting lost is how to integrate that into the example use of np.vectorize.
When I try:
def t_func(x,y,z):
return stats.ttest_1samp()
out = np.vectorize(stats.ttest_1samp(a,b,c, mean = 0))
We get:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Answers:
Apply numpy
‘s vectorization to t_func
:
def t_func(x,y,z):
return stats.ttest_1samp(a=[x, y, z], popmean=0)
t_func = np.vectorize(t_func)
out = t_func(a, b, c)
im a bit confused on how to implement a t-test across elements of multiple 3d arrays with np.vectorize.
Goal: So, take three arrays with the same dimensions and size and apply a t test across one element from each in identical positions, creating another array with the same dimensions and size as the original three, with the results
To start road required libraries
import numpy as np
import scipy.stats as stats
Consider,
#create 3 toy 3d arrays
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]]])
In a previous post I learned, about the vectorize function to conduct operations on across the arrays. Like so:
# Define the function to apply to the arrays
def some_function(x, y, z):
return f'result: {x} {y} {z}'
# Apply the function to the arrays using numpy.vectorize()
result = np.vectorize(some_function)(a, b, c)
# Print the result
print(result)
To conduct a t test, a a 1 dimensiuonal array it is pretty trivial.
eg = np.array([1,2,3,4])
t_statistic, p_value = stats.ttest_1samp(a=, popmean=5)
t_statistic
Where i am getting lost is how to integrate that into the example use of np.vectorize.
When I try:
def t_func(x,y,z):
return stats.ttest_1samp()
out = np.vectorize(stats.ttest_1samp(a,b,c, mean = 0))
We get:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Apply numpy
‘s vectorization to t_func
:
def t_func(x,y,z):
return stats.ttest_1samp(a=[x, y, z], popmean=0)
t_func = np.vectorize(t_func)
out = t_func(a, b, c)