Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coefficient of Variation and NumPy

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 .argmaxreturns 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)
like image 588
clovis Avatar asked Mar 12 '17 23:03

clovis


People also ask

How do you do coefficient of variation in Python?

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.

Where can I find CV in Python?

CV = σ / μ where: σ: The standard deviation of dataset.

How do you find the variance in Numpy?

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) .

What is the difference between SD and CV?

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.


1 Answers

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)
like image 114
snail123815 Avatar answered Sep 21 '22 12:09

snail123815