I'd like to create a function with two arguments (a, axis=0)
that computes the coefficient of variation of each column or row (2-dimensional array) and returns the index of the column or row with the maximum coefficient of variation.
I understand that .argmax
returns the indices of the maximum values along an axis, but I'm unsure of how to proceed after that.
I'd like for the code to pass the following tests:
print(np.asscalar(arg_cvmax(b)) is 2)
print(np.asscalar(arg_cvmax(b,1)) is 0)
print(np.asscalar(arg_cvmax(b,axis=1)) is 0)
variation(arr, axis = None) function computes the coefficient of variation. It is defined as the ratio of standard deviation to mean. Parameters : arr : [array_like] input array.
CV = σ / μ where: σ: The standard deviation of dataset.
The variance is the average of the squared deviations from the mean, i.e., var = mean(x) , where x = abs(a - a. mean())**2 . The mean is typically calculated as x. sum() / N , where N = len(x) .
Both the standard deviation and the coefficient of variation measure the spread of values in a dataset. The standard deviation measures how far the average value lies from the mean. The coefficient of variation measures the ratio of the standard deviation to the mean.
About putonspectacles's answer.
Since the variation calculate the ratio of the biased standard deviation to the mean, it make sense to change the np.var
in original lambda to np.std
.
The non-stats import version should be like this:
A = np.random.randn(10, 10)
cv = lambda x: np.std(x) / np.mean(x)
var = np.apply_along_axis(cv, axis=0, arr=A)
idmax = np.argmax(var)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With